//submit protect
var SubmitProtect = new function () {
   
   this.forButton = function(theButton) {
       if( theButton.hasClicked ) {
          window.alert('Submit only allow once! \n操作按鈕只能夠按一次！');
          return false;
       } else {
          theButton.hasClicked = true;
          theButton.style.color= 'red';
          var inputs = document.getElementsByTagName('input') ;
	      for(var i = 0 ; i < inputs.length ; i=i+1) {
	         var tempobj = inputs[i];
			 if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset"|| tempobj.type.toLowerCase() == "button") {
			    if(tempobj == theButton) {
			        continue;
			    }
			    tempobj.disabled = true; 
			 }
	      }
	      
          return true;
       }
   };
   
   this.forId = function(buttonId) {
		var outer = this;
		var theButton = document.getElementById(buttonId);
		var orgOnClick = theButton.onclick ;
		if(!orgOnClick) {
		    theButton.onclick = function() {
			   return outer.forButton(theButton);
		    } ;
		} else {
		    theButton.onclick = function() {
		        if( outer.forButton(theButton) ) {
		           return orgOnClick() ;
		        }
		    };
		}
   };
};
//disable zone
var DisabledZone = new function () {
	var DISABLED_ZONE_ID = "wicket_js_disabled_zone_id";
	var LOADING_MESSAGE_ID = "wicket_js_loading_message_id";
	var showLoadingMessage = null;
	var SHOW_LOADING_AFTER_MINI_SEC = 300;

    var supportFixed = true;
    var isIE = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5.7)
			isIE = true;
	  @elif (@_jscript_version >= 1)
			supportFixed = false;
			isIE = true;
	  @else @*/

	  /*@end
	@*/

	function getDisabledZone() {
		return document.getElementById(DISABLED_ZONE_ID);
	}
	function getLoadingMessage() {
		return document.getElementById(LOADING_MESSAGE_ID);
	}
	this.on = function () {
		var dz = getDisabledZone();
		if (!dz) {
			dz = document.createElement("div");
			dz.setAttribute("id", DISABLED_ZONE_ID);
			dz.style.position = "absolute";
			dz.style.zIndex = "30000";
			dz.style.left = "0px";
			dz.style.top = "0px";
			document.body.appendChild(dz);
			var lm = document.createElement("span");

            if(isIE) { //IE hacks for screen guard
				dz.style.backgroundColor = "white";
				dz.style.filter = "alpha(opacity=0)";
            }

			if (supportFixed) {
				lm.style.position = "fixed";
			} else {
				lm.style.position = "absolute";
			}
			lm.setAttribute("id", LOADING_MESSAGE_ID);
			lm.style.zIndex = "10000";
			lm.style.right = "0px";
			lm.style.top = "0px";
			lm.style.backgroundColor = "#BB0000";
			lm.style.backgroundImage = "url('../images/ajax-loader-ball.gif')";
			lm.style.backgroundPosition = "95%";
			lm.style.backgroundRepeat = "no-repeat";
			// lm.style.fontWeight = "bold";
			lm.style.fontSize = "12px";
			lm.style.padding = "0.2em 2em 0.2em 0.5em";
			lm.style.color = "white";
			lm.style.visibility = "hidden"; // initially hidden
			document.body.appendChild(lm);
			var text = document.createTextNode("Loading...");
			//var text = document.createTextNode("讀取中");
			lm.appendChild(text);
		}
		dz.style.width = document.body.scrollWidth + "px";
		dz.style.height = document.body.scrollHeight + "px";
		dz.style.visibility = "visible";
		//show loading message if loading too long
		showLoadingMessage = window.setTimeout(function () {
			var temp_lm = getLoadingMessage();
			temp_lm.style.visibility = "visible";
		}, SHOW_LOADING_AFTER_MINI_SEC); 		

		//fix position must after width/height change
		correctFixedPosition();
	};
	this.off = function () {
		if (showLoadingMessage) {
			window.clearTimeout(showLoadingMessage);
		}
		var dz = getDisabledZone();
		if (dz) {
			var lm = getLoadingMessage();
			lm.style.visibility = "hidden";
			dz.style.visibility = "hidden";
			dz.style.width = "0px";
			dz.style.height = "0px";
		}
	};
	function correctFixedPosition() {
		if (supportFixed) {
			return;
		}
		var el = getLoadingMessage();
		el.style.top = getScrollPos()["y"];
		if (!el.fixed_position_bound) {
			var delayScroll = function () {
				window.setTimeout(function () {
					el.style.top = getScrollPos()["y"];
				}, 100);
			};
			var oldScroll = window.onscroll;
			if (oldScroll) {
				window.onscroll = function () {
					oldScroll();
					delayScroll();
				};
			} else {
				window.onscroll = function () {
					delayScroll();
				};
			}
			el.fixed_position_bound = true;
		}
	}

	function getScrollPos() {
		if (window.pageYOffset) {
			return {y:window.pageYOffset, x:window.pageXOffset};
		}
		if (document.documentElement && document.documentElement.scrollTop) {
			return {y:document.documentElement.scrollTop, x:document.documentElement.scrollLeft};
		}
		if (document.body) {
			return {y:document.body.scrollTop, x:document.body.scrollLeft};
		}
		return {y:0, x:0};
	}
};
//image hover
var ImageHover = new function() {
	var turnOn = true;
	this.toggle = function(onOff) {
		turnOn = onOff;
	};
	var middleMode = false;
	
	var curretShowId = null;
	function clearCurrent() {
		if(curretShowId) {
			clearTimeout(curretShowId);
		}
	}
	this.init = function(event, fullId, thumbId, isMiddle) {
		var imgThumb = $(thumbId);
		var imgFull = $(fullId) ;
		clearCurrent();
		if(turnOn) {
			curretShowId = setTimeout("ImageHover.show('"+fullId+"')",300);
		}
		if(imgThumb.initialized) {return ;}
		if(isMiddle) {middleMode = isMiddle;}

		imgThumb.full = imgFull;
		imgFull.src=imgFull.alt;
		imgThumb.onmouseout = function(e) {
			clearCurrent();
			this.full.style.display='none';
		};
		imgThumb.onmousemove = function(e) {
			doLocate(e, this.full);
		};
		imgThumb.initialized=true;
	};
	
	this.show = function(imgId) {
		$(imgId).style.display='';
	};

	var offset = 15 ;
	function doLocate(event, imgFull) {
		if(!turnOn) {return;}
		
		if(!event) {event=window.event;}
		var scrollTop = Window.getScrollTop();
		var cusorTop = event.clientY + scrollTop;
		//by default use bottom 
		imgFull.style.top = ( cusorTop + offset )+ 'px';

		if(	Window.getHeight() - event.clientY - imgFull.height < 0 ) { //bottom not enouch
			var topViewSize = event.clientY;
			var bottomViewSize = Window.getHeight() - event.clientY;
			if(middleMode) {
				var adjust = (cusorTop - ( imgFull.height - bottomViewSize )-offset );
				imgFull.style.top = (adjust < scrollTop ? scrollTop+offset : adjust ) + 'px';
			} else {
				//top enough or top space larger than bottom
				if(event.clientY > imgFull.height || topViewSize > bottomViewSize) { 
					imgFull.style.top = ( cusorTop - imgFull.height - offset )+ 'px';
				}
			}
		} 
		if(event.clientX + imgFull.width > Window.getWidth()) {
		    var estimate = (event.clientX - imgFull.width) ;
		    imgFull.style.left= (estimate < 0 ? 100 : estimate ) + 'px';
		} else {
		    imgFull.style.left= (event.clientX + offset) + 'px';
		}
	}
};
var ScrollLock = new function() {
	this.init = function() {
		if(window.beforeScrollLock) return ;
	    window.beforeScrollLock = window.onscroll ;
		window.zxcMaxYScroll=Window.getScrollTop(); 
		window.onscroll = function(){
			var zxcWS=Window.getScrollTop(); 
			if (zxcWS>zxcMaxYScroll){ setTimeout("window.scroll(0,window.zxcMaxYScroll)",250); }
			if (zxcWS<zxcMaxYScroll){ setTimeout("window.scroll(0,window.zxcMaxYScroll)",250); }
		};
	};
	this.release = function() {
		window.onscroll = null; 
	    window.onscroll = window.beforeScrollLock ;
	    window.beforeScrollLock = null;
		window.zxcMaxYScroll = null;
	};
};

