/*********************************************/
//Product: LDXH University
//Name: Nguyen Loc Vu.
//Email: locvu79@yahoo.com
//Date: 14/04/2010
/*********************************************/

$(function(){
	YMCom_ImageSlideShow();
	menuLeft();
	menuTop();
	arrangeBoxes();
    setInterval('shiftLeft()', 3000);
})

function menuLeft(){
	$("#main_dept ul").css({display: "none"}); // Opera Fix
	$("#main_dept li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		},function(){
		$(this).find('ul:first').hide(200).css({visibility: "hidden"});
		});
}
function menuTop(){
	$("#maintop ul").css({display: "none"}); // Opera Fix
	$("#maintop li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		$('#wrapper_header').css({'position':'relative','zIndex':1000});
		},function(){
		$(this).find('ul:first').hide(200).css({visibility: "hidden"});
		$('#wrapper_header').css({'position':'','zIndex':''});
		});
}
var flag=true;
var dirLeft=true;
var intTime = 7000;
var intTime2 = 3000;
var intShow = 300;
var pos=1;
var top=10;
function YMCom_ImageSlideShow(){
	$('input[name=pos]').val(1);
	var numImagesLoad=4;
	var numImagesLoaded=1;
	var speedScroll=1000;
	var numPageIndex=1;
	var numItems=$('.img_block').length;
	var numImgBlockWidth=$('.img_block').width();
	var numImgBlockHeight=$('.img_block').height();
	for(var i=0;i<numItems;i++) $('.img_block:eq('+i+')').css("left",(i*numImgBlockWidth));
	$('.content_imglist').width(numImagesLoad * numImgBlockWidth);
	$('.content_imglist').height(numImgBlockHeight);
	if(parseInt($('input[name=pos]').val())==1){
		$('.btn_left').hide(intShow);
		$('.btn_right').show(intShow);									 
	}
	$('.img_block').mouseover(function(){
		$(this).addClass('active');
	}).mouseout(function(){
		$(this).removeClass('active');
	});
	$(".btn_right").click(function(){
							if(parseInt($('input[name=pos]').val())==1)
								$(".btn_left").show(intShow);
							if(parseInt($('input[name=pos]').val())==(parseInt(numItems/numImagesLoaded)-numImagesLoad))
							{
								$(".btn_left").show(intShow);
								$(".btn_right").hide(intShow);
							}
							flag = false;	   
							clearTimeout(g);
							if(flag==false){
								if(parseInt($('input[name=pos]').val())<=(numItems/numImagesLoaded)){
									$(".img_block").animate({left:'-='+numImgBlockWidth*numImagesLoaded+'px'}, speedScroll);
									$('input[name=pos]').val(parseInt($('input[name=pos]').val())+1);
								} 
							}							
							clearTimeout(t);	
					});
	$(".btn_left").click(function(){
							flag = false;
							clearTimeout(g);
							if(flag==false){
								if(parseInt($('input[name=pos]').val())>=1){
									$(".img_block").animate({left:'+='+numImgBlockWidth*numImagesLoaded+'px'}, speedScroll);
									$('input[name=pos]').val(parseInt($('input[name=pos]').val())-1);
								} 
							}
							if(parseInt($('input[name=pos]').val())==1)
								$(".btn_left").hide(intShow);
								$(".btn_right").show(intShow);
							if(parseInt($('input[name=pos]').val())>1)
								$(".btn_right").show(intShow); 
							clearTimeout(t);
					});
	if(flag==true && dirLeft==true){
		pos=$('input[name=pos]').val();
		if(pos<(numItems/numImagesLoaded)){
			g=setTimeout("left("+numImgBlockWidth+","+numImagesLoaded+","+speedScroll+","+(pos++)+","+numItems/numImagesLoaded+","+numImagesLoad+")",intTime);
		}
	}
	else
	{

		clearTimeout(g);	
	}
}
function left(a,b,c,d,e,f){
	if(d>0) $('.btn_left').show(intShow);
	if(d>0&&(d<e-f)){
		$('.btn_right').show(intShow);
	}
	if(d >= parseInt(e)-f){
		$('.btn_right').hide(intShow);	
	}
	else{
		$('.btn_right').show(intShow);	
	}
	if(d<e-f+1){
		$(".img_block").animate({left:'-='+a*b+'px'}, c);
		d++;
		if(d<e && dirLeft==true){
			t=setTimeout("left("+a+","+b+","+c+","+d+","+e+","+f+");",intTime);
		}
		else
		t=setTimeout("right("+a+","+b+","+c+","+d+","+e+","+f+");",intTime);
	}
	else
	{
		dirLeft=false;
		t=setTimeout("right("+a+","+b+","+c+","+d+","+e+","+f+");",intTime);
	}
	$('input[name=pos]').val(d);
}
function right(a,b,c,d,e,f){
	dirLeft=false;
	$(".img_block").animate({left:'+='+a*b+'px'}, c);
	d--;
	if(d>1 && dirLeft==false){
		t=setTimeout("right("+a+","+b+","+c+","+d+","+e+","+f+");",intTime);
	}
	else
	{
		t=setTimeout("left("+a+","+b+","+c+","+d+","+e+","+f+");",intTime);
	}
	//alert(d);
	if(d<=1) 
	{
		dirLeft=true;
		$('.btn_left').hide(intShow);
	}
	else if(d>1)
	{
		//$('.btn_left').show(intShow);
		$('.btn_right').show(intShow);
	}
}
function arrangeBoxes() {
	$('.banner').each( function(i, item) {
		temp = $(item).prev().css('top')?$(item).prev().css('top'):'0px';
        var position = $(item).prev().css('top')?(parseInt(temp.substr(0,temp.length-2))) + 3 + ( $(item).prev().height() + 10 ):10;
        $(item).css('top', position+'px');
    });
}
function shiftLeft() {
    $('.banner').animate({'top' : "-=100px"}, intTime2, 'linear', checkEdge());
}
function getNewPosition() {
    return $('.banner:last').position().top + $('.banner:last').outerHeight() + 10;
}
function checkEdge() {
	var windowsLeftEdge = top;
    $('.banner').each( function(i, box) {
        var boxRightEdge = $(box).position().top + $(box).height();
        var newPosition = getNewPosition();

        if ( parseFloat(boxRightEdge) < parseFloat(windowsLeftEdge) ) { 
            $(box).css('top', newPosition);
            $(box).remove().appendTo('.content_banner');
            first = $('.banner:first').attr('class');
        }
    });
	$('.banner').mouseover(function(event){
		$('.banner').stop();
	})
}