// JavaScript Document
var current = '';
var current_lang = '';
var current_news = '';
var current_about = '';
var is_exchanging = false;
var is_Cufon = false;
var org_cat = '';
var tar_cat = '';
var img_width = 38;
if($.browser.msie){
	$.getScript("script/Helvetica_Neue_LT_Pro_275.font.js",function(){
		$('h1 .en').css('margin','4px 0')
		Cufon.replace('h1 .en',{color:'#000'});
	});
	is_Cufon = true;
}
function change_lang(str){
	current_lang = str;
	$(".lang").each(function(){
		if($(this).hasClass(str)){
			$(this).css('display','');
		}else{
			$(this).css('display','none');
		}
	})
	
	$('link[@rel*=stylesheet][title]').each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == str) this.disabled = false;
	});
}
function show_content(type){
	is_exchanging = true;
	if(type == current) return;
	$('h1').removeClass("current");
	
	if(is_Cufon) Cufon.replace('h1 .en',{color:'#000'});
	
	
	if(type != 'work'){
		$('#title_'+type).addClass("current");
		if(is_Cufon) Cufon.replace('#title_'+type+' .en',{color:'#ff3300'});
	}
	
	$("#"+type).children('.info').addClass('no_empty');
	var height = $("#"+type+' .info').height();
	if(current != ''){
		$("#"+type).children('.info').animate({height: 'toggle'},"slow",function(){is_exchanging = false;}); 
		$("#"+current).children('.info').animate({height: 'toggle'},"slow",function(){ $(this).removeClass('no_empty') }); 
	}else{
		$("#"+type).children('.info').animate({height: 'toggle'},"slow",function(){is_exchanging = false;}); 
	}
	
	if((current == 'work') || (current == 'news')|| (current == 'about')){
		$("#"+current).children('.list').fadeOut(); 
	}
	if((type == 'work') || (type == 'news')|| (type == 'about')){
		$("#"+type).children('.list').fadeIn(); 
	}
	current = type;
}

function init(){	
	$('.list').fadeOut('fast'); 
	change_lang('en');
	//show_content('work');
	init_work_list();
	init_h1();
	init_news_list();
	init_about_list();
	//$('#work .list ul li:first-child').click();
	$('#news .list ul li:first-child').click();
	$('#about .list ul li:first-child').click();
	
	$('#press .press').each(function(){
		var id = $(this).attr('id');
		$('a.'+id+'_thumb').lightBox({fixedNavigation:true});
		$('a.'+id+'_enlarge').lightBox({fixedNavigation:true});
	})
	//$('a.press_thumb').lightBox();
	//$('a.press_enlarge').lightBox();
	
	
}

function init_h1(){	
	if(is_Cufon){
		$('h1 .en').css('margin','4px 0')
		Cufon.replace('h1 .en',{color:'#000'});
	}
	$('h1').click(function(){
		if(is_exchanging == true) return;
		var id = $(this).attr('id');
		if(id.length>6) 
			type = id.substr(6);
		
		if((current == '') && (type == 'work')){
			var id = $("#work #gallery img.latest:first-child").attr('id');
			$("#work #gallery img#"+id).click();
		}else{
			if(type == current) return;
		
			show_content(type);
			if(type == 'press') init_press();
		}
	})
}

function init_news_list(){		
	$('#news .info .wrap').css('height',0);
	$('#news .list ul li').click(function(){
		var cat = $(this).attr('class');
		if(cat == current_news) return;
		$('#news .info .' + cat + ' .image' ).html('<img src="'+news_list_image_path + news_list_image[cat] + '" alt="" title="">');
		if(current_news == '') $('#news .info').css('display','block');//add to avoid get 0 from height()
		var o = $('#news .info .'+cat);
		var target_height = o.height();
		if(current_news != ''){
			$('#news .info .' + current_news).parent().animate({ height: 0}, "skow");
		}
		if(current_news == '') $('#news .info').css('display','none');//add to avoid get 0 from height()
		
		$('#news .info .' + cat).parent().animate({ height: target_height},  "skow");
	
		$(this).parent().children('li.current').removeClass('current');
		$(this).addClass('current');
		
		current_news = cat;
		change_lang(current_lang);
	})
}

function init_about_list(){		
	$('#about .info .wrap').css('height',0);
	$('#about .list ul li').click(function(){
		var cat = $(this).attr('class');
		if(current_lang == 'cn'){
			var doc = about_list_cn_doc[cat] ;
		}else{
			var doc = about_list_en_doc[cat] ;
		}
		if(doc.length > 0){
			window.open('download.php?folder='+about_list_doc_path+'&filename='+doc);
			return;
		}
		
		if(cat == current_about) return;
		//$('#about .info .' + cat + ' .image' ).html('<img src="'+news_list_image_path + news_list_image[cat] + '" alt="" title="">');
		if(current_about == '') $('#about .info').css('display','block');//add to avoid get 0 from height()
		var o = $('#about .info .'+cat);
		var target_height = o.height();
		if(current_about != ''){
			$('#about .info .' + current_about).parent().animate({ height: 0}, "skow");
		}
		if(current_about == '') $('#about .info').css('display','none');//add to avoid get 0 from height()
		
		$('#about .info .' + cat).parent().animate({ height: target_height},  "skow");
	
		$(this).parent().children('li.current').removeClass('current');
		$(this).addClass('current');
		
		current_about = cat;
		change_lang(current_lang);
	})
}

function init_press(){		
	$("#press .press:odd").css('margin-left','20px')
	$("#press .press_thumb").each(function(){
		if($(this).children('img')[0] == null)	$(this).html('<img src="'+ $(this).html() + '" alt="" title="">');
	})
	change_lang(current_lang);
}
function trans_work_gallery(){
	hide_work_gallery();
}
function hide_work_gallery(){
	$("#work #gallery .year .year_img img").each(function(i){
		target_opacity = 0;
		if(($(this).hasClass(org_cat) || (org_cat == '')) && $(this).hasClass(tar_cat)) target_opacity = 0.25;
		if(current == '') target_opacity = 1;
		if(i == 0){
			$(this).animate({"opacity": target_opacity},"slow",function(){shrink_work_gallery()}); 
		}else{
			$(this).animate({"opacity": target_opacity},"slow",function(){}); 
		}
	})
}
function shrink_work_gallery(){
	//alert('shrink_work_gallery');
	$("#work #gallery .year .year_img").each(function(i){
		target_width = 1;
		if(($(this).children('img').hasClass(org_cat)  || (org_cat == ''))&& $(this).children('img').hasClass(tar_cat)) target_width = img_width;
		if(i == 0){
			$(this).animate({"width": target_width},"slow",function(){stretch_work_gallery()}); 
		}else{
			$(this).animate({"width": target_width},"slow",function(){}); 
		}
	})
	
	$("#work #gallery .year").each(function(i){
		target_width = 0;
		if($(this).children('.year_img').children('.'+tar_cat).size()==0){
			$(this).animate({"width": target_width},"slow"); 
		}
	})
}
function stretch_work_gallery(){
	//alert('stretch_work_gallery');
	//return;
	if(org_cat == ''){
		final_work_gallery();
		return;
	}
	$("#work #gallery .year").each(function(i){
		target_width = 0;
		target_width = $(this).children('.year_img').children('.'+tar_cat).size()*img_width;
		$(this).animate({"width": target_width},"slow"); 
		if(i == 0){
			$(this).animate({"width": target_width},"slow",function(){}); 
		}else{
			$(this).animate({"width": target_width},"slow",function(){}); 
		}
	})
	$("#work #gallery .year .year_img").each(function(i){
		target_width = img_width;
		if(!$(this).children('img').hasClass(tar_cat)) target_width = 0;
		if(i == 0){
			$(this).animate({"width": target_width},"slow",function(){show_work_gallery()}); 
		}else{
			$(this).animate({"width": target_width},"slow",function(){}); 
		}
	})
	
}

function show_work_gallery(){
	//alert('show_work_gallery');
	$("#work #gallery .year .year_img img").each(function(i){
		target_opacity = 0;
		if($(this).hasClass(tar_cat)){
			target_opacity = 0.25;		
			$(this).parent().width(img_width+'px');
		}
		if(i == 0){
			$(this).animate({"opacity": target_opacity},"slow",function(){final_work_gallery()}); 
		}else{
			$(this).animate({"opacity": target_opacity},"slow",function(){}); 
		}
	})
}