var SkypeCheck = new function(){

	var activex = ((navigator.userAgent.indexOf('Win')  != -1) && (navigator.userAgent.indexOf('MSIE') != -1) && (parseInt(navigator.appVersion) >= 4 ));
	var CantDetect = ((navigator.userAgent.indexOf('Safari')  != -1) || (navigator.userAgent.indexOf('Opera')  != -1));
	
	function oopsPopup() {
	    if((navigator.language && navigator.language.indexOf("ja") != -1) || (navigator.systemLanguage && navigator.systemLanguage.indexOf("ja") != -1) || (navigator.userLanguage && navigator.userLanguage.indexOf("ja") != -1)) {
	        var URLtoOpen = "http://download.skype.com/share/skypebuttons/oops/oops_ja.html";
	    } else {
	        var URLtoOpen = "http://download.skype.com/share/skypebuttons/oops/oops.html";
	    }
		var windowName = "oops";
		var popW = 540, popH = 305;
		var scrollB = 'no';
		w = screen.availWidth;
		h = screen.availHeight;
		var leftPos = (w-popW)/2, topPos = (h-popH)/2;
		oopswindow = window.open(URLtoOpen, windowName,'width=' + popW + ',height=' + popH + ',scrollbars=' + scrollB + ',screenx=' +leftPos +',screeny=' +topPos +',top=' +topPos +',left=' +leftPos);
		return false;
	}
	
	if(typeof(detected) == "undefined" && activex) {
	    document.write(
	        ['<script language="VBscript">',
	        'Function isSkypeInstalled()',
	        'on error resume next',
	        'Set oSkype = CreateObject("Skype.Detection")',
	        'isSkypeInstalled = IsObject(oSkype)',
	        'Set oSkype = nothing',
	        'End Function',
	        '</script>'].join("\n")
	    );
	}
	
	function skypeCheck() {
	    if(CantDetect) {
	        return true;
	    } else if(!activex) {
	        var skypeMime = navigator.mimeTypes["application/x-skype"];
	        detected = true;
	        if(typeof(skypeMime) == "object") {
	            return true;
	        } else {
	            return oopsPopup();
	        }
	    } else {
	        if(isSkypeInstalled()) {
	            detected = true;
	            return true;
	        }
	    }
	    
	    detected = true;
	    return oopsPopup();
	}
	
	function loadDetection() {
	    if(document.getElementById && document.getElementsByTagName) {
	        if (window.addEventListener) window.addEventListener('load', addDetection, false);
	        else if (window.attachEvent) window.attachEvent('onload', addDetection);
	    }
	}
	
	function addDetection() {
	    var pageLinks = document.getElementsByTagName("a");
	    for (var i=0; i < pageLinks.length; i++) {
	        if(pageLinks[i].childNodes[0] && pageLinks[i].childNodes[0].src) {
	            if((pageLinks[i].childNodes[0].src.indexOf('download.skype.com\/share\/skypebuttons') != -1 || pageLinks[i].childNodes[0].src.indexOf('mystatus.skype.com') != -1) && (typeof(pageLinks[i].onclick) == "undefined" || pageLinks[i].onclick == null)) {
	                pageLinks[i].onclick = function sChk() { return skypeCheck(); }
	            }
	        }
	    }
	}
			
	this.init =loadDetection;

	this.check = skypeCheck;
};