var previous = 0;
var next = 0;
var INTERVAL = 5000;
var isIE6 = false;
var isIE = false;
var formValid = false;

$(document).ready(function(){
  if($.browser.msie){
    isIE = true;
    if($.browser.version == '6.0'){
     isIE6 = true; 
    }
  }
  addSlideShowBehaviour();
  addLoginBehaviour();
  addPrintLinkBehaviour();
  addFooterPrintLinkBehaviour();
  initializeSearchbox();
  if(isIE6){
    floatIE6MainnaviToLeft();
    addIE6HomeBehaviour();
  }else{
    floatMainnaviToLeft();
    addHomeBehaviour();
  }
  initializeGoogleMap();
  addFormBehaviour();
});

function addFormBehaviour(){
  if($('form[enctype$=form-data]').length > 0){
    $('form[enctype$=form-data] select[name=bereich]').change(function(){
      var text = $(this).attr('value');
      text = $('form[enctype$=form-data] input[name=subject]').attr('value')+" "+text;
      $('form[enctype$=form-data] input[name=subject]').attr('value', text);
    });
  }
}

function checkForm(){
  check = true;
  if($('form[enctype$=form-data] input[name=vorname]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=vorname]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=vorname]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=name]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=name]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=name]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=firma]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=firma]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=firma]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=strasseNummer]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=strasseNummer]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=strasseNummer]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=plzOrt]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=plzOrt]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=plzOrt]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] select[name=Land]').attr('value') == ''){
    $('form[enctype$=form-data] select[name=Land]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] select[name=Land]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=telefon]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=telefon]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=telefon]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=email]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=email]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=email]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] select[name=bereich]').attr('value') == ''){
    $('form[enctype$=form-data] select[name=bereich]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] select[name=bereich]').removeClass('form-error');
  }
  if($('form[enctype$=form-data] textarea').attr('value') == ''){
    $('form[enctype$=form-data] textarea').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] textarea').removeClass('form-error');
  }
  if($('form[enctype$=form-data] input[name=captchaResponse]').attr('value') == ''){
    $('form[enctype$=form-data] input[name=captchaResponse]').addClass('form-error');
    check = false;
  }else{
    $('form[enctype$=form-data] input[name=captchaResponse]').removeClass('form-error');
  }
  return check;
}

function initializeGoogleMap(){
  if($('#googlemap').length > 0){
    loadGoogleMap();
    $('#geofinder').hide();
  }
}

function addHomeBehaviour(){
  if($('div.bottom-text').length > 0){
    $('#frame1').hover(function(){
      $('.bottom-text:first p').show();
      $('.bottom-text:last p').hide();
      $('#frame2').attr('style', '');
    },function(){
      $('.bottom-text:first p').hide();
      $('#frame1').attr('style', '');
    });
    $('.bottom-text:first p').mouseover(function(){
      $('.bottom-text:first p').show();
      $('#frame1').attr('style', 'background-color: #013E7A; background-image: none;');
    });
    $('#frame2').hover(function(){
      $('.bottom-text:last p').show();
      $('.bottom-text:first p').hide();
      $('#frame1').attr('style', '');
    },function(){
      $('.bottom-text:last p').hide();
      $('#frame2').attr('style', '');
    });
    $('.bottom-text:last p').mouseover(function(){
      $('.bottom-text:last p').show();
      $('#frame2').attr('style', 'background-color: #013E7A; background-image: none;');
    });
  }
}

function floatMainnaviToLeft(){
  if($('li.float-to-left').length > 0){
    var abzug = 15;
    var pixel = $('#main-navi-top li.act ul').width() - $('#main-navi-top li.act').width() - abzug;
    $('li.float-to-left ul').attr('style', 'margin-left: -'+pixel+'px;');
  }
}

function initializeSearchbox(){
  if($('#indexedsearch').length > 0){
    if($('#tx_indexedsearch-input').attr('value')==''){
      var text = $('#searchbox-text').attr('value');
      $('#tx_indexedsearch-input').attr('value', text);
    }
    $('#tx_indexedsearch-input').click(function(){
      if($('#tx_indexedsearch-input').attr('value') == $('#searchbox-text').attr('value')){
        $('#tx_indexedsearch-input').attr('value', '');
      }
    });
  }
}

function addPrintLinkBehaviour(){
  if($('#print-link').length > 0){
    $('#top-header ul li.print-link').show();
    $('#print-link').click(function(e){
      e.preventDefault();
      window.print();
    });
  }
}

function addFooterPrintLinkBehaviour(){
  if($('#footer-print-link').length > 0){
    $('#footer-print-link').show();
    $('#footer-print-link').click(function(e){
      e.preventDefault();
      window.print();
    });
  }
}

