var isIE, isiPad , loaded=false, ua = navigator.userAgent;

$(document).ready(function(){
	isIE = ($.browser.msie) ? true : false;
	isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
	if($('#intrologo').length){
		var introw = ($(window).width() - 956) /2;
		$('#intro-btnwrap').css('margin-right',introw * -1 )
		$('#intrologo, #intro-name, #intro-btnwrap a').fadeOut(0);
		
	}
	
	$('.main-nav ul li').each(function(index, element) {
		if($(this).children('div').length >0){ 
			$(this).children(':first').addClass('hasChild')
		}
	});
	$('a.hasChild').click(function(){
		$(this).parent().trigger('hover');
		return false;
	});
	//BACKGROUND
	if($('#atbackground').length){
			var bgeffect = $('#atbackground').attr('data-eff');
			$("#atbackground").atbackground({inpage:bgeffect});
			$.fn.atbackground.slideplay();
		
		
	}
	
	$('.main-nav ul li').mouseenter(function () {
		$(this).children('div').stop(true, true).animate({height: ['toggle', 'swing'],opacity: 'toggle'},500)
	}).mouseleave(function () {
			$(this).children('div').stop(true, true).animate({height: ['toggle', 'swing'],opacity: 'toggle'},300)
		});   

	if($('.hproducts').length){
		homeslide()
	}
	$('.abimg-slider').easySlider();
	$('.achi-sl').easySlider({showSlides:2})
	adjustHeight();
	
	//$('.prddt-slider').easySlider();
	$('.prjdt-imgsl').easySlider();
	$('.partner-slider').easySlider();
	
	if($('.news-nav-sl').length){
		$('.news-nav-sl').easySlider();
		var crrn = $('.news-nav-sl li a.current').parent().prevAll().length;
		for(i=0;i<crrn;i++){
			setTimeout(newscl,i*800)
		}
	}
	function newscl(){
		$('#nextBtn a').trigger('click')
	}
	
	$('.news-imgs').easySlider({prevId:'previmg', nextId:'nextimg'});
	
		$(".jbgallery").jbgallery({caption: true, style: 'zoom', menu: false, slideshow: true, timers: {fade: 500, interval: 6000} });	
	
	$('.photo-slider').easySlider();
	$('.doc-slider').easySlider({prevId:'previmg', nextId:'nextimg'});
	
	if($('.photo-page').length){
		$('.phto-tabs').hide()
		$('.phto-tabs').first().fadeIn(300);
		$('.photo-pagename h3').click(function(){
			$('.photo-pagename h3').removeClass('current');
			$(this).addClass('current');
			var pos = $(this).prevAll().length;
			if( pos == 1){
				$('.phto-tabs').css('display','none');
				$('.phto-tabs').last().fadeIn(300);
			}else{
				$('.phto-tabs').css('display','none');
				$('.phto-tabs').first().fadeIn(300);
			}
		});
	}
})
	
$(window).load(function(){
	
	if($('#intrologo').length){
		$('#intrologo').delay(1000).fadeIn('slow','linear',showinname);
	}
	function showinname(){
		$('#intro-name').fadeIn(300,'linear',showinbtn);
	}
	function showinbtn(){
		$('#intro-btnwrap a').fadeIn(400);
	}
	
	$('.con-frmbtn').click(function(){
		$('#confrm').fadeIn(800);
		return false;
	});
	$('.confrm-close').click(function(){
		$('#confrm').fadeOut(400);
		return false;
	});
	$('.faqbox-close').click( function(){ $('.faq-ask-btn').trigger('click'); return false}  );
	$('.faq-ask-btn').click(function(){
		if($('.faq-box').css('display') == 'none'){
			$('.faq-box').fadeIn(600);
		}else{
			$('.faq-box').fadeOut(300);
		}
		return false;
	});
	

	
	if($('.ggmap').length >0 ){
		initialize();
	}
	
	var txtholder=['Search','Công ty','Điện thoại','Email','Địa chỉ','Họ & tên','Company name','Phone','Address','Name'];
	txtholder.join(" ");
	var txtRep ="";
	$('input').focus(function(){
		txtRep = $(this).val();
		if(txtholder.indexOf(txtRep)>=0){
			$(this).val("");
		}
	});
	$('input').focusout(function(){
		if($(this).val() == "") $(this).val(txtRep);
	});
	
	if($('#prjSlider').length ){
		//var dur = $('#prjSlider li').length *3000;
	  	$('#prjSlider').carouFredSel({
			circular:false,
	  		prev: {button:'#prev2',items:1},
			next: {button:'#next2',items:1},
			pagination: "#pager2",
			auto: {easing: "linear",duration: 500,items:1,pauseDuration:2500}
	  	});
	}
	if($('#ftlogo').length ){
	  	$('#ftlogo').carouFredSel({
			auto:false,
	  		prev: {button:'#prev1',items:1},
			next: {button:'#next1',items:1},
	  	});
	}
	
	 
	
	//product slider
	var ani=ani1="";
	var tipitem =""
	if($('.prd-slider').length >0){
		$('.prd-slider').easySlider({showSlides:3})
		function onOverx(){
			var desch = $(this).find('.prdit-desc').height() - 14;
			desch = (desch > 50)? 50 : desch;
			$(this).find('.prdit-desc').css({'display':'block','z-index':10}).animate({'top': -desch},500);
			$(this).find('.prdit-name').animate({'top': -(desch +30)},500);
			$(this).find('.prdit-img').css('opacity',0.5);
		}
		function onOutx(){
			$(this).find('.prdit-desc').animate({'top': 0},500,'linear',function(){ $(this).css({'display':'none','z-index':-1})});
			$(this).find('.prdit-name').animate({'top': 0},500);
			$(this).find('.prdit-img').delay(500).css('opacity',1);
		}
		var xconfig = {
			over:onOverx,
			timeout: 100,
			out:onOutx
		};
		$('.prd-item').hoverIntent(xconfig)
		/*$('.prd-item').mouseenter(function(){
			var desch = $(this).find('.prdit-desc').height() - 14;
			$(this).find('.prdit-desc').css('display','block').animate({'top': -desch},500);
			$(this).find('.prdit-name').animate({'top': -(desch +30)},500);
			$(this).find('.prdit-img').css('opacity',0.5);
		}).mouseleave(function(){
			$(this).find('.prdit-desc').animate({'top': 0},500,'linear',function(){ $(this).css('display','none')});
			$(this).find('.prdit-name').animate({'top': 0},500);
			$(this).find('.prdit-img').delay(500).css('opacity',1);
		})*/
	}
	function attip(tipitem){
		var itemtop = 0;
		tipitem.mouseenter(function(e) {
			//var 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 ;
			//console.log($(this).offset().top - $(this).parent().offset().top);
			itemtop =  $(this).children(".tooltip").css('top');
			itemtop = parseInt(itemtop.replace("px","")) ;// + ($(this).offset().top - $(this).parent().offset().top) ;
			//if(itemtop < 0){ itemtop = 8}
			//console.log(itemtop);
			//var itemtop =  parseInt($(this).children(".tooltip").css('top');
			ani1= {'display':'block', 'opacity':'0', 'top': itemtop +10};
			ani = {'opacity':1, 'top':(itemtop)+'px'};
			
			$(this).children(".tooltip").css(ani1).stop(true,true).animate(ani,400);
		}).mouseleave(function() {
			ani ={'opacity':0, 'top':(itemtop-10) +'px'} ;
			$(this).children(".tooltip").stop(true,true).animate(ani,200,'linear', function(){$(this).css({'display':'none','top': itemtop +'px'}); });
		});
	}
	
	//tab
	$('.tabs-contents .tabs-content').hide();
	
	$('.tabs-nav li').click(function() {
		$('.tabs-nav li').removeClass("current"); 
		$(this).addClass("current");
		$('.tabs-content').hide();
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn(300,'linear',function(){adjustHeight()}); 
		return false;
	});
	
	if($('.tabs-nav li').hasClass('current')){
		$('.tabs-nav li.current').trigger('click');
	}else{
		$('.tabs-contents .tabs-content:first-child').show();
		$('.tabs-nav li:first-child').addClass("current");
	}
	
	$('.fancyitem').fancybox();
	
	
	
	if(isIE){
		$('.tooltip').supersleight();
		//$('.nivo-caption').supersleight();
		
	}
	 $.preloadCssImages();
	 adjustHeight();
	 
});
$(window).resize(function(){
	adjustHeight();
});
function adjustHeight(){
	var wdwidth = $(window).width();
	var wdheight = $(window).height();
	//console.log(wdheight);
	if($('#intrologo').length){
		var introw = (wdwidth - 956) /2;
		$('#intro-btnwrap').css('margin-right',introw * -1 )
	}
	if($('.hnew-wrap').length){
		$('.hnew-wrap').width(656 + (wdwidth - 956)/2);
	}
	if($('.ab-introbg').length){
		if(wdheight < ($('.ab-introbg').height() + 283)){
			$('.ab-introbg').css('height','100%');
		}else{
			$('.ab-introbg').height(wdheight - 284);
		}
	}
	if($('.prd-slwrap').length){
		if(wdheight > 670){
			$('.prd-slwrap').height((wdheight -670) + 326);
		}else{
			$('.prd-slwrap').height(326);
		}
	}
	if($('.prjdt-content').length){
		if( wdheight > $('.prjdt-content').height() + 344){
			$('.prjdt-content').css('height', wdheight - 345);
		
		}else{
			$('.prjdt-content').css('height','100%');
		}
		
	}
	
	if($('.part-slwrap').length){
		if( wdheight > ($('.part-slwrap').height() + 344)){
			$('.part-slwrap').height( wdheight - 345)
		}else{
			$('.part-slwrap').css('height','100%');
		}
	}
	if($('.photo-slwrap').length){
		if( wdheight > ($('.photo-slwrap').height() + 344)){
			$('.photo-slwrap').height( wdheight - 345)
		}else{
			$('.photo-slwrap').css('height','100%');
		}
	}
	if($('.news-colright').length){
		if( wdheight > $('.news-colright').height() + 304){
			$('.news-colright').css('height', wdheight - 305);
		}else{
			$('.news-colright').css('height','100%');
		}
		//console.log(wdheight + '   ' + $('.news-colright').height());
	}
	if($('.contact-colright').length){
		if( wdheight > ($('.contact-colright').height() + 306)){
			$('.contact-colright').height(wdheight - 307)
		}else{
			$('.contact-colright').css('height','100%');
		}
		//$('.contact-colright').height(370)
		//console.log(wdheight + '  ' + $('.contact-colright').height());
	}
	if($('.customer-colright').length){
		
		if( wdheight > (685 )){
			$('.customer-colright').height(wdheight - 310)
		}else{
			$('.customer-colright').css('height','100%');
		}
	}
	if($('.career-colright').length){
		//console.log(wdheight + '  ' + $('.career-colright').height());
		if( wdheight > ($('.career-colright').height() + 304 )){
			$('.career-colright').height(wdheight - 305)
		}else{
			$('.career-colright').css('height','100%');
		}
	}
	if($('.ser-page').length){
		if( wdheight > ($('.ser-page').height() + 258)){
			if(!isiPad) $('.ser-page').css('height',(wdheight - 259));
		}else{
			$('.ser-page').css('height','100%');
		}
		//console.log($('.ser-page').height() + '   ' + $('section').height());
		
	}
	
	if($('.prddt-wrap').length){
		//console.log(wdheight + '  ' + $('.prddt-wrap').height() + '  ' + $('#dactinh').height() + '  ' + $('#thongso').height() );
		
		//if( wdheight > ($('.prddt-wrap').height() + 344)){
		if( wdheight > ($('.prddt-wrap .wrap').height() + 344)){
			if(!isiPad) $('.prddt-wrap').height(wdheight - 345)
		}else{
			$('.prddt-wrap').css('height','100%');
		}
	}
	if($('#home-wrap').length){
		//console.log(wdheight + '  ' + $('#home-wrap').height());
		if( wdheight > ($('#home-wrap').height() + 217)){
			$('#home-wrap').height(wdheight - 218)
			
		}else{
			$('#home-wrap').css('height','100%');
			
		}
	}
	
	
}
function homeslide(){
	var crrit  = 0, crrplay = 0, slLeng = $('.hprd-item').length;
	var sleff = 'easeOutExpo';
	
	var bezier_params =  {start: { x: 50, y: 250, angle: -100, length:0.3 },   end: {x:735,y:-20,angle: 30, length: 0.5 }};
	var bezier_params1 = {start: { x: 50, y: 250, angle: -100, length:0.3 },   end: {x:536,y:-7 ,angle: 30, length: 0.5 }};
	var bezier_params2 = {start: { x: 50, y: 250, angle: -100, length:0.3 },   end: {x:345,y:10 ,angle: 30, length: 0.5 }};
	var bezier_params3 = {start: { x: 50, y: 250, angle: -100, length:0.3 },   end: {x:160,y:90 ,angle: 190, length: 0.5 }};
	
	var bezier_next = { start: { x: 536, y: -7 },   end: {x:735,y:-20 }};
	var bezier_next1 = {start: { x: 345, y: 10 },   end: {x:536,y:-7,angle: -50, length: 0.05 }};
	var bezier_next2 = {start: { x: 160, y: 90 },   end: {x:345,y:10,angle: -50, length: 0.05 }};
	var bezier_next3 = {start: { x: 50, y: 250  },   end: {x:160,y:90,angle: 190, length: 0.05 }};
	
	var bezier_prev = { start: { x: 160, y: 90 },   end: {x:50,y:250 }};
	var bezier_prev1 = {start: { x: 345, y: 10 },   end: {x:160,y:90,angle: -50, length: 0.05 }};
	var bezier_prev2 = {start: { x: 536, y: -7 },   end: {x:345,y:10,angle: -50, length: 0.05 }};
	var bezier_prev3 = {start: { x: 735, y: -20  }, end: {x:536,y:-7,angle: 190, length: 0.05 }};
	
	$('.hprd-item').each(function(index, element) {
			//if(index<5){ $(this).addClass('active')}
		$(this).addClass('hitem' + index);
	});
	
	/*function showcrr(){
		$('.hitem'+crrit).delay(300).fadeIn(500,'linear',playcrr);
	}
	function gonext(){
		crrit++;
		if(crrit < 5) showcrr();
	}*/
	function playcrr(){
		
		//var bezier_params4 = {start: { x: 0, y: 400, angle: -100, length:0.7 },   end: {x:735,y:-20,angle: -50, length: 0.05 }};
	  
	
		$('.hitem0').fadeIn(500).animate({path : new $.path.bezier(bezier_params)},1500,sleff);
		$('.hitem1').delay(600).fadeIn(500).animate({path : new $.path.bezier(bezier_params1)},1500,sleff);
		$('.hitem2').delay(1200).fadeIn(500).animate({path : new $.path.bezier(bezier_params2)},1500,sleff);
		$('.hitem3').delay(1800).fadeIn(500).animate({path : new $.path.bezier(bezier_params3)},1500,sleff);
		$('.hitem4').delay(2400).fadeIn(500);
		{if(slLeng>5) $('.sl-next').removeClass('sl-disable');	}
	}
	function playnext(){
		
		$('.hitem'+crrplay).fadeOut(300,'linear',function(){$(this).addClass('itemdis')});
		$('.hitem'+(crrplay+1)).delay(200).animate({path : new $.path.bezier(bezier_next)},800,sleff);
		$('.hitem'+(crrplay+2)).delay(400).animate({path : new $.path.bezier(bezier_next1)},800,sleff);
		$('.hitem'+(crrplay+3)).delay(600).animate({path : new $.path.bezier(bezier_next2)},800,sleff);
		$('.hitem'+(crrplay+4)).delay(800).animate({path : new $.path.bezier(bezier_next3)},800,sleff);
		$('.hitem'+(crrplay+5)).delay(1000).fadeIn(300);
		crrplay++;
		//console.log(crrplay);
		
		if(slLeng == crrplay + 5){	$('.sl-next').addClass('sl-disable');}
	}
	function playprev(){
		crrplay--;
		$('.hitem'+(crrplay+5)).fadeOut(300);
		$('.hitem'+(crrplay+4)).delay(200).animate({path : new $.path.bezier(bezier_prev)},800,sleff);
		$('.hitem'+(crrplay+3)).delay(400).animate({path : new $.path.bezier(bezier_prev1)},800,sleff);
		$('.hitem'+(crrplay+2)).delay(600).animate({path : new $.path.bezier(bezier_prev2)},800,sleff);
		$('.hitem'+(crrplay+1)).delay(800).animate({path : new $.path.bezier(bezier_prev3)},800,sleff);
		$('.hitem'+(crrplay)).delay(1000).fadeIn(300);
		
		//console.log(crrplay);
		
		if(crrplay==0){	$('.sl-prev').addClass('sl-disable');}
	}
	
	/*function hslinit(){
		playcrr();
		//console.log(slLeng);
	}*/
	playcrr();
	$('.hprd-item').mouseenter(function(e){ 
			//move_thumb($(this),e);
			$(this).children('.htip').stop().fadeIn(600);
		}).mouseleave(function(e){
			//move_thumb($(this),e);
			$(this).children('.htip').stop(true,true).fadeOut(100);
		});
		
	$('.sl-next').click(function(){
		if(slLeng > (crrplay + 5)){
			playnext();
			$('.sl-prev').removeClass('sl-disable')
		}
		return false;
	})
	$('.sl-prev').click(function(){
		if(crrplay >0){
			playprev();
			$('.sl-next').removeClass('sl-disable')
		}
		return false;
	})
	$('#home-wrap').touchwipe({
		 wipeLeft: function() {$('.sl-prev').trigger('click'); },
		 wipeRight: function() { $('.sl-next').trigger('click'); },
		 min_move_x: 20,
		 min_move_y: 20,
		 preventDefaultEvents: false
	});
}
