/**
 * @title Just Fate, no stay night
 * @author Cyan
 */

var Fate;

(function(){

Fate = function(){
	
	var F = this, a = arguments;
	
	if (!(F instanceof Fate)){
		return new Fate();
	} else {
		
		this.init();
		
		return F;
	}
}

Fate.prototype = {
	
	/**
	 * @alias Vesperia.init
	 * @param {String} [Initial Type]
	 */
	init: function(){
		var F = this, a = arguments;
		if (a[0]) {
			var init_a = [];
			init_a = a[1] ? a[1] : "";
			eval("F._init_" + a[0] + "(init_a)");
		}
	}, // init
	
	
	_init_header: function(){
		
		/**
		 * Initial
		 */
		var F = this, roller = jmallJQ("#jmall_roll_tip");
		
		F._init_textRoller(roller, roller.width(), 300);
		
	}, // _init_header
	
	_init_textRoller: function(roller, roller_width, ivl){
		
		if(roller.length > 0){
			// Set css
			roller.css("position","relative").css("overflow","hidden").data("text",roller.text());
			
			// Set roller node
			var roller_node = jmallJQ("<span>" + roller.data("text") + "</span>").css("position","absolute").css("display","block").css("top","5px");
			var roller_node_width = roller_node.appendTo(roller).width();
			roller_node.css("width",roller_node_width);
			var nodes = [], nodes_num = Math.floor(roller_width / (roller_node_width + ivl)) + 2;
			
			// Clear original Text
			roller.html("&nbsp;");
			
			// Act!
			for (i = 0; i < nodes_num; i++){
				if( i == 0)
					nodes[i] = roller_node.clone().appendTo(roller).css("left",roller_width);
				else 
					nodes[i] = roller_node.clone().appendTo(roller).css("left",roller_width + (roller_node_width + ivl) * i);
			}
			var time_handle = setInterval(function(){
				for (i = 0; i < nodes_num; i++){
					if (Number(nodes[i].css("left",Number(nodes[i].css("left").split("px")[0]) - 1).css("left").split("px")[0]) < -roller_node_width) {
						nodes[i].css("left", roller_node_width + 2 * ivl);
					}
				}
			},20)
		}
		
	}, // _init_textRoller
	
	_init_body: function(){
		
		var F = this;
		// Translate Menu
		F.bindPopup("#jmall_btn_translate","#jmall_trans_menu",250,"cm");
		
		/*
		jmallJQ("#jmall_btn_shoppingcart").bind("click",function(){
			F.showCart();
		});
		
		jmallJQ("#jmall_btn_backtoshopping").bind("click",function(){
			F.hideCart();
		});
		*/
		
	}, // _init_body
	
	bindPopup: function(triggerObj,popupObj,respTime,popType,callback){
		if(typeof(triggerObj) == "string")
			triggerObj = [triggerObj];
			
		// Debug Mode
		this.debug = 0;
		
		var that = this;
		this.callback = callback;
		// Constructor here
		this.triggerObj = triggerObj;
		this.triggerIndex,this.triggerChildIndex;
		this.popupObj = popupObj;
		this.respTime = respTime;
		this.popType = popType;
		
		// Status Variable
		this.overTrigger = Boolean(0);
		this.overPopup = Boolean(0);
		this.showType = "";
		this.hideType = "";
		
		// Time handle
		this.timerhandle = null;
		
		// Define showtype
		if (popType.substr(0,1) == "c") 
			this.showType = "click";	
		if (popType.substr(0,1) == "m") 
			this.showType = "mouseover";
		
		if (popType.substr(1,2) == "c") 
			this.hideType = "click";	
		if (popType.substr(1,2) == "m") 
			this.hideType = "mouseover";
		
		// Click show
		if(this.showType == "click"){
			for (i = 0; i < this.triggerObj.length; i++) {
				jmallJQ(this.triggerObj[i]).click(function(e){
					for (j = 0; j < that.triggerObj.length; j++) {
						for (k = 0; k < jmallJQ(that.triggerObj[j]).length; k++) {
							if (this == jmallJQ(that.triggerObj[j])[k]) 
								that.triggerIndex = j;
							that.triggerChildIndex = k;
						}
						
						if (jmallJQ(that.popupObj).is(":hidden")) {
							that._showPopup(e);
						}
						
						if (that.debug == 1) {
							jmallJQ("#triCliCount").html((parseInt(jmallJQ("#triCliCount").html()) + 1).toString());
							jmallJQ("#popupStatus").html("show");
						}
					}
				})
			}
		}
		
		// Click hide
		if (this.hideType == "click"){
			jmallJQ("body").click(function(){
				if (!Boolean(jmallJQ(that.popupObj).is(":hidden")) && !that.overTrigger && !that.overPopup) {
					that._hidePopup();
				}
				
				if(that.debug == 1){
					jmallJQ("#bodyCliCount").html((parseInt(jmallJQ("#bodyCliCount").html()) + 1).toString());
					jmallJQ("#popupStatus").html("hide");
				}
			})
		}
		
		
		// Mouseover show
		for (i = 0; i < this.triggerObj.length; i++) {
			jmallJQ(this.triggerObj[i]).mouseover(function(e){
				for (j =0 ; j < that.triggerObj.length; j++){
					for (k = 0; k < jmallJQ(that.triggerObj[j]).length; k++) {
						if (this == jmallJQ(that.triggerObj[j])[k]) {
							that.triggerIndex = j;
							that.triggerChildIndex = k;
							
							that.overTrigger = 1;
							clearTimeout(that.timerhandle);
							
							that.timerhandle = setTimeout(function(){
								jmallJQ("#onTrigger").html("yes");
								if (that.showType == "mouseover") {
									if (that.debug == 1) 
										jmallJQ("#popupStatus").html("show");
									that._showPopup(e);
								}
							}, respTime)
							
							break;
						}
					}
				}
			});
		}
		
		jmallJQ(this.popupObj).mouseover(function(){
			that.overPopup = 1;
			clearTimeout(that.timerhandle); 
			that.timerhandle = setTimeout(function(){
				if(that.debug == 1) {
					jmallJQ("#onPopup").html("yes");
					jmallJQ("#popupStatus").html("show");
				}
				that._showPopup();
			},respTime)
		})
		
	
		// Mouseout hide
		for (i = 0; i < this.triggerObj.length; i++) {
			jmallJQ(this.triggerObj[i]).mouseout(function(){
				that.overTrigger = 0;
				if (that.debug == 1) 
					jmallJQ("#onTrigger").html("no");
				for (j =0 ; j < that.triggerObj.length; j++){
					for (k = 0; k < jmallJQ(that.triggerObj[j]).length; k++) {
						if (this == jmallJQ(that.triggerObj[j])[k]) 
							break;
					}
					isMouseout(j);
				}
			})
		}
		
		jmallJQ(this.popupObj).mouseout(function(){
			that.overPopup = 0;
			if(that.debug == 1) jmallJQ("#onPopup").html("no");
			isMouseout(-1);
		})
		
	
		/**
		 * @alias isMouseout
		 * @alias JSH.bindPopupEvent.isMouseout
		 * @return {null}
	 	 * @projectDescription mouseout event for mouseover trigger
		 */
		var isMouseout = function(outIndex){
			if (outIndex == that.triggerIndex || outIndex == -1) {
				clearTimeout(that.timerhandle);
				
				if (that.hideType == "mouseover") {
					that.timerhandle = setTimeout(function(){
						if (!that.overTrigger && !that.overPopup) {
							if (that.debug == 1) 
								jmallJQ("#popupStatus").html("hide");
							that._hidePopup();
						}
					}, that.respTime)
				}
			}
		}
		
	}, // bindPopup


			
	/**
	 * @alias Vesperia.bindPopup.hidePopup
	 * @projectDescription Default hidePopup method for bindPopup.
	 */
	_hidePopup: function(){
		jmallJQ(this.popupObj).fadeOut(100);
	},
	
	
	/**
	 * @alias Vesperia.bindPopup.showPopup
	 * @projectDescription Default showPopup method for bindPopup.
	 */
	_showPopup: function(e){
		if (this.callback) 
			if (!this.callback(e, this.triggerObj[this.triggerIndex], this.popupObj, this.triggerChildIndex)) 
				return;
		jmallJQ(this.popupObj).fadeIn(100);
	},
	
	
	/**
	 * @alias Vesperia.getBrowserRules
	 * @return {Array} [height,width,sTop,sLeft,sHeight,sWidth] 
	 * @projectDescription Return browser size param
	 */	
	getBrowserRules: function(){
		return {
			height : document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight,
			width : document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth,
			sTop: document.documentElement.scrollTop,
			sLeft: document.documentElement.scrollLeft,
			sHeight: document.documentElement.scrollHeight,
			sWidth: document.documentElement.scrollWidth
		}
	},


	/**
	 * @alias Vesperia.getMousePos
	 * @return {Array} [x,y] 
	 * @projectDescription Return mouse position
	 */
	getMousePos: function(ev){
		if (ev.pageX || ev.pageY) {
			return {x:ev.pageX, y:ev.pageY}
		} 
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		}
	},
	
	showCart: function(){
		
		var cart = jmallJQ("#jmall_cart_detail"), F = this;
		cart.show();
		
		var offsetTopValue = (F.getBrowserRules().height - cart.height() > 0 ? F.getBrowserRules().height - cart.height() : 0)/2;
		var offsetLeftValue = (F.getBrowserRules().width - cart.width() > 0 ? F.getBrowserRules().width - cart.width() : 0)/2
		
		cart.css("top", offsetTopValue + F.getBrowserRules().sTop + 6);
		cart.css("left", offsetLeftValue);
	},
	
	hideCart: function(){
		jmallJQ("#jmall_cart_detail").hide();
	}
	
} // Fate.prototype

for (i in Fate.prototype){
	Fate[i] = Fate.prototype[i];
}
Fate();
	
})();