﻿cj.init({require:'inline_popup,debug'});

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
cj.util.loadJs(gaJsHost + "google-analytics.com/ga.js");

cj.evt.add(cj.w, 'load', function(){
	//fix_height
	(function(){
		var n = cj.d.getElementById('fix_height');
		if(n){
			n.parentNode.style.height = cj.util.getElmSize(n).h + 'px';
		}
	}());
	
	try {
		var pageTracker = _gat._getTracker("UA-11162456-1");
		pageTracker._trackPageview();
	}catch(err){}
	
	var lang = (/\/tw\//.test(cj.w.location)) ? 'tw' : 'en';
	
	var n;
	var uls = cj.d.getElementsByTagName('ul');	
	for(var i=0; i<uls.length; i++){
		n = uls[i];
		if(/prodContainer/.test(n.className)){
			n.lb = cj.d.getElementById(n.getAttribute('lftBtn'));
			n.rb = cj.d.getElementById(n.getAttribute('rgtBtn'));
			n.items = n.getElementsByTagName('li').length;
			if(!n.pos){
				n.pos = 0;
				n.minPos = 4-n.items;
			}
			if(n.items<4){
				cj.util.setOpacity(n.lb.childNodes[0], 0.4);
				n.lb.style.cursor = 'default';
				n.lb.onclick = function(){ return false;};
				cj.util.setOpacity(n.rb.childNodes[0], 0.4);
				n.rb.style.cursor = 'default';
				n.rb.onclick = function(){ return false;};
				
			}else{
				n.lb.tg = n.rb.tg = n;
				cj.util.setOpacity(n.lb.childNodes[0], 0.4)
				
				n.rb.onclick = function(){
					var t = this, n = t.tg;
					n.step = 1;
					if(n.pos > n.minPos){
						n.pos -= 1;
						cj.fn.slid.run(n, '-');
					}
					op = (n.pos == n.minPos) ? 0.4 : 1;
					cj.util.setOpacity(t.childNodes[0], op);
					
					op = (n.pos == 0) ? 0.4 : 1;
					cj.util.setOpacity(n.lb.childNodes[0], op);
					return false;
				}
				
				n.lb.onclick = function(){
					var t = this, n = t.tg;
					n.step = 1;
					if(n.pos < 0){
						n.pos++;
						cj.fn.slid.run(n, '+');
					}
					op = (n.pos == 0) ? 0.4 : 1;
					cj.util.setOpacity(t.childNodes[0], op);
					
					op = (n.pos == n.minPos) ? 0.4 : 1;
					cj.util.setOpacity(n.rb.childNodes[0], op);
					return false;
				}
			}
		}
	}
	
	var view_main = cj.d.getElementById('view_main');
	var view_btn = cj.d.getElementById('view_btn');
	var view_btn_open = cj.d.getElementById('view_btn_open');
	var view_btn_close = cj.d.getElementById('view_btn_close');
	if(view_main && view_btn){
		view_main.open = 0;
		
		view_main.imgs = view_main.getElementsByTagName('img');
		if(!view_main.imgs.length) return;
		view_main.img = view_main.imgs[0];
		view_main.maxH = view_main.img.offsetHeight;
		view_main.minH = 178;
		view_main.step = 5;
		view_main.h = view_main.minH;
		
		view_main.show = function(){
			if(view_main.open==0){
				view_main.h += view_main.step;
				view_main.h = Math.min(view_main.h, view_main.maxH);
				view_main.style.height = view_main.h + 'px';
				if(view_main.h<view_main.maxH){
					setTimeout(function(){view_main.show()}, 3);
				}else{
					view_main.open ^= 1;
				}
			}else{
				view_main.h -= view_main.step;
				view_main.h = Math.max(view_main.h, view_main.minH);
				view_main.style.height = view_main.h + 'px';
				if(view_main.h>view_main.minH){
					setTimeout(function(){view_main.show()}, 3);
				}else{
					view_main.open ^= 1;
				}
			}
			
		}
		view_btn.onclick = function(){
			if (view_main.open == 0){
				view_main.show();
				view_btn_open.style.display = 'none';
				view_btn_close.style.display = 'block';
			}else{
				view_main.show();
				view_btn_open.style.display = 'block';
				view_btn_close.style.display = 'none';
			}
		}
		var imgs = view_main.getElementsByTagName('img');
		if(imgs && imgs.length==1){
			img = imgs[0];
			img.style.cursor = 'pointer';
			img.onclick = view_btn.onclick;
		}
		//view_btn.onclick();
	}
	
	var f = cj.d.getElementById('formContact');
	if(f){
		f.onsubmit = function(){
			if(!cj.form.chk(f, ['uname,text', 'email,email', 'message,text'])){
				return false;
			}
			return true;
		}
	}
	
	//search
	var daynday_keyword = cj.d.getElementById('daynday_keyword');
	var daynday_search_btn = cj.d.getElementById('daynday_search_btn');
	if(daynday_keyword&&daynday_search_btn){
		daynday_keyword.onkeypress = function(ev){
			var ev = ev || cj.w.event;
			var code = ev.keyCode || ev.which;
			if(code==13){
				daynday_search_btn.click();
			}
		}
		daynday_search_btn.onclick = function(){
			if(daynday_keyword.value){
				loc = 'search&q=' + daynday_keyword.value;
				cj.w.location = loc;
			}
		}
	}
	
	var history_main3 = cj.d.getElementById('history_main3');
	if(history_main3&&history_main3.className=='result'){
		var h = history_main3.clientHeight;
		if(h<500) history_main3.style.height = '500px';
	}
});

cj.fn = {
	slid : {
		width : 160,
		step : 100,
		mtime : 5,
		rate : 1.5,
		run : function(n, dir){
			if(!n.step) n.step = 1;
			dx = Math.pow( this.rate , n.step) * (this.width/this.step);
			n.step++;
			
			if(dir=='+'){
				npos = parseInt(n.offsetLeft + dx);
				if(npos < n.pos * this.width){
					n.style.left = npos+'px';
					setTimeout(function(){cj.fn.slid.run(n, dir)}, this.mtime);
					
				}else{
					n.style.left = (n.pos * this.width) + 'px';
				}
			}else{
				npos = parseInt(n.offsetLeft - dx);
				if(npos > n.pos * this.width){
					n.style.left = npos+'px';
					setTimeout(function(){cj.fn.slid.run(n, dir)},this.mtime);
					
				}else{
					n.style.left = (n.pos * this.width) + 'px';
				}
			}
		}
	}
	,prodShowBimg : function(id){
		var prodBimgHolder = cj.d.getElementById('prodBimgHolder'), t = this;
		if(!prodBimgHolder) return;
		prodBimgHolder.style.backgroundImage = 'url(' + cj.d.getElementById(id).src + ')'; return;
		alert(prodBimgHolder.style.backgroundImage); return;
		t.bimgs = prodBimgHolder.getElementsByTagName('img');
		for(var i=0; i<t.bimgs.length; i++){
			n = t.bimgs[i];
			n.style.display = (n.id==id) ? 'block' : 'none';
		}
	}
	,closePopup : function(){
		try{
			var w = cj.w.parent;
			if(cj.w.name == 'popup'){
				w.focus();
				cj.w.close();
			}else{
				w.cj.inline_popup.close();
			}
		}catch(e){}
	}
}