function final_work_gallery(){
	if(current == ''){
		//alert('x');preload();
	}else{
	 	var id = ($("#work #gallery img."+tar_cat).attr('id'));
		$("#work #gallery img#"+id).click();
	}
	 is_exchanging = false;
}


function init_work_list(){
	$("#work #gallery .year .year_img img").click(function(i){
		if(current != 'work')
			show_content('work');
			
		//$("#work #gallery .year .year_img img").addClass('alpha25');
		//if($(this).hasClass('current')) return;
		var id = $(this).attr('id');
		$("#work #gallery .year .year_img img").not("#"+id).removeClass('current');
		$("#work #gallery .year .year_img img").not("#"+id).animate({'opacity':0.25},"fast");
		$(this).addClass('current');
		//$(this).removeClass('alpha25');
		$(this).animate({'opacity':1},"fast");
		var id = $(this).attr('id');
		$('#work_image').attr('src',work_list_image_path + work_list_image[id]);
		var img = new Image(); 
		img.src=work_list_image_path + work_list_image[id]; 
		if((img.width != undefined) && (img.width > 0)){
			$('#work_image').attr('width', img.width);
			$('#work_image').attr('height',img.height);
		}else{
			$('#work_image').load(function(){
				img.src=$(this).attr('src'); 
				$('#work_image').attr('width', img.width);
				$('#work_image').attr('height',img.height);
			})
		}
		$('#work .info .rt .main_text').html(work_list_desc[id] + '<br />' + work_list_year[id] );
		change_lang(current_lang);
	});
	
	$('#work .list ul li').click(function(){
		if($(this).hasClass('current')) return;
		
		org_cat = tar_cat;
		tar_cat = $(this).attr('class');
		$(this).parent().children('li.current').removeClass('current');
		$(this).addClass('current');
		is_exchanging = true;
		trans_work_gallery();
	})
}
function preload(){
	//preload
//	for(ss in press_list_image){
//		jQuery.preLoadImages(press_list_image_path + press_list_image[ss]);
//	}
	for(ss in work_list_image){
		jQuery.preLoadImages(work_list_image_path + work_list_image[ss]);
	}
	for(ss in news_list_image){
		jQuery.preLoadImages(news_list_image_path + news_list_image[ss]);
	}
}

$(document).ready(function(){
	init();
	
	$('#work_image').load(function(){
		//$('#work .info .rt').css('height',$(this).height()+'px');
	})
		
	// validate signup form on keyup and submit
	$("#frm_contact").validate({
		rules: {
			"name":{required:true,minlength:2},
			"email":{required:true,email:true},
			"company":{required:false},
			"subject":{required:true},
			"message":{required:true}
		},
		messages: {
			"name":{
				required:"<span class='lang en'>Please enter Name</span><span class='lang cn'>请输入姓名</span>",
				minlength:"<span class='lang en'>Your Name must be at least 2 characters long</span><span class='lang cn'>您的姓名至少包含2个字符</span>"},
			"email":{
				required:"<span class='lang en'>Please enter Email</span><span class='lang cn'>请输入邮箱</span>",
				email:"<span class='lang en'>Please enter a valid Email</span><span class='lang cn'>请输入正确的邮箱地址</span>"},
			"subject":{required:"<span class='lang en'>Please enter Subject</span><span class='lang cn'>请输入标题</span>"},
			"message":{required:"<span class='lang en'>Please enter Message</span><span class='lang cn'>请输入留言</span>"}
		},
		submitHandler: function(form) {
	   		$('#send_result').html('sending... / 发送中...');
	   		form.submit();
	   },
		showErrors: function(errorMap, errorList) {
			this.defaultShowErrors();
	   		change_lang(current_lang);
	   }
	});
	
	var gallery_total = $("#work #gallery .year .year_img img").size();
	
	gallery_total = Math.floor(gallery_total*0.5);
	
	var gallery_loaded = 0;
	is_exchanging = true;
	
	$("#work #gallery .year .year_img img").each(function(i){
		$(this).load(function(){
			if(++gallery_loaded == gallery_total){
				//alert('load over');
				org_cat = '';
				tar_cat = 'latest';
				$('#work .list li:first-child').addClass('current');
				trans_work_gallery();
				//preload();
			}else{
				//alert(gallery_loaded);
			}
		})
	})
	

	
	
})
