$(document).ready(function() {
 	
	season = gcookie('season');
	
	if((season != "winter") && (season != "summer")){
		var currentMonth = new Date().getMonth();
		currentMonth = currentMonth + 1;
		if((currentMonth >= 11) || (currentMonth < 4)){
		//winter
    		scookie('season', 'winter', 0)
  			changeCss('winter');
			$("#content_img a.illustrations").fancybox({
			titleShow: 'false',
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			speedIn: 600,
			speedOut: 300,
			overlayOpacity: 0.8,
			overlayColor: '#1F2F50'
			});
			$(document).pngFix();
		}else{
		//summer
			scookie('season', 'summer', 0)
  			changeCss('summer');
  			$("#content_img a.illustrations").fancybox({
			titleShow: 'false',
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			speedIn: 600,
			speedOut: 300,
			overlayOpacity: 0.8,
			overlayColor: '#FFF'
			});
			$(document).pngFix();
		}
	}
	
	if(season == 'winter'){
		changeCss('winter');
		$("#content_img a.illustrations").fancybox({
		titleShow: 'false',
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		speedIn: 600,
		speedOut: 300,
		overlayOpacity: 0.8,
		overlayColor: '#1F2F50'
		});
		$(document).pngFix();
		$('#links_home').corner("cc:#1F2F50 5px");
		$('#last_news_home').corner("cc:#1F2F50 5px");
		$('#next_events_home').corner("cc:#1F2F50 5px");
		$('#spots_TV_home').corner("cc:#1F2F50 5px");
		$('#menu_medias_page').corner("cc:#1F2F50 5px");
		$('#menu_seasons_page').corner("cc:#1F2F50 right 5px");
		$('.block_sub_page_txt').corner("cc:#1F2F50 left 5px");
		$('#language_selector').corner("cc:#1F2F50 bottom 5px");
		$('#block_meteo').corner("cc:#1F2F50 bottom 5px");
		$('#menu ul.sf-menu').corner("cc:#1F2F50 5px");
		$('#menu ul.sf-menu li:first-child').corner("cc:#1F2F50 top 5px");
		$("#content_txt img").imgr({size:"1px",color:"#006494", radius:"5px"});
	}else{
		changeCss('summer');
		$("#content_img a.illustrations").fancybox({
		titleShow: 'false',
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		speedIn: 600,
		speedOut: 300,
		overlayOpacity: 0.8,
		overlayColor: '#FFF'
		});
		$(document).pngFix();
		$('#links_home').corner("cc:#FFF 5px");
		$('#last_news_home').corner("cc:#FFF 5px");
		$('#next_events_home').corner("cc:#FFF 5px");
		$('#spots_TV_home').corner("cc:#FFF 5px");
		$('#menu_medias_page').corner("5px");
		$('#menu_seasons_page').corner("cc:#FFF right 5px");
		$('.block_sub_page_txt').corner("cc:#FFF left 5px");
		$('#language_selector').corner("cc:#FFF bottom 5px");
		$('#block_meteo').corner("cc:#FFF bottom 5px");
		$('#menu ul.sf-menu').corner("cc:#FFF 5px");
		$('#menu ul.sf-menu li:first-child').corner("cc:#FFF top 5px");
		$("#content_txt img").imgr({size:"1px",color:"#003C1F", radius:"5px"});
	}
	
	height_max = 0;
	
 	$('#list_link li').each(function(){
		height = $(this).height();
		if(height > height_max){
			height_max = height;
		}
 	});
 	
 	$("ul.sf-menu").supersubs({ 
		minWidth:    5,  
		maxWidth:    25, 
		extraWidth:  1    
	}).superfish();  

	$(".block_img_webcam a").fancybox({
		'overlayOpacity':0.8 
	});
	
		
	$("ul.sf-menu li ul.children:last-child").append("<li class='li_without_background'><span class='left_corner'></span><span class='center'></span><span class='right_corner'></span></li>");
	
	$("#block_meteo img").hover(
		function () {
			$("#meteo").fadeIn();
		}, 
		function () {
			$("#meteo").fadeOut();
		}
	);
	
	setTimeout("resize_menu()", 500);

	$('#content_page .block_sub_page:last-child').css("border-bottom","none");
	$('#language_selector ul li:last-child').css("border-right","none");
	$('#content_img a:last-child').css("margin-bottom","0px");
	nb = $('.blog-post').length;
	$('.blog-post').eq(nb-1).css("border-bottom","none");
	
	setTimeout("size_content()", 500);
	
	 $('#list_link li.linkcat').each(function(){
		height = $(this).height(height_max);
 	});

	show_last_news();
	
	show_imgs();

});

