
var activity = 0;
var promo = 0;
var promo_next = 0;

function load_promo(id){
	if (id!=promo && activity == 0){
		activity = 1;
		$('.navl a').removeClass('sel');
		$('#promo_'+id).addClass('sel');
		$('#promo_box2').load('include/promo.inc.php',{promo:id},function(data){
			$('#promo').animate({'left':'-789px'},function(){
				$('#promo_box1').remove();	
				$('#promo').css({'left':'0px'});
				$('#promo_box2').attr('id','promo_box1');
				$('#promo').append('<div id="promo_box2">&nbsp;</div>');
				promo = id;
				activity = 0;
				start_autoplay();
			});
		});
		
	}
}

function load_press(id,type){
	$('#press_detail').load('include/press_detail.inc.php',{press:id},function(){
		if (type==1){
			$('#press_box2').load('include/press.inc.php',{press:id},function(){
				$('#press_box_holder').animate({'top':'-482px'},function(){
					$('#press_box1').remove();	
					$('#press_box_holder').css({'top':'0px'});
					$('#press_box2').attr('id','press_box1');
					$('#press_box_holder').append('<div id="press_box2">&nbsp;</div>');
					var prev_next = $('#press_box1 div.press_box').attr('id').replace('press_','').split('_');
					var prev = prev_next[0];
					var next = prev_next[1];
					$('#press .nav_top a').attr('id','press_'+prev);
					$('#press .nav_bottom a').attr('id','press_'+next);
					activity = 0;
				});
			});
		}
		else{
			$('#press_box2').remove();
			$('#press_box_holder').css({'top':'-482px'});
			$('#press_box_holder').prepend('<div id="press_box2">&nbsp;</div>');
			$('#press_box2').load('include/press.inc.php',{press:id},function(){
				$('#press_box_holder').animate({'top':'0px'},function(){
					$('#press_box1').remove();	
					$('#press_box2').attr('id','press_box1');
					$('#press_box_holder').append('<div id="press_box2">&nbsp;</div>');
					var prev_next = $('#press_box1 div.press_box').attr('id').replace('press_','').split('_');
					var prev = prev_next[0];
					var next = prev_next[1];
					$('#press .nav_top a').attr('id','press_'+prev);
					$('#press .nav_bottom a').attr('id','press_'+next);
					activity = 0;
				});
			});
		}
	});
}


function start_autoplay(){
	stop_autoplay();
	if ($('#promo_'+promo).parent().next().length>0){
		promo_next = $('#promo_'+promo).parent().next().children('a').attr('id').replace('promo_','');
	}
	else{
		promo_next = $('.navl ul li:first a').attr('id').replace('promo_','');
	}
	timerID = setInterval("load_promo("+promo_next+");",10000);
}

function stop_autoplay(){
	if(typeof(timerID) !== 'undefined'){
		clearInterval(timerID);
	}
}


$(document).ready(function(){

	if ($('.navl a.sel').length>0){
		promo = $('.navl a.sel').attr('id').replace('promo_','');
	}

	if ($('#promo').length>0){
		var pcount = $('.navl ul li').size();
		if (pcount>1){
			if ($('#promo_'+promo).parent().next().length>0){
				promo_next = $('#promo_'+promo).parent().next().children('a').attr('id').replace('promo_','');
			}
			else{
				promo_next = $('.navl ul li:first a').attr('id').replace('promo_','');
			}
			start_autoplay();		
			$('#home').live('mouseover',function(){
				stop_autoplay();
			});
	
			$('#home').live('mouseout',function(){
				start_autoplay();
			});
		}
	}

	$('#press .nav a').live('click',function(){
		if (activity==0){
			activity = 1;
			var my_id = $(this).attr('id').replace('press_','');
			var type = 0;
			if ($(this).parent().hasClass('nav_bottom')){
				type = 1;
			}
			if ($('#press_detail').is(':visible')){
				$('#press_detail').slideUp(function(){
					load_press(my_id,type);				
				});
			}
			else{
				load_press(my_id,type);
			}
		}
		return false;
	});

	$('#press p.more a').live('click',function(){
		if (!$('#press_detail').is(':visible') && activity==0){
			activity = 1;
			$('#press_detail').slideDown(function(){
				activity = 0;
			});
		}
		return false;
	});


	$('.navl a').live('click',function(){
		var my_id = $(this).attr('id').replace('promo_','');
		load_promo(my_id);
		return false;
	});

	$('li.wsm a:first').live('click',function(){
		if (!$(this).parent().hasClass('sm_sel')){
			var elem = $(this).parent().children('ul.sm');
			if (elem.is(':hidden')){
				elem.slideDown();
			}
			else{
				elem.slideUp();		
			}
		}
		return false;
	});
	
	$('#nav_more').live('click',function(){
		var hh = $('#product_holder').height();
		var elements = $('#product_holder ul li').size();
		var rows = Math.ceil(elements/4);
		var visible = Math.ceil(hh/123);
		var next = (rows-visible>4?4:rows-visible);
		var height = (next>0?next*123:0);
		$('#product_holder').animate({'height':'+='+height+'px'},function(){
			hh = $('#product_holder').height();
			visible = Math.ceil(hh/123);
			if ((rows-visible)<=0){
				$('#nav_more').parent().fadeOut(function(){
				
				});
			}
		});
		return false;
	});

});
