$(document).ready(function(){
    var cook= iJouleBrowser.getCookieLang();
	var lang = iJouleBrowser.getBrowserLang();
	
	if(window.location.pathname =='/'){
    //check language
	
		if(cook!=null && cook !=""){
            //check for language redirect due to customer prefs
			if(cook=="de")
				window.location.replace('/de');
			
		}
		else{
            //set language due to browser prefs
            iJouleBrowser.setCookieLang(lang);
            if(lang == 'de')
                window.location.replace('/de');
		}
	
	}	
	
	$('.lang').click(function(){
		if(cook!=null && cook !=""){
            iJouleBrowser.setCookieLang(cook=='de'?'en':'de'); //switch language in the cookie
		}
	});
	
	
	
/*Clouds*/
	var timed = window.setTimeout(changePos, 2000);
	function changePos(){
		if($('.img_cloud').css('visibility') != 'hidden'){
			$("#img_cloud_1").css("left", "500px");
			$("#img_cloud_2").css("left", "50px");
			$("#img_cloud_3").css("left", "700px");
			$("#img_cloud_4").css("left", "200px");
			$("#img_cloud_5").css("left", "650px");
		}
	}

/*Android-Mail*/
	$('#android_signup form').hide();
	$('#android_signup').hide();
	$('#android_figure, #android_pfeil').click(function() {
		$('#android_signup form').hide();
		$('#android_signup').toggle(200, function(){
			$('#android_signup form').fadeToggle();
		});
	});
	
/*Text-Slide*/
	$('#content_regeln_info_bottom').hide();
	$('#content_regeln_info_mehr').click(function() {
		$('#content_regeln_info_bottom').slideToggle(400, 'swing');
	});
	
	$('#regeln div span').next().hide();
	$('#regeln div span').click(function() {
		$(this).next().slideToggle(400, 'swing');
	});
	
	$('#regeln h3').click(function() {
			$(this).next().slideToggle(400);
			$('#regeln h3').not(this).next().slideUp(400);
			return false;
	}).next().hide();
	
});


var iJouleBrowser = {
    setCookieLang: function(lang) {
        $.cookie('lang', lang, {path: '/'});
    },
    getCookieLang: function() {
        return $.cookie('lang');
    },
    getBrowserLang: function() {
        return (navigator.language || navigator.systemLanguage || navigator.userLanguage || 'en').substr(0, 2).toLowerCase();
    }
}

/*Footer-Hide*/
$(document).ready(function(){
	if($('.img_cloud').css('visibility') != 'hidden'){
		$('#footer').css('overflow','hidden');
		$('#footer ul').css('position','relative');
		$('#footer ul').css('left','221px');
		$('#footer_mehr').toggle(
			function(){
				$('#footer ul').stop().animate({'left':'0px'},400);
			},
			function(){
				$('#footer ul').stop().animate({'left':'221px'},400);
			}
		);
	}
});

/*Recommended List*/
$(document).ready(function(){
	$('#recommended').innerfade({
		animationtype: 'fade',
		speed: 750,
		timeout: 2000,
		type: 'sequence',
		containerheight: '20px'
	});
	$('#recommended li').css('margin','0px');
});

/*Presse-Slide*/
$(document).ready(function(){
    /* This code is executed after the DOM has been completely loaded */
    
    var totWidth=0;
    var positions = new Array();
    
    $('#slides .slide').each(function(i){
        
        /* Traverse through all the slides and store their accumulative widths in totWidth */
        
        positions[i]= totWidth;
        totWidth += $(this).width();
        
        /* The positions array contains each slide's commulutative offset from the left part of the container */
        
        if(!$(this).width())
        {
            alert("Please, fill in width & height for all your images!");
            return false;
        }
        
    });
    
    $('#slides').width(totWidth);

    /* Change the cotnainer div's width to the exact width of all the slides combined */

    $('#menu ul li a').click(function(e,keepScroll){

            /* On a thumbnail click */

            $('li.menuItem').removeClass('act').addClass('inact');
            $(this).parent().addClass('act');
            
            var pos = $(this).parent().prevAll('.menuItem').length;
            
            $('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
            /* Start the sliding animation */
            
            e.preventDefault();
            /* Prevent the default action of the link */
            
            
            // Stopping the auto-advance if an icon has been clicked:
            if(!keepScroll) clearInterval(itvl);
    });
    
    $('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
    /* On page load, mark the first thumbnail as active */
    
    
    
    /*****
     *
     *  Enabling auto-advance.
     *
     ****/
     
    var current=1;
    function autoAdvance()
    {
        if(current==-1) return false;
        
        $('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('click',[true]);   // [true] will be passed as the keepScroll parameter of the click function on line 28
        current++;
    }

    // The number of seconds that the slider will auto-advance in:
    
    var changeEvery = 10;

    var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

    /* End of customizations */
});
