// JavaScript Document
String.prototype.find=function(what){
	return (this.indexOf(what)>=0 ? true : false);
}
var em= function(){
	function load(func){
		var oldonload = window.onload;
		if(typeof window.onload != "function")
			window.onload = function(){
				attach(window,"unload",clean);
				func();
			}
		else{
			window.onload = function(){
				oldonload();
				func();
			}
		}
	}
	function clean(){
		window.__objs = null;
		window.__funs = null;
	}
	function attach(obj,type,func,useCapture){
		if(window.addEventListener)
			obj.addEventListener(type,func,useCapture);
		else if(window.attachEvent)
			obj.attachEvent("on"+type,func);
	}
	function detach(obj,type,func,useCapture){
		if(obj.removeEventListener)
			obj.removeEventListener(type,func,useCapture);
		else if(obj.detachEvent)
			obj.detachEvent("on"+type,func);
	}
	return {
		attach:attach,
		detach:detach,
		load:load
	}
}();
function $(str){
	var ts=str.replace(" ",""),objs=[],ds=[];
	if(ts.find(",")){
		ds=ts.split(",");
		var l=ds.length;
		for(var i=0;i<l;i++){
			objs[i]=document.getElemntById(ds[i]);
		}
		return (objs);
	}else{
		var obj=document.getElementById(ts);
		return (obj)?obj:false;
	}
}


if(typeof cgAjax == "undefined") var cgAjax = {};


function slideSimple(){
	this.elem = $('effect');
	if(!this.elem) return;
	this.tmp1 = g.cs(g.fc(this.elem));
	this.target = $('cv').parentNode;
	this.slideN = 0;
	this.srcs = [];
	this.triggers1 = [];
	this.x = null;
	this.length = this.tmp1.length;
	this.direction = 'down';
	for(var i=0; i<this.length; i++){
		var el = g.fc(this.tmp1[i]);
		this.srcs[i] = el.src;
	}
	this.construct = function(){
		this.x = setInterval(this.run.close(this),7000);
	};
	this.run = function(){
		this.mkdirection();
		this.nextSlide();
		this.effect();
		this.setTrigger();
	};
	this.effect = function(){
		var tp = this.slideN+1;
		var str = "<img id='cv' src='"+this.srcs[this.slideN]+"' usemap='#p"+tp+"' />";
		this.target.innerHTML = str;
	};
	this.mkdirection = function(){
		if(this.slideN == this.length -1)
			this.direction = 'up';
		else if(this.slideN == 0)
			this.direction = 'down';
	};
	this.nextSlide = function(){
		if(this.direction == 'down')
			this.slideN ++;
		else if(this.direction == 'up')
			this.slideN --;	
	};
	this.setTrigger = function(){
		for(var i=0; i< this.length; i++){
			if(this.triggers1[i].className.find('current')){
				this.triggers1[i].className = '';
				break;
			}
		}
		this.triggers1[this.slideN].className = 'current';
	};
	this.clear = function(){
		clearInterval(this.x);	
	};
	this.setSlide = function(e){
		this.clear();
		var et = e||window.event;
		var elm = et.srcElement || et.target;
		if(elm.bool != true){
			this.slideN = elm.index;	
			this.setTrigger();
			this.effect();
		}
	};
	this.tmp2 = g.cs($('eTrigger'));
	this.tmp3 = $$('map.xTrigger');
	for(var i=0; i< this.length; i++){
		var obj = g.fc(this.tmp2[i]);
		obj.index = i;
		this.triggers1[i] = obj;
		em.attach(this.triggers1[i],'mouseover',this.setSlide.close(this));
		em.attach(this.triggers1[i],'mouseout',this.construct.close(this));
		var tmp = g.cs(this.tmp3[i]);
		for(var j=0; j<3; j++){
			tmp[j].index = i;
			tmp[j].bool = true;
			em.attach(tmp[j],'mouseover',this.setSlide.close(this));
			em.attach(tmp[j],'mouseout',this.construct.close(this));
		}	
	}
	this.construct();
}

var clockConf={type:"rct",target:"clock"};
function Clock(){
	this.clockConf = null;
	this.target = null;
	this.isIE = null;
	
	/*behavior*/
	this.t = null;
	
	this.run = function(clockConf){
		this.clockConf = clockConf;	
		this.target = $(this.clockConf.target);
		if(!this.target) return false;
		this.isIE = cgAjax.IE;	

		var self = this;

		switch(this.clockConf.type){
			case "clock":
				var date = new Date();
				this.target.hour = date.getHours();
				this.target.min = date.getMinutes();
				this.target.sec = date.getSeconds();
				setInterval(function(){self.countUp();},1000);
				break;
			case "ct":
				this.target.hour =0;
				this.target.min =0;
				this.target.sec =0;
				setInterval(function(){self.countUp();},1000);
				break;
			case "rct":
				var tmp = this.target.getAttribute("title").split("-");
				this.target.hour = 	tmp[0];
				this.target.min = tmp[1];
				this.target.sec = tmp[2];
				this.t = setInterval(function(){self.countDown();},1000);
				break;
			default:
				break;
		}
	};
	
	this.countUp = function(){
		this.target.sec +=1;
		
		if(this.target.sec == 60){
			this.target.sec = 0;
			this.target.min +=1;
		}
		
		if(this.target.min == 60){
			this.target.min = 0;
			this.target.hour +=1;
		}
		
		this.showTime();
	};
	
	this.countDown = function(){
		this.target.sec = parseInt(this.target.sec);
		this.target.min = parseInt(this.target.min);
		this.target.hour = parseInt(this.target.hour);
		
		this.target.sec -=1;
		
		if(this.target.sec == -1){
			this.target.sec = 59;
			this.target.min -=1;
		}
		
		if(this.target.min == -1){
			this.target.min = 59;
			this.target.hour -=1;
		}
		
		if(this.target.sec <10) this.target.sec = "0"+this.target.sec;
		if(this.target.min <10) this.target.min = "0"+this.target.min;
		if(this.target.hour<10) this.target.hour = "0"+this.target.hour;
		
		if(this.target.hour == 0) this.target.id = "hurry";
		
		this.showTime();
		
		if(this.target.hour == 0 && this.target.min == 0 && this.target.sec == 0) clearInterval(this.t);
	};
	
	this.showTime = function(){
		this.target.innerHTML ="Time Left: "+this.target.hour+":"+this.target.min+":"+this.target.sec;
	}
}


function iniClass(){
	var slide = new slideSimple(); 
	var clock=new Clock();clock.run(clockConf);
	var gallery=new Photo();gallery.gallery(photoConf);
	var proTab=new Tab();proTab.initialise(tabConf);
	var newsTab=new Tab();newsTab.initialise(tabxConf);
	var rr = new Runner();
}

em.load(iniClass);


