/*
function listHeight(ul){
	var sum = 0;
	ul.find('li').each(function(){
		sum+=$(this).innerHeight();
	});
	return sum==0?200:sum;;
}
*/
function rollDown(){
	var ul = $(this).find('ul');
	if(ul.length>=1)
		$(ul[0])
			.css('overflow','visible')
			.css('height','auto')
			.stop()
			.animate({opacity: 1.0},300);
	
	var a = $($(this).find('a')[0]);
	if(a.find('img').length>=2)
		$(a.find('img')[1]).stop().animate({opacity:1.0},300);
}
function rollUp(){
	var ul = $(this).find('ul');
	if(ul.length>=1)
		$(ul[0])
			.stop()
			.animate({opacity: 0.0},300,function(){
				$(this)
					.css('overflow','hidden')
					.css('height','0px');
			});
	
	var a = $($(this).find('a')[0]);
	if(a.find('img').length>=2)
		$(a.find('img')[1]).stop().animate({opacity:0.0},300);
}
function _selectEvent(){
	this.select();
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
var panel_is_active = true;
function loadContent(data) {
	var ith = 250;
	var l = $('#border .cnews').length;
	var h = (Math.floor((l-1)/4)+1)*ith;
	$('#border').css('height',''+h+'px');
	if(h==0){
			$('#border').html(data);
			l = $('#border .cnews').length;
			h = (Math.floor((l-1)/4)+1)*ith;
			h = (h==0?200:h);
			$('#border').animate({'height':''+h+'px'},h*2,function(){
				panel_is_active = true;
				c = $('#border .pagination').html();
				$('.pagination').remove();
				$('.content').after('<div class="pagination"></div>');
				$('.pagination').html(c);
			});
	}else{
		$('#border').animate({'height':'0px'},h*2,function(){
			$('#border').html(data);
			l = $('#border .cnews').length;
			h = (Math.floor((l-1)/4)+1)*ith;
			h = (h==0?200:h);
			$('#border').animate({'height':''+h+'px'},h*2,function(){
				panel_is_active = true;
				c = $('#border .pagination').html();
				$('.pagination').remove();
				$('.content').after('<div class="pagination"></div>');
				$('.pagination').html(c);
			});
		});
	}
}
function categoryClick(){
	if(panel_is_active){
		panel_is_active = false;
		$.ajax({
			url: this.href ,
			success: loadContent
		});
	}
	return false;
}

var isLoaded = false;

$(document).ready(function(){
	$('a.category').click(categoryClick);
		
///////////////////////////////////////////////////////
////////////////////////////////////////////////////////
	$('#searching_panel input')[0].onfocus = _selectEvent;
    $($('#searching_panel input')[0]).Watermark($('#searching_panel label')[0].innerHTML);
	$('div#sidebar a').css('cursor','default').click(function(){return false;});
	$('#header .top ul li a').hover(
		function(){if($(this).attr('class')!='active') $(this).stop().animate({color:'#7fcb21'},300);},
		function(){if($(this).attr('class')!='active') $(this).stop().animate({color:'#898989'},300);}
	);
	$('a.detail').hover(
		function(){$(this).stop().animate({color:'#000000'},300);},
		function(){$(this).stop().animate({color:'#8d8d8d'},300);}
	);
	$('.category h4 a').hover(
		function(){$(this).stop().animate({color:'#aa0000'},300);},
		function(){$(this).stop().animate({color:'#267440'},300);}
	);
	$('#footer a').hover(
		function(){$(this).stop().animate({color:'#888888'},300);},
		function(){$(this).stop().animate({color:'#444444'},300);}
	);
	$('#logo > div a').click(function(){
		$('#logo > div').html('<div id="logovideo"></div>');
		jwplayer("logovideo").setup({
			flashplayer: "/video/player.swf",
			file: "/video/naturelle.mp4",
			image: "/images/naturelle_pre.png",
			autostart: "true",
			width: "479px",
			height: "266px"				
		});
	});
	
	$('ul#menu > li').each(function(){
		var li = $(this);
		var a = $(li.find('a')[0]);
		var img = a.find('img');
		if(li.find('ul').length>0)
			img.load(function(){
				isLoaded = true;
				$(li.find('ul')[0]).width( li.innerWidth()-5 );
			});
		if(img.attr('src').indexOf('/active/')==-1){
		
			img.after(
					$('<img src="'+img.attr('src').replace('/passive/','/active/')+'" alt="" />')
						.css('opacity',0.0)[0]
			);			
		}

	});
	$('ul#menu > li > ul a').each(function(){
		var img = $(this).find('img');
		if(img.attr('src').indexOf('/active/')==-1){
			img.load(function(){
				img
					.css('marginRight','-'+img.width()+'px')
					.after(
						$('<img src="'+img.attr('src').replace('/passive/','/active/')+'" alt="" />')
							.css('opacity',0.0)[0]
				);			
			});
		}	
	
	});

	$('ul#menu li').hover(rollDown,rollUp);

	$('ul#menu a').click(function(){
		return this.href.indexOf('#')==-1;
	});


	$('#logo span a').each(function(){
		var img = $(this).find('img');
		if(img.attr('src').indexOf('/active/')==-1){
			$(this).append(
				$('<img src="'+ $(this).find('img').attr('src').replace('passive','active') +'" alt="" />').css('opacity',0.0)
			);
			$(this).hover(galleryLinkUp, galleryLinkDown);
		}
	});
	
/*
	$('a').click(function(){
		if( /#$/.test(this.href) ){
			$(this).css('cursor','default');
			return false;
		}else{
			return true;
		}
	});
*/
});
function galleryLinkUp(){
	var img = $($(this).find('img')[1]);
	img.stop().animate({opacity: 1.0},300);
}
function galleryLinkDown(){
	var img = $($(this).find('img')[1]);
	img.stop().animate({opacity: 0.0},300);
}
window.onload = function(){
	if($('#loader').length > 0){
		$('#loader').css('backgroundImage','none');
		$('#loader').animate({
			opacity: 0.0
		},1000 , function(){$('#loader').css('display','none');});
	}

	if(!isLoaded){
		$('ul#menu > li').each(function(){
			var li = $(this);
			var a = $(li.find('a')[0]);
			var img = a.find('img');
			if(li.find('ul').length>0)
				$(li.find('ul')[0]).width( li.innerWidth()-5 );
			if(img.attr('src').indexOf('/active/')==-1){
				img.after(
					$('<img src="'+img.attr('src').replace('/passive/','/active/')+'" alt="" />')
						.css('opacity',0.0)[0]
				);			
			}
		});
		$('ul#menu > li > ul a').each(function(){
			var img = $(this).find('img');
			if(img.attr('src').indexOf('/active/')==-1){
				img
					.css('marginRight','-'+img.width()+'px')
					.after(
						$('<img src="'+img.attr('src').replace('/passive/','/active/')+'" alt="" />')
							.css('opacity',0.0)[0]
				);			
			}	
		});
	
	}	
/*
	$('ul#menu li a').hover(
		function(){if($(this).find('img').length>=2) $($(this).find('img')[1]).stop().animate({opacity:1.0},300);},
		function(){if($(this).find('img').length>=2) $($(this).find('img')[1]).stop().animate({opacity:0.0},300);}
	);
*/

	var arr = $('.news').css('height','auto');

	for(var i=0; i<arr.length ; i+=3){
		var max = $(arr[i]).height();
		var limit = Math.min(i+3,arr.length);
		for(var j = i+1; j < limit ; j++)
			if($(arr[j]).height()>max)
				max = $(arr[j]).height();
		for(var j = i; j < limit ; j++)
			$(arr[j]).height(max);
	}

};
