var isIE, prdcat=false, ctheight = 525;
$(document).ready(function(){

	isIE = ($.browser.msie && $.browser.version <9)? true :false;
	atop = (isIE)? "" : {'opacity':0};
	atop1= (isIE)? "" : "'opacity':1";
	
	$(window).trigger('resize');
	if($('.prd-cat-wrap').length >0 ){
		prdcat = true;
	}else{
		$('.maincontent').addClass('webkit');
	}
	// transform
	$('a.transform').click(function(e){
		e.preventDefault();
		lk = this.href;
		if(isIE){ $('.container').supersleight({shim:ieimg});}
		$('.wh1').css('display','none');
		$('.container').fadeOut(600,redirectPage);
	});
	function redirectPage() {
		window.location = lk;
	}
	if(isIE){ $('.sl-dis').supersleight({shim:ieimg}).css('filter','Alpha(Opacity=30)');}
});
$(window).load(function(){
	$(window).trigger('resize');
	//isload();
	$('.wh1').css('display','block');
	
	
	$('video').VideoJS();
	$('.newslst-wrap').easySlider();
	$('.prd-cat-sl').easySlider({atCustom:3});
	$('.slider-ct').easySlider({atCustom:3});
	$('.vdp-nav').easySlider();
	$('.part-nav').easySlider({});
	$('.ab-ct-slider').easySlider({firstId:'ab-ct-slider'});
	
	if($('.prd-cat-sl').length >0){
		$('#nextBtn, #prevBtn').css({'top':ctheight -175});
	}
	
	$('.partner-page-nav span').first().addClass("current").show();
	$('.partner-page-nav span').click(function() {
		$('.partner-page-nav span').removeClass("current"); 
		$(this).addClass("current");
		$('.tabs-hide').hide();
		var activeTab = $(this).find("a").attr("href"); 
		$(activeTab).fadeIn(300); 
		$('.scr-content').tinyscrollbar();
		return false;
	});
	
	$('.tabs-hide').first().show();
	$('ul.tabs-link li:first').addClass("current").show();
	$('ul.tabs-link li').click(function() {
		$('ul.tabs-link li').removeClass("current"); 
		$(this).addClass("current");
		$('.tabs-hide').hide();
		var activeTab = $(this).find("a").attr("href"); 
		$(activeTab).fadeIn(300); 
		$('.scr-content').tinyscrollbar();
		return false;
	});
	
	//product slider
	var ani=ani1="", tipitem ="", isprd=false;
	
	if($('.prd-slider').length >0){
		//prdSlider();
		$('.prd-slider').easySlider({atCustom:3});
		tipitem = $('.prd-slider li a');
		isprd = true;
		attip(tipitem);
	}
	function attip(tipitem){
		var itemtop = 0;
		tipitem.mouseenter(function(e) {
			var itemleft;
			if(isprd){
				itemleft = $(this).offset().left - $(this).parent().parent().parent().offset().left ;
			}else{
				itemleft = $(this).offset().left - $(this).parent().offset().left ;
			}
			var itemw = parseInt($(this).css('width').replace("px","")) + parseInt($(this).css('marginRight').replace("px","")) + parseInt($(this).css('marginLeft').replace("px",""));
			
			itemleft = itemleft + (itemw-245)/2 ;
			
			itemtop =  $(this).children(".tooltip").css('top');
			itemtop = parseInt(itemtop.replace("px",""));// + ($(this).offset().top - $(this).parent().offset().top) ;
			//var itemtop =  parseInt($(this).children(".tooltip").css('top');
			ani1= (isIE)? {'display':'block', 'left':itemleft} : {'display':'block', 'opacity':'0', 'left':itemleft};
			ani = (isIE)? {'top': (itemtop-10) +'px'} : {'opacity':1, 'top':(itemtop-10)+'px'};
			
			$(this).children(".tooltip").css(ani1).stop(true,true).animate(ani,400);
		}).mouseleave(function() {
			ani = (isIE)? {'top':(itemtop-20) +'px'} : {'opacity':0, 'top':(itemtop-20) +'px'};
			$(this).children(".tooltip").stop(true,true).animate(ani,200,'linear', function(){$(this).css({'display':'none','top': itemtop +'px'}); });
		});
	}
	
	// bottom logo
	if($('.lst-logo').length > 0){
		
		$('.lst-logo').supersleight({shim:ieimg});
		var wlogo = $('.lst-logo').width();
		
		var ttlogo = $('.lst-logo').children().length;
		$('.logo-wrap').append($('.lst-logo').clone().addClass('lst-logo1'))
		$('.logo-wrap').width(wlogo*2+20);
		
		logoplay();
		$('.logoitem').hover(function() {
			$('.logo-wrap').stop();
			$(this).children().stop().fadeTo('slow', 0.5 );
		} , function() { 
			$(this).children().stop().fadeTo('fast', 1 );
			logoplay();
		});
		
	}
	function logoplay(){
		//console.log(ttlogo);
		$('.logo-wrap').animate({'left':-1*wlogo},2000*ttlogo,'linear',function(){logoaj();});
		
	}
	function logoaj(){
		$('.logo-wrap').css('left',0);
		logoplay();
	}
	
	
	$('.login-box').click(function(){
		if($('.forgotpass').css('display') == 'none'){
			$('#login-box').toggle(200);
		}else{
			$('.fg-close').trigger('click');
		}
		
		return false;
	});
	
	$('.fogotpass').click(function(){
		$('#login-box').fadeOut(300);
		$('.forgotpass').fadeIn(300);
		return false;
	});
	$('.fg-close').click(function() {
		$('.forgotpass').fadeOut(200);
		return false;
	});
	
	//partner slider
	if($('.partner-slider').length >0){
		$('.partner-slider').easySlider();
		var tipitem = $('.partner-holder a');
		attip(tipitem);
	}
	if($('.brand-slider').length >0){
		
		$('.brand-slider').easySlider();
		tipitem = $('.brand-slider li a');
		attip(tipitem);
		
	}

	var txtholder=['Email','Tìm kiếm','User name','Password'];
	txtholder.join(" ");
	var txtRep ="";
	$('input').focus(function(){
		txtRep = $(this).val();
		if(Array.indexOf){
			if(txtholder.indexOf(txtRep)>=0){
				$(this).val("");
			}
		}else{
			$(this).val("");
		}
	});
	$('input').focusout(function(){
		if($(this).val() == "") $(this).val(txtRep);
	});
	if($.browser.msie){ $('.vjs-poster').hide()}
	//newsletter
	$('.nl-btn').click(function(){$(this).trigger('mouseenter');});
	$('.nl-btn').mouseenter(function(){opennl();}).mouseleave(function(){hidenl()});
	var attimer1;
	function opennl(){
		if($('#newsletter').css('display') == 'none'){
			ani = (isIE)? {'top':'-60px'} : {'top':'-60px','opacity':1};
			$('#newsletter').stop().css('display','block').animate(ani,'slow');
		}
	}
	function hidenl(){
		ani = (isIE)? {'top':'-80px'}:{'top':'-80px','opacity':0};
		$('#newsletter').stop().animate(ani,'fast','linear',function(){$(this).css({'display':'none','top':'-50px'});});
	}
	
		
	//footer
	
	$('.weblink').mouseenter(function(){
		ani = (isIE)? {'top':'-140px'} : {'top':'-140px','opacity':1};
		$('.ft-link ul').stop().css('display','block').animate( ani,'slow');
	}).mouseleave(function(){
		ani = (isIE)? {'top':'-150px'} : {'top':'-150px','opacity':0};
		$('.ft-link ul').stop().animate(ani,'fast','linear',function(){$('.ft-link ul').css({'display':'none','top':'-130px'});});
	});
	$('.videobtn').click(function(){
		$('#video2').css({'left':-2000, 'display':'block'})
		$('#video2').animate({'left':'0px'},'slow','swing');
		return false;
	});
	
	//product video
	$('.prd-video').click(function(){
		$('#video').css({'left':-2000, 'display':'block'})
		$('#video').animate({'left':'0px'},'slow','swing');
		return false;
	});
	$('.video-close').click(function(){
		$('video').trigger('click');
		$(this).parent().parent().animate({'left':'-2000px'},'normal','swing',function(){ $(this).css('display','none')});
		return false;
	});
	
	
	//content 
	$('.scr-content').tinyscrollbar();
	
	if( $('.prod-gallery').length >0){
		prdGallery();
	};
	
	
});
$(window).unload(function(){ }); 
$(window).resize(function(){
	var wh = $(window).height();
	var ch = $('.container').height();
	//var ch = 700;
	if( wh>= ch){
		var sh = (wh - ch)/2;
		$('.wh').css({'height':sh});
	}else{
		$('.wh').css('height',0)
	}
	
})

