var player = null;
function playerReady(thePlayer) {
  player = document.getElementById('player');
  //player = window.document['player'];
  //alert("Player: "+player);
  //player.sendEvent('PLAY')
}

var myData = new Object();
$( document ).ready( function(){

  $("#login-form").hide();

  // Mouseover functions
  $('#menu-login-button').click(function(){
    return false;
  });

  $('#menu-login-button').mouseover(function(){
    if(window.willhide) clearTimeout(window.willhide);
    $('#login-form').slideToggle('fast');
  });
  $('#login-form').mouseover(function(){
    if(window.willhide) clearTimeout(window.willhide);
  });
  // Focus functions
  $('#menu-login-button').focus(function(){
    if(window.willhide) clearTimeout(window.willhide);
  });
  $('#login-form').focus(function(){
    if(window.willhide) clearTimeout(window.willhide);
  });
  // Mouseout functions
  $('#menu-login-button').mouseout(function(){
    window.willhide = setTimeout(function() {
      $('#login-form').slideToggle('fast');
    },1000);
  });
  $('#login-form').mouseout(function(){
    window.willhide = setTimeout(function() {
      $('#login-form').slideToggle('fast');
    },1000);
  });
  // Blur functions
  $('#menu-login-button').blur(function(){
    window.willhide = setTimeout(function() {
      $('#login-form').slideToggle('fast');
    },1000);
  });
  $('#login-form').blur(function(){
    window.willhide = setTimeout(function() {
      $('#login-form').slideToggle('fast');
    },1000);
  });

  if ( $("#login-error-notice").length>0 ){
    window.willhide = setTimeout(function() {
      $('#login-form').slideToggle('fast');
    },4000);
    $('#login-form').slideToggle('fast');
  }

  // Switch between the text field and the password field
  $('#password').hide();
  $('#password-text').click(function() {
    $(this).hide();
    $(this).siblings('#password').show();
    $(this).siblings('#password')[0].focus();
  });
  $('#password-text').focus(function() {
    $(this).hide();
    $(this).siblings('#password').show();
    $(this).siblings('#password')[0].focus();
  });
  $('#password').blur(function() {
    if ($(this).val()=='') {
     $(this).hide();
     $(this).siblings('#password-text').show();
    }
  });

  $('a[href="' + window.location.pathname + '"]').addClass('selected');
  $('a[href="' + window.location.pathname + '"]').parent().addClass('selected');

    // Function to select all the input fields in the page and store their original
    // values for further usage.
    $('.input-field').each(function() {
        myData[$(this).attr("name")] = $(this).val();
    });
    // Function to clear the input fields
    $('.input-field').click(function() {
        input_text = $(this).val();
        if( input_text == myData[ $(this).attr("name") ] ){
            $(this).val('');
        }
    });
    // Function to refill the input fields to the original value
    $('.input-field').blur(function() {
        input_text = $(this).val();
        if(input_text == ''){
            $(this).val(myData[$(this).attr("name")]);
        }
    });

    /* hover classes for IE6 */
    $("#nav li, #prdMenu li, #prdMenuS li, #infoImgs div, div.slideshow-container ul li")
        .hover(function () {$(this).addClass('sfhover')}, function() {$(this).removeClass('sfhover')});

    $("div.slideshow-container > button.slideshowPrev")
        .hover(function(){$(this).addClass('sfhoverPrev');}, function(){$(this).removeClass('sfhoverPrev');});
    $("div.slideshow-container > button.slideshowNext")
        .hover(function(){$(this).addClass('sfhoverNext');}, function(){$(this).removeClass('sfhoverNext');});

    $( '#topMenu' ).scrollFollow( {container: 'footerWrapper', offset: 2});


    if ($("#sx").length) {
      $('#sx').cycle({
        fx         : 'fade',
        sync       : 0,
        timeout    : 12000,
        pager      : '#sx_nav',
        prev       : '#prev',
        next       : '#next',
        after      : onAfter,
        pagerClick : function(zeroBasedSlideIndex, slideElement) {
          $("#sx").cycle('pause');
        }
      });
    }
    

    if ($(".slideshow").jCarouselLite) {
      $(".slideshow").jCarouselLite({
        btnNext: ".slideshowNext",
        btnPrev: ".slideshowPrev",
        visible: 3,
        speed: 400,
        circular: false,
        vertical: true
      });
    }
    
    /*$("#pressMenu li > a").click(function() {
        var $curActive = $("#pressMenu li.sfhover > a");
        $curActive.parent('li').removeClass('sfhover').end().next('div.discText').slideToggle('slow');
        if ($curActive[0] !== this) {
            $(this).parent('li').toggleClass('sfhover').end().next('div.discText').slideToggle('slow');
        }
        return false;
    });*/

    // Products Gallery
    productPictsOpacity('#infoImgs .prdInfoPicts');
    productPictsOpacityV2('#fading-images td');

    $("a.compAustria").click(function(e) {
      e.preventDefault();
      $(this).parent('div').hide('fast');
      $("#compAustria_body").slideDown(1500);
    });

    $("div.faqH > h4").click(function() {
      var $sender = $(this);
      //$sender.parent('div').siblings('div.faqH > div').hide();
      $sender.siblings('div').slideToggle('slow');
    });

    if ($("#PressHL a.fancyImgs").length) {
      $("#PressHL a.fancyImgs").fancybox({
        'speedIn'     : 800,
        'speedOut'    : 300,
        'overlayShow'	: true
      });
    }

    if ($(".fancybox").length > 0) {
      $(".fancybox").fancybox({
        'speedIn'     : 800,
        'speedOut'    : 300,
        'overlayShow'	: true
      });
    }

    if ($("#prodGallery").length) {
      $("#prodGallery .thumbMask").hover(function() {
        var $li = $(this).parent('li').addClass('sfhover');
        $('div.hoverOpac', $li).fadeTo(600, 0.75);
        
      }, function() {
        var $li = $(this).parent('li').removeClass('sfhover');
        $('div.hoverOpac', $li).hide();
      }).click(function() {
        var $sender = $(this);
        var $LI = $sender.parent('li');
        var ftype = $LI.attr('vtype');
        var vd;
        if ('youtube' == ftype) {
          vd = {'file': $LI.attr('rel'), 'type': ftype};
        }
        else {
          vd = $(this).parent('li').attr('rel');
        }

        var $info = $sender.prev('div.hoverOpac');
        $("#vidTitle").text($("div.vtitle", $info).text());
        $("#vidDisc").text($("div.vidInfo", $info).text());

        player.sendEvent('LOAD', vd);
        //player.sendEvent('PLAY');  not used if autostart is set!
      });

    }

    if ($("#jwplayer").length) {
      var $cvId = $("#" + gvId);
      var s1 = new SWFObject('/images/jwplayer/player.swf','player','500','375','9');
      s1.addParam('allowfullscreen','true');
      s1.addParam('allowscriptaccess','always');
      s1.addParam('wmode','opaque');

      var video = $cvId.attr('rel');
      if ('youtube' == $cvId.attr('vtype')) {
        //s1.addParam('flashvars','file='+video+'&type=youtube');
        s1.addVariable('file', video);
        s1.addVariable('type', 'youtube');
      }
      else {
        //s1.addParam('flashvars','file='+video);
        s1.addVariable('file', video);
      }
      s1.addVariable('autostart','false');
      s1.write('jwplayer');
      
      $("#vidTitle").text($("div.vtitle", $cvId).text());
      $("#vidDisc").text($("div.vidInfo", $cvId).text());
    }

    if ($("#rssReg").length) {
      $("#rssReg").hover(function() {
        $(".rssRegInfo").show('slow');
      }, function() {
        $(".rssRegInfo").hide('slow');
      });
    }

});

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    if (0 == index) {
        $("#prev").attr('class', 'first_el');
    }
    else {
        $("#prev").attr('class', 'first_el_g');
    }
    if ((opts.slideCount - 1) == index) {
        $("#next").attr('class', 'next_el');
    }
    else {
        $("#next").attr('class', 'next_el_g');
    }
    /*var prev_t = 0;
    if (index == 0) {
      prev_t = 3;
    }
    else {
      prev_t = index;
    }*/
    var targets = index + 1;
    //$("#comBanner .s"+prev_t).hide();
    $("#comBanner .s1, #comBanner .s2, #comBanner .s3").hide();
    $("#comBanner .s"+targets).show('slow');
}

function productPictsOpacity(thumbs) {
  var activeOpacity = 1.0, inactiveOpacity = 0.6, fadeTime = 350, clickedClass = "selected";
  $(thumbs).fadeTo(1, inactiveOpacity);
  $(thumbs).hover(
      function() {
        $(this).fadeTo(fadeTime, activeOpacity);
//        $("div.info > div", $(this)).fadeIn(500);
      },
      function() {
        $(this).fadeTo(fadeTime, inactiveOpacity);
//        $("div.info > div", $(this)).fadeOut(500);
      }
    );
}

function productPictsOpacityV2(thumbs) {
  var activeOpacity = 1.0, inactiveOpacity = 0.6, fadeTime = 350, clickedClass = "selected";
  $(thumbs).fadeTo(1, inactiveOpacity);
  $(thumbs).hover(
    function() {
      var myClass = $(this).attr("class");
      $("."+myClass).stop().fadeTo(fadeTime, activeOpacity);
    },
    function() {
      var myClass = $(this).attr("class");
      $("."+myClass).stop().fadeTo(fadeTime, inactiveOpacity);
    }
  );
}

//$("div.info > div").each(function(){
//  $(this).hide();
//});