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

$(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);
	
	adjustCt();
	if($('#home-content').length){
		homepage();
	}
	if($('#page-content').length){
		centerpage();
	}
	if($('#newsdt').length){
		newsdtpage();
	}
	if($('#contact-page').length){
		contactpage();
	}
	
	var lstbg = new Array();
	$('#atbground a').each(function(index, element) {
		if( $(this).attr('href') != null ){
			lstbg.push({image :  $(this).attr('href') });
		}
	});
	
	$.supersized({
				
					//autoplay:false,
					slide_interval          :   3000,		// Length between transitions
					transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
					transition_speed		:	1000,		// Speed of transition		// Components							
					//performance				:	2,
					slide_links				:	'blank',	// Individual links for each slide (Options: false, 'num', 'name', 'blank')
					slides 					:  	lstbg	
				});
	
	$('.news-imgs').easySlider();
	$('.content-box').each(function(index, element) {
		if($(this).hasClass('active')){
			$(this).children('.box-mask').addClass('nav-left');
		}else{
			$(this).children('.box-mask').addClass('nav-right');
		}
	});
	
});

$(window).load(function(){
	
	$('.main-nav').click(function(){
		if($(this).hasClass('active')){
			var homelink = $(this).find('.home').children('a').attr('href');
			//console.log(homelink);
			//window.location = homelink;
		}else{
			$(this).trigger('mouseenter');
		}
	});
	$('.main-nav').mouseenter(function(){
			$('.nav-container').fadeIn(300,'linear',function(){$('.main-nav').addClass('active');});
		}).mouseleave(function(){
			$('.nav-container').fadeOut(300,'linear', function(){$('.main-nav').removeClass('active');});
		});
	
	$('.showhide-eml').click(function(){
		if($('#content').css('display') == 'block'){
			$('#content').fadeOut('fast');
		}else{
			$('#content').fadeIn('fast');
		}
		//$('#content').toggle('fast','easeInQuart');
		return false;
	});
	$('.client-item').mouseenter(function(){ $(this).find('.tooltip').fadeIn(300,'linear',function(){$(this).animate({'bottom':'100px'},300) })}).mouseleave(function(){$(this).find('.tooltip').fadeOut(100,'linear',function(){$(this).css('bottom','90px')})});
	
	$('.box-mask').bind('click',function(){
		if($(this).hasClass('nav-left')){
			if(!$('#page-content').hasClass('playing') && !$('.content-box').first().hasClass('active')){
				$('#page-content').addClass('playing');
				crrleft = parseInt( $('#page-content').css('left').replace('px',''));
				newleft = crrleft + 582;
				$('.content-box.active').removeClass('active').prev().addClass('activetem');
				$('#page-content').animate({'left':newleft},1000,'easeOutQuart',function(){
					$('.content-box.activetem').removeClass('activetem').addClass('active').next().children('.box-mask').removeClass('nav-left').addClass('nav-right');
					$('#page-content').removeClass('playing');
				});
			}
		}else{
			if(!$('#page-content').hasClass('playing') && !$('.content-box').last().hasClass('active')){
				$('#page-content').addClass('playing');
				crrleft = parseInt( $('#page-content').css('left').replace('px',''));
				newleft = crrleft - 582;
				$('.content-box.active').removeClass('active').next().addClass('activetem')
				$('#page-content').animate({'left':newleft},1000,'easeOutQuart',function(){
					$('.content-box.activetem').removeClass('activetem').addClass('active').children('.box-mask').removeClass('nav-right').addClass('nav-left');
					$('#page-content').removeClass('playing');
				});
			}
		}
		return false;
	});
	
	var txtholder=['YOUR NAME (*)','YOUR COMPANY','YOUR TITLE','TELEPHONE','E-MAIL (*)'];
	txtholder.join(" ");
	var txtRep ="";
	$('input[type=text]').focus(function(){
		txtRep = $(this).val();
		if(!isIE){
			if(txtholder.indexOf(txtRep)>=0){
				$(this).val("");
			}
		}else{
			$(this).val('')
		};
	});
	$('input').focusout(function(){
		if($(this).val() == "") $(this).val(txtRep);
	});
	$('textarea').focus(function(){
		if($(this).val() == 'MESSAGE (*)' ) $(this).val('');
	});
	$('textarea').focusout(function(){
		if($(this).val() == '' ) $(this).val('MESSAGE (*)');
	});
	
	$('.newsdt-video').click(function(){
		$('#video-wrap').css('left',0);
		var vdl = $('a.videolink').attr('href');
		$('#videocon').html('<iframe width="100%" height="390" src="' + vdl +'" frameborder="0" allowfullscreen></iframe>');
	});
	$('.close-video').click(function(){
		$('#video-wrap').css('left','-100%');
		$('#videocon').html('');
		return false;
	});
	
	//tab
	$('.tabs-contents .tabs-content').hide();
	$('.tabs-contents .tabs-content:first-child').show();
	$('.tabs-nav ul li:first-child').addClass("current");
	$('.tabs-nav ul li').click(function() {
		$(this).parent().children().removeClass("current"); 
		$(this).addClass("current");
		$(this).parent().parent().next().children('.tabs-content').hide();
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn(300); 
		return false;
	});
	
	$('#middle').touchwipe({
	     wipeLeft: function() { if($('.box-mask').length){ $('.box-mask.nav-right').trigger('click');} },
	     wipeRight: function() {  if($('.box-mask').length){ $('.box-mask.nav-left').trigger('click'); }},
	     min_move_x: 100,
	     min_move_y: 100,
	     preventDefaultEvents: false
	});
	
	if(isIE){
		//$('.tooltip').supersleight();
		
	}
	 $.preloadCssImages();
});
$(window).resize(function(){
	adjustCt();
})
function adjustCt(){
	var wh = $(window).height();
	var wdw = $(window).width();
	var topct = (wh-330)*0.5 ;
	//var leftct = (ww-565)*0.5;
	$('#content').css({'top':topct});
	//$('.ctnav-left, .ctnav-right').width((wdw-580)/2);
}
function homepage(){
	var imgholder, lastBlock = $("#fitem"), maxWidth = 460, minWidth = 175;	
	function centerHome(){
		var ww = $(window).width();
		var leftct = (ww-850)*0.5;
		$('#home-content').css('left',leftct);
	}
	centerHome();
	$('.img-slide img:first-child').addClass('crr');
	$(window).bind('resize',centerHome);
	
	function playitem(elm){
		imgholder = elm.find('.img-slide');
		timeOut = setTimeout(playx,1000);
	}
	function playx(){
		crrtop = imgholder.find('img.crr');
		newtop = crrtop.next().is('img')? crrtop.next() : imgholder.children().first();
		crrtop.addClass('last-crr');
		newtop.css({opacity: 0.0}).addClass('crr').animate({opacity: 1.0},600,'linear',function(){
			 crrtop.removeClass('crr last-crr'); 
		})
		
		timeOut = setTimeout(playx,2000);
	}
	
   

    $(".home-item").hover(
      function(){
		 $('.homeacc ul li').addClass('item-alpha');
		 $(this).parent().removeClass('item-alpha');
        $(lastBlock).animate({width: minWidth+"px"},{easing:'easeOutExpo',queue:false, duration:500});
		$(this).animate({width: maxWidth+"px"}, { easing:'easeOutExpo',queue:false, duration:500});
		playitem($(this));
		lastBlock = this;
      },function(){ if(typeof(timeOut) =='number') clearTimeout(timeOut);}
    );
}
function centerpage(){
	var ww,leftct, newleft;
	function centerCt(){
		ww = $(window).width();
		leftct = (ww-580)*0.5;
		$('#page-content').css('left',leftct);
		$('.page-name').css('left',leftct);
		//console.log($('#page-content').offset().left+ '  ' + $('#page-content .active').offset().left + '  ' + leftct);
	}
	centerCt();
	function centerResize(){
		
		newleft = ( $(window).width() -580)*0.5;
		crrleft = ($('#page-content .active').offset()==null)? newleft : $('#page-content .active').offset().left  ;
		allleft = $('#page-content').offset().left ;
		//console.log(leftct + '  ' + crrleft );
		if(newleft > leftct){
			newpos = newleft - leftct;
			$('.page-name').animate({'left':newleft},300);
			newpos = newleft  -(  crrleft - allleft )
			$('#page-content').animate({'left':newpos},300);
			//console.log('moveleft :' + newpos);
		}else{
			newpos = newleft - leftct;
			$('.page-name').animate({'left':newleft},300);
			newpos = newleft  -(  crrleft - allleft  )
			$('#page-content').animate({'left':newpos},300);
			//console.log('moveleft :' + newpos);
		}
		// if($('#page-content .active').offset().left < leftct){
//			newpos = /*$('#page-content').offset().left - */(leftct - ($('#page-content .active').offset().left - $('#page-content').offset().left ));
//			$('.page-name').animate({'left':leftct},300);
//			$('#page-content').animate({'left':newpos},300);
//			console.log('move right :' + newpos);
//			console.log($('#page-content').offset().left+ '  ' + $('#page-content .active').offset().left + '  ' + leftct);
//		}else{
//			$('.page-name').animate({'left':leftct},300);
//			$('#page-content').animate({'left':leftct},300);
//			console.log(leftct);
//		}
		
		
		
	}
	$(window).bind('resize',centerResize);
}
function newsdtpage(){
//	var wdhe = $(window).height(), wdwi = $(window).width();
	var news_w, news_h ;
	
	function centerNews(){
		news_w = $(window).width() - ($(window).width()-580)*0.5;
		news_h = $(window).height() - ($(window).height()-330)*0.5  -55;
		$('#newsdt').css({'height':news_h , 'width':news_w  });
		if($('.news-imgs').length){
			$('.news-text').css({'width':( news_w - 315), 'height':(news_h - 20)});
		}else{
			$('.news-text').css({'width':( news_w - 20), 'height':(news_h - 20)});
		}
		
	}
	centerNews();
	$(window).bind('resize',centerNews);
}
function contactpage(){
	function centerCt(){
		news_w = $(window).width() - ($(window).width()-580)*0.5;
		news_h = $(window).height() - ($(window).height()-330)*0.5  -55;
		$('#contact-page').css({'height':news_h , 'width':news_w  });
		$('.ct-right').width(news_w - $('.ct-left').width() - 1)
	}
	centerCt();
	$(window).bind('resize',centerCt);
}