function addLoginBehaviour(){
  if(!isIE6 && $('.logout').length > 0 && $.getURLParam("id")!=19){
    var lang = 'id=19&L=0';
    var element = '#login';
    if($('#login_fr').length>0){
      var lang = 'id=19&L=1';
      var element = '#login_fr';
    }
    if($('#login_eng').length>0){
      var lang = 'id=599';
      var element = '#login_eng';
    }
    $(element).click(function(e){
      e.preventDefault();
      $('#ajax-content').addClass('ajax-content-login');
      $('#ajax-content').load('index.php?'+lang+' #content-middle', function(){
        $('#ajax-content div:first-child').removeClass('span-12');
        $('#ajax-content div:first-child input[type=submit]').attr('value', '');
        $('#ajax-content div:first-child').attr('style', 'margin-left: 0px;width:315px;display:block;');
        $('#ajax-content div:first-child h1').prepend('<a id="close-ajax" href="#" alt="close"><img src="http://www.gwf.ch/fileadmin/template/images/close.png" title="close"/></a>');
        $('#filter').fadeIn(125, function(){
          $('#ajax-content').fadeIn(125, function(){
            $('#close-ajax').click(function(e){
              e.preventDefault();
              $('#ajax-content').fadeOut(125, function(){
                $('#filter').fadeOut(125);
              });
            });
          });
        });
      });
    });
  }
}

function addSlideShowBehaviour(){
  if($('#top-header-image').length > 0){
    $('#top-header-image').bannerrotator('http://www.gwf.ch/fileadmin/slideshow/slideshow'+Math.round(Math.random()*2)+'.xml',{fade:1000,timeout:3000});
  }
  if($('#flash').length > 0){
    $('#flash').show();
    $('#flash').flash({ src: 'http://www.gwf.ch/fileadmin/template/flash/logoentwicklung.swf',
                        width: 300, 
                        height: 34,
                        wmode: 'transparent' },
                      { version: 8 });
    $('#main-navi').attr('style', 'margin-top: -38px;');
  }
}

function slideshow(){
  $('#slideimg'+previous).fadeOut(125, function(){
    $('#slideimg'+next).fadeIn(125);
  });
  previous = next;
  next++;
  if(next > 3){
    next = 1;
  }
}

/**************************************/
/* this section below is just for IE6 */
/**************************************/
var frame1On = false;
var frame2On = false;

function addIE6HomeBehaviour(){
  if($('div.bottom-text').length > 0){
    $('#shortcuts li:first-child').addClass('first-child');
    $('#frame1').hover(function(){
      hideIE6IMG('#frame1');
      showIE6Ptag(0);
    },function(){
      showIE6IMG(1);
      hideIE6Ptag(0);
    });
    $('.bottom-text:first').hover(function(){
      hideIE6IMG('#frame1');
      showIE6Ptag(0);
    },function(){
      showIE6IMG(1);
      hideIE6Ptag(0);
    });
    $('#frame2').hover(function(){
      hideIE6IMG('#frame2');
      showIE6Ptag(1);
    ;},function(){
      showIE6IMG(2);
      hideIE6Ptag(1);
    });
    $('.bottom-text:last').hover(function(){
      hideIE6IMG('#frame2');
      showIE6Ptag(1);
    ;},function(){
      showIE6IMG(2);
      hideIE6Ptag(1);
    });    
  }
}

function showIE6IMG(id){
  switch(id){
    case 1:
      $('#frame1').attr('style', 'background-image:url(http://www.gwf.ch/fileadmin/template/home/versorgung1.jpg);');
      break;
    case 2:
      $('#frame2').attr('style', 'background-image:url(http://www.gwf.ch/fileadmin/template/home/gebaeude1.jpg);');
      break;
    default:
      break;
  }
}

function hideIE6IMG(id){
  $(id).attr('style', 'background-image:none;background-color:#013E7A;');
}

function showIE6Ptag(pos){
  var i = 0;
  $('.bottom-text').each(function(){
    if(i==pos){
      $(this).children('p').show();
    }
    i++;
  });
}

function hideIE6Ptag(pos){
  var i = 0;
  $('.bottom-text').each(function(){
    if(i==pos){
      $(this).children('p').hide();
    }
    i++;
  });
}

function floatIE6MainnaviToLeft(){
  if($('li.float-to-left').length > 0){
    var width = 0;
    $('#main-navi-top li.act ul li').each(function(){
      width += $(this).width();
      if($(this).width()>0){
        width += 32;
      }
    });
    abzug = 0;
    $('li.float-to-left ul').attr('style', 'margin-left: -'+(width-$('#main-navi-top li.act').width()-15-abzug)+'px;width:'+(width)+'px;');
  }
}
