var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27540875-1']);
_gaq.push(['_setDomainName', 'extremevital.com']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

$(document).ready(function() {

    // credit card links
    //$('#cards a').bind('click', function() {return false;});

    // initialize animation variables
    var time = 5000;
    var categories = $('#categories li');
    var count = categories.length - 2;
    var timer = 0;
    var i=0,j=0,k=0;
    var filename = window.location.href.substr(window.location.href.lastIndexOf("/")+1);

    if (filename == 'index-en.html') {
      var subfolder = 'en/';
    } else {
      var subfolder = '';
    }
    // bind events and start animation
    $('#categories li').bind('mouseenter', pauseAnimation);
    $('#categories li').bind('mouseleave', resumeAnimation);
    startAnimation(0);

    // activate category panel
    function activateCategory(j) {
        $(categories[j]).find('span.fix').hide();
        $(categories[j]).find('a').css('background-position', '0 0');
        $(categories[j]).addClass('active').css('z-index', 50);

        if (j == 0) {
          $('#tourimg').attr('src', 'tour/bikes.jpg').stop(true,true).hide().fadeIn(500, 'linear');
          $("a#tourhref").attr("href", "tour/" + subfolder + "bike/index.html");
        } else if (j == 1) {
          $('#tourimg').attr('src', 'tour/skiis.jpg').stop(true,true).hide().fadeIn(500, 'linear');
          $("a#tourhref").attr("href", "tour/" + subfolder + "ski/index.html");
        } else if (j == 2) {
          $('#tourimg').attr('src', 'tour/board.jpg').stop(true,true).hide().fadeIn(500, 'linear');
          $("a#tourhref").attr("href", "tour/" + subfolder + "board/index.html");
        } else if (j == 3) {
          $('#tourimg').attr('src', 'tour/cwear.jpg').stop(true,true).hide().fadeIn(500, 'linear');
          $("a#tourhref").attr("href", "tour/" + subfolder + "casual/index.html");
        } else if (j == 4) {
          $('#tourimg').attr('src', 'tour/thule.jpg').stop(true,true).hide().fadeIn(500, 'linear');
          $("a#tourhref").attr("href", "tour/" + subfolder + "thule/index.html");
        }
    }

    // deactivate category panel
    function deactivateCategory(k) {
        $(categories[k]).find('span.fix').show();
        $(categories[k]).find('a').css('background-position', '-211px 0');
        $(categories[k]).removeClass('active');
    }

    // start animation function
    function startAnimation() {
        timer = setTimeout(function() {
            deactivateCategory(i);
            if ( i > count ) {
                i = 0;
                $(categories).each(function(index){
                    $(categories[index]).css('z-index', 5-index);
                });
            } else {
                i++;
            }
            activateCategory(i);
            $(categories[i]).find('img:visible').fadeOut('slow');
            $(categories[i]).find('img:hidden').fadeIn('slow');
            startAnimation(i);
        }, time);
    }

    // pause animation function
    function pauseAnimation() {
        deactivateCategory(i);
        i = $(this).index();
        $(categories).each(function(index){
            $(categories[index]).css('z-index', i > index ? index+1 : 50-index);
        });
        activateCategory(i);
        clearTimeout(timer);
    }

    // resume animation function
    function resumeAnimation() {
        i = $(this).index();
        //activateCategory(i);
        startAnimation(i);
    }

    // dump variable to console
    function dump(variable) {
        if ( window.console ) {
            console.log(variable);
        }
    };

	$('a[rel="lightbox"]').fancybox({
		'width'         : 975,
		'height'        : 400,
		'speedIn'       : 200,
		'speedOut'      : 200,
		'overlayOpacity': 0.6,
		'overlayColor'  : '#000',
		'autoScale'     : false,
		'transitionIn'  : 'fade',
		'transitionOut' : 'fade',
		'type'          : 'iframe',
		'scrolling'     : 'no'
	});

});
