/*
 * @version 1.0
 */

$(document).ready(function(){
						   
	// Get current year
	now = new Date;
	theYear = now.getYear();
	if (theYear < 1900)	theYear = theYear+1900;
	$('#currentYear').html(theYear);
	
	
	// Search form hover stage
	$('.searchForm .txf').hover(
		function(){ $(this).parent().addClass('hover'); },
		function(){ $(this).parent().removeClass('hover'); }
	);
	
	
	// Language
	$('.lang a').hover(
		function(){
			$(this).find('span').stop(true, true).show(150);
		},
		function(){
			$(this).find('span').stop(true, true).hide(150);
		}
	);
		
	
	// Contact	
	$('#formcontact').submit(function(){
		var fullname = $(this).find('input[name="fullname"]');
		if(fullname.val()==''){ jAlert('Please enter Your name'); fullname.focus(); return false;}
		
		var email = $(this).find('input[name="email"]');
		if(!isEmail(email.val())){ jAlert('Please enter Email'); email.focus(); return false;}
		
		var tel = $(this).find('input[name="tel"]');
		if(tel.val()==''){ jAlert('Please enter Your Phone');tel.focus();return false;}
		
		var subject = $(this).find('input[name="subject"]');
		if(subject.val()==''){ jAlert('Please enter Subject');subject.focus();return false;}
		
		var content = $(this).find('textarea[name="content"]');
		if(content.val()==''){ jAlert('Please enter Message');content.focus();return false;}
		
		var module = $(this).find('input[name="module"]');
		var company = $(this).find('input[name="company"]');
		var args = {
			'fullname': fullname.val(),
			'email': email.val(),
			'tel': tel.val(),
			'company': company.val(),
			'subject': subject.val(),
			'content': content.val()
		}
		$('#sending').append('Sending... &nbsp;&nbsp;');
		 $(this).find('input[type="submit"]').attr({'disabled':'disabled'});
		$.post('./'+module.val(),args,function(data){
			$('#formcontact').html('<div class="msg">'+data+'</div>');
			$('#sending').html('');
		});
		return false;
	});
	
	
	// Photo detail
	$('.photoOther li:first').addClass('active');
	$('.photoOther img').click(function(){
		$(this).parent().parent().find('.active').removeClass('active');
		$(this).parent().addClass('active').parent().prev().find('img').attr('src', $(this).attr('rel'));
	});
	
	
/*	//Bg Sound
	var press_flashvars = { };
	var press_params = { wmode:"transparent", allowScriptAccess:"always", menu:"false", allowFullscreen: 'true'};
	var press_attributes = {};
	swfobject.embedSWF("swf/soundBg.swf", "bgsound", "71", "18", "9.0.0","swf/expressInstall.swf", press_flashvars, press_params, press_attributes);
*/	
	
	// Project
	rotateImg();
	rotateImg_paging();
	rotateSlide();
	
	
	// Sub menu
	if ($('.section_about').html != null){
		$('.menusub > a').click(
			function(){
				var menuSub = $(this).next().html();
				if (menuSub != null){
					$(this).toggleClass('show');
					
					if ($(this).hasClass('show')){
						$(this).next().slideDown(200);
					}
					else $(this).next().slideUp(200);
					return false;
				}				
			}
		);
	}
});


var delayRotate = 5000;
var animationSpeed = 1000;
var tempRotateI = 0;
var count_lastest;
var buttonClick = false;
var autoRotate;
function rotateImg(){
	
	count_lastest = $('.rotateItem').length;		
	
	//images
	if (buttonClick==false){
		$('.rotateItem').fadeOut(animationSpeed);
		$('.rotateItem:eq('+tempRotateI+')').fadeIn(animationSpeed);
	}
	else{
		$('.rotateItem').hide();
		$('.rotateItem:eq('+tempRotateI+')').show();
	}
	
	$('#rotateThumb .active').removeClass('active');
	$('#rotateThumb img:eq('+tempRotateI+')').addClass('active');
	
	if ((count_lastest>1) /*&& (buttonClick==false)*/) autoRotate=setTimeout("rotateImg()",delayRotate)
	tempRotateI = tempRotateI+1;
	if (tempRotateI == count_lastest) tempRotateI=0;
	
	buttonClick = false;
};

function rotateImg_paging(){	
	$('#rotateThumb img:first').addClass('active');
	
	$('#rotateThumb img').each(function(i){
		$(this).attr('rel',i);
	});
	
	$('#rotateThumb img').hover(function(){
		$('#rotateThumb img').removeClass('active');
		$(this).addClass('active');
									  
		if (autoRotate) clearTimeout(autoRotate);
		buttonClick = true;
		tempRotateI = parseInt($(this).attr('rel'));
		rotateImg();
	});
}

function rotateSlide(){
	var numImagesLoad=10;
	var speedScroll=300;
	var numPageIndex=1;
	var numItems=$('.rotateThumb').length;
		if (numItems>10) $('#btnRight').show().css({display:'block'});
	var numImgBlockWidth=$('.rotateThumb').width();
	var numImgBlockHeight=$('.rotateThumb').height();
	for(var i=0;i<numItems;i++) $('.rotateThumb:eq('+i+')').css("left",(i*(Number(numImgBlockWidth)+4)));
	$('.slideInner').width(Number(numImagesLoad * (numImgBlockWidth+13))+2);
	$('.slideInner').height(numImgBlockHeight+2);
	$("#btnRight").click(function(){								 
		if(numPageIndex<(numItems/numImagesLoad)){
			$('#btnLeft').show().css({display:'block'});
			$(".rotateThumb").animate({left:'-='+(Number(numImgBlockWidth)+4)*numImagesLoad+'px'}, speedScroll, function(){
				/*curPosition =  $('.nivo-controlNav .active').css('left');
				numCurPosition = curPosition.substring(0, curPosition.length-2);
				
				actPosition = 0 - Number(numCurPosition);
				if (actPosition<0) actPosition-=104;
				else actPosition+=104;*/
			});
			numPageIndex++;
			if (numPageIndex>=numItems/numImagesLoad) $('#btnRight').hide();
		}
		return false;
	});
	$("#btnLeft").click(function(){
		if(numPageIndex>1){
			$('#btnRight').show().css({display:'block'});
			$(".rotateThumb").animate({left:'+='+(Number(numImgBlockWidth)+4)*numImagesLoad+'px'}, speedScroll, function(){
				/*curPosition =  $('.nivo-controlNav .active').css('left');
				numCurPosition = curPosition.substring(0, curPosition.length-2);
				
				actPosition = 0 - Number(numCurPosition);
				if (actPosition<0) actPosition-=104;
				else actPosition+=104;*/
			});
			numPageIndex--;
			if (numPageIndex==1) $('#btnLeft').hide();
		}
		return false;
	});
}