////////////////////////
function isload(){
	if( prdcat){
		if(ctheight < $('.maincontent .wrap').height()){
			//console.log(ctheight);
			ctheight = $('.maincontent .wrap').height();
			$('.maincontent').animate({'height':ctheight},300,'linear',function(){
				$(window).trigger('resize');
			});
		}
		
	};

	
	if($('#flashcontent').length >0){
		homeslider();
	}

	$.preloadCssImages();

}

//////////////////////////product gallery////////////////////
function prdGallery(){
	var crrpd =0;
	var thumwidth = 110;
	var isplay = false;
	var sumli = $('.thumb-nav').children().length;
	$('.thumb-nav').css("width",sumli*thumwidth);
	if ( sumli>3){
		$('.prod-gallery-wrap').append('<a href="javascript:;" id="thumb-left" class="btn">Left</a><a href="javascript:;" id="thumb-right"  class="btn">Right</a>');
	}
	//$('.prod-gallery-wrap').append('<div class="thumb-bg"></div>');
	
	$('#thumb-right').click(function(){
		thumplay("next",true);
		return false;
	});
	$('#thumb-left').click(function(){
		thumplay("prev",true);
		return false;
	});
	$('.thumb-nav li a').click(function(){
		var newsrc = $(this).attr("href");
		$('.img-main img').attr('src',newsrc);
		return false;
	});
	checkctrl();
	function checkctrl(){
		$('#thumb-left').show();
		$('#thumb-right').show();
		if(crrpd<=0){
			$('#thumb-left').hide();
		}
		if(crrpd == (sumli-3)){
			$('#thumb-right').hide();
		}
		isplay = false;
	}
	function thumplay(dir,cl){
		if(!isplay){
			isplay = true;
			switch(dir){
				case "next":
					crrpd++;
					break; 
				case "prev":
					crrpd--;
					break; 
				default:
					crrpd = dir;
					break; 
			};
	
			var speed = 600;
			offset = crrpd*thumwidth*-1;
			$('.thumb-nav').animate({marginLeft: offset},{ queue:false, duration:speed, complete:checkctrl,easing:'swing' });
		}
	}
}