function size_content(){
	height_content_img = $("#content_img").height();
	height_content_txt = $("#content_txt").height();
	if(height_content_txt < height_content_img){
		$("#content_txt").height(height_content_img-20);
	}
}

function scookie(name,value,expires){
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";path=/;expires=" + expires_date.toGMTString() : "" );
	if(value != ""){
		document.location = 'http://www.moleson.ch';
	}
}
			
function gcookie(name){
	if(document.cookie.length>0){
		start=document.cookie.indexOf(name+"=");
		pos = start+name.length+1;
		if(start!=0){
			start=document.cookie.indexOf("; "+name+"=");
			pos = start+name.length+3;
		}
		if(start!=-1){ 
			start=pos;
			end=document.cookie.indexOf(";",start);
			if(end==-1){
				end=document.cookie.length;
			}
			return unescape(document.cookie.substring(start,end));
		} 
	}
	return '';
}

function changeCss(season){
 	document.getElementById("style_season").href = "http://www.moleson.ch/wp-content/themes/moleson/"+season+".css";
}

function resize_menu(){
 		$('.children').each(function(){
			width = $(this).width();
			width_center = width-10;
			$(this).children('li.li_without_background').css("width",width);
			$(this).children('li.li_without_background').children("span.center").css("width",width_center);
 		});
}

function show_last_news(){
	jQuery.fn.exists = function(){return jQuery(this).length>0;}
	$('.last_news').eq(0).fadeIn('slow');
	$('.last_news').eq(0).addClass('visibleNews')
	setInterval(function(){
		nextNews = $('.visibleNews').next('.last_news');
		if(!nextNews.exists()){
			nextNews = $('.last_news').eq(0);
		}
		$('.visibleNews').fadeOut('fast', function(){
			$('.visibleNews').removeClass('visibleNews');
			nextNews.fadeIn();
			nextNews.addClass('visibleNews');
		});
	}, 5000);
}

function show_imgs(){
	jQuery.fn.exists = function(){return jQuery(this).length>0;}
	$('.imgs_home').eq(0).fadeIn('slow');
	$('.imgs_home').eq(0).addClass('visibleImg');
	$('.one_img').addClass('visible_one_Img');
	$(".visibleImg a").fancybox({
	titleShow: 'false',
	transitionIn: 'elastic',
	transitionOut: 'elastic',
	speedIn: 1000,
	speedOut: 700,
	overlayOpacity: 0.8,
	overlayColor: '#FFF'
	});
	$(".visible_one_Img a").fancybox({
	titleShow: 'false',
	transitionIn: 'elastic',
	transitionOut: 'elastic',
	speedIn: 1000,
	speedOut: 700,
	overlayOpacity: 0.8,
	overlayColor: '#FFF'
	});
	setInterval(function(){
		nextImg = $('.visibleImg').next('.imgs_home');
		if(!nextImg.exists()){
			nextImg = $('.imgs_home').eq(0);
		}
		$('.visibleImg').fadeOut('fast', function(){
		$('.visibleImg').removeClass('visibleImg');
		nextImg.fadeIn();
		nextImg.addClass('visibleImg');
		$(".visibleImg a").fancybox({
			titleShow: 'false',
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			speedIn: 1000,
			speedOut: 700,
			overlayOpacity: 0.8,
			overlayColor: '#FFF'
			});
		});
		
	}, 5000);
}