//////////////////////////home slider ////////////////////
function homeslider(){
	var crr = 0, speed = 7000, dur = 1000, total = 0, hItem, crrItem, limg='', pimg='', ww, ielimg = new Array() , iepimg = new Array(), iecontent = new Array();
	hItem = $('.hsl-item');
	total = hItem.length ;
	$('.hsl-item').first().addClass('active');
	init();
	if(!isIE) setTimeout(hplay,500);
	function init(){
		if(isIE){
			var iecount = 0;
			ww = $(window).width();
			if(ww >=1220){
				$('.ie-fix').css({'width':'1200px','margin':'0 auto'});
			}
			if((ww >960)&& (ww <1220) ){
				$('.ie-fix').css({'width':ww})
			}
			if(ww <=960){
				$('.ie-fix').css({'width':960})
			}
			
			//$('.ie-wrap').append('<div class="wrap"></div>');
			$('.hsl-item').each(function(index, element) {
				var crritem = $('<div class="hitem-ie hitem' + index +'"></div>').appendTo('.ie-fix');
					crritem.css({'background-image':'url(' + $(this).find('.limg').attr('src') + ')'});
				var crrimg = $('<div class="hitemimg hitemimg' + index +'"></div>').appendTo('.ie-wrap .wrap');
					crrimg.css({'background-image':'url(' + $(this).find('.pimg').attr('src') + ')'});
				var crrcontent = $('<div class="hitemcontent hitemcontent' + index +'"></div>').appendTo('.ie-wrap .wrap');
					crrcontent.html($(this).find('.hsl-content').html());
			});
			$('.hitem-ie, .hitemimg').supersleight({shim:ieimg});
			
		}
	}
	function hplay(){
		//$('.home-slholder').css('opacity','0');
		limg = $('.home-slholder .active img.limg').attr('src');
		pimg = $('.home-slholder .active img.pimg').attr('src');
		crrItem = $('.home-slholder').children('.active');
		
		if(isIE){
			//limg = ielimg[crr];
			$('.hitem' + crr ).fadeIn(dur,'linear',showProImg);
			//$('.home-slholder').supersleight({shim:ieimg});
			
			//$('.pcontent-ie').height(0)
		}else{
			$('.home-slholder').css({'background-image':'url(' + limg + ')','opacity':0}).fadeTo(dur,1,'linear',showProImg);
		}
	}
	function showProImg(){
		if(isIE){
			$('.hitemimg'+crr).fadeIn(dur,'linear',showDesc);
			//$('.pimg-ie').supersleight({shim:ieimg});
		}else{
			crrItem.css({'background-image':'url(' + pimg + ')','opacity':0}).fadeTo(dur,1,'linear',showDesc);
		}
	}
	function showDesc(){
		if(isIE){
			$('.hitemcontent' + crr).fadeIn(dur/2);
			//$('.pcontent-ie').supersleight({shim:ieimg});
		}else{
			$('.hsl-content').css({'display':'block'}).animate({'height':400,'opacity':1},dur);
		}
		setTimeout(hideDesc,speed+dur);
	}
	function hideDesc(){
		if(isIE){
			//$('.pcontent-ie').supersleight({shim:ieimg});
			$('.hitemcontent').fadeOut(300);
			$('.hitemimg'+crr).delay(dur/2).fadeOut(dur);
			$('.hitem' + crr).delay(dur).fadeOut(dur,'linear',nextSlider);
		}else{
			$('.hsl-content').fadeOut(dur,0);
			crrItem.delay(dur/2).fadeOut(dur,0);
			$('.home-slholder').delay(dur).fadeTo(dur,0,'linear',nextSlider);
		}
		
	}
	function nextSlider(){
		$('.hsl-item').removeClass('active').css('display','none');
		crr++;
		if(crr == total){
			$('.hsl-item').first().addClass('active');
			crr =0;
		}else{
			crrItem.next().addClass('active');
		}
		hplay();
	}
	
}
















