// JavaScript Document
var isIpad = navigator.userAgent.indexOf("iPad") > 0;
var isIE6 = navigator.userAgent.indexOf("MSIE 6.0") > 0;
jQuery(function(){
	//nav
	Setborder();
	var HoverTimer = null;
	var current = $("#HeaderNav > li[class~='current']");
	if(!isIpad){
		//navigation
		$("#NavProduct, #NavSupport, #NavReseller, #NavAbout").mouseover(function(){
			var _this = $(this)[0];
			var lis = $("#HeaderNav > li");
			for(var i = 0,j = lis.length; i < j; i++){
				if(lis[i] != _this){
					$(lis[i]).find(">ul").css("display","none").end().find("iframe").css("display","none");
				}
				if(lis[i] != _this && lis[i] != current[0]){
					$(lis[i]).removeClass("current");
				}
			}
			if(HoverTimer){ clearTimeout(HoverTimer);}
			if(isIE6){
				$(this).siblings().css("z-index","0");
				$(this).css("z-index","99");
				if(!$(this).find(">iframe")[0]){
					var iframe = document.createElement("iframe");
					iframe.className = "headSubNavMask";
					$(iframe).css({
								  "top": "42px",
								  "width": $(this).find(">ul").width() + "px",
								  "height": $(this).find(">ul").height() + "px"
								});
					$(iframe).insertAfter($(this).find(">ul"));
				}
				$(this).find(">iframe").css("display","inline");
			}
			$(this).addClass("current").find(">ul").css("display","inline");						
		}).mouseout(function(){
			var boolean = ($(this)[0] != current[0]);
			var o = $(this);
			HoverTimer = setTimeout(function(){
				if(boolean){ o.removeClass("current"); }
				o.find("ul").css("display","none").end().find("iframe").css("display","none");
			},350);
		});
		$("#NavProduct li, #NavSupport li, #NavReseller li, #NavAbout li").hover(function(){
			if(isIE6){
				$(this).siblings().css("z-index","0");
				$(this).css("z-index","99");
				if(!$(this).find(">iframe")[0]){
					var iframe = document.createElement("iframe");
					iframe.className = "headSubNavMask";
					
					$(iframe).css({
								  "top": "-1px",
								  "width": $(this).find(">ul").width() + 3 + "px",
								  "height": $(this).find(">ul").height() + 3 + "px"
								});
					$(iframe).insertAfter($(this).find(">ul"));
				}
				$(this).find(">iframe").css("display","inline");
			}
			$(this).addClass("current").find(">ul").css("display","inline");
		},function(){
			$(this).removeClass("current").find(">ul").css("display","none").end().find("iframe").css("display","none");
		});
	}else{
		//navigation
		$("#NavProduct h2, #NavSupport h2, #NavReseller h2, #NavAbout h2").attr("open","close");
		$("#NavProduct h2, #NavSupport h2, #NavReseller h2, #NavAbout h2").click(function(){
			var boolean = ($(this).parent()[0] != current[0]);

			if($(this).attr("open") == "close"){
				$(this).attr("open","open");
				$(this).parent().siblings("li").find(">h2").attr("open","close");
				var lis = $(this).parent().siblings("li");
				for(var i = 0,j = lis.length; i < j; i++){
					if(lis[i] != current[0]){
						$(lis[i]).removeClass("current");
					}
				}
				$(this).parent().siblings("li").find(">ul").css("display","none");
				$(this).parent().addClass("current").find(">ul").css("display","inline");
			}else if($(this).attr("open") == "open"){
				if(boolean){ $(this).parent().removeClass("current"); }
				$(this).attr("open","close");
				$(this).parent().find("ul").css("display","none");
			}
		});	
		$("#NavProduct h2 a, #NavSupport h2 a, #NavReseller h2 a, #NavAbout h2 a").click(function(event){ event.stopPropagation(); });
		$("#NavProduct li, #NavSupport li, #NavReseller li, #NavAbout li").click(function(){
			$(this).siblings("li").removeClass("current").find("ul").css("display","none");
			$(this).addClass("current").find(">ul").css("display","inline");
		});
	}
	//search
	$("#Header input[type='text'], .Search").focus(function(){
		this.style.color = "#222";
		$("#SearchBtn").removeClass().addClass("topSearchBtn_act");
	});
	$("#Header input[type='text'], .Search").blur(function(){
		this.style.color = "#777";
		$("#SearchBtn").removeClass().addClass("topSearchBtn");
	});
	//boxD
	$(".boxDhead a").click(function(){
		if($(this).hasClass("on")){
			$(this).removeClass("on").addClass("off");
			$(this).parents("div[class~='boxD']").find("div[class~='content']").addClass("hidden");
		}else{
			$(this).removeClass("off").addClass("on");
			$(this).parents("div[class~='boxD']").find("div[class~='content']").removeClass("hidden");
		}
	});
	$("#SubscribeEmail").focus(function(){ InputFocus(this, "Enter Email Address"); }).blur(function(){ InputBlur(this, "Enter Email Address"); });
	$("#ProductNumber").focus(function(){ InputFocus(this, "Enter Product Name or Part Number"); }).blur(function(){ InputBlur(this, "Enter Product Name or Part Number"); });
	$("#NewsSearchTop, #NewsSearchBottom").focus(function(){ InputFocus($(this)[0], "keyword..."); }).blur(function(){ InputBlur(this, "keyword..."); });
	//input focus
	$(".input input, .inputLong input, .inputBigLong input").focus(function(){$(this).css("color","#222")});
	$(".input input, .inputLong input, .inputBigLong input").blur(function(){$(this).css("color","#777")});
	//tabs, buttons
	$(".tabsA > span, .tabsB > span, .buttons > ul > li").hover(function(){ if(!$(this).hasClass("current")){ $(this).addClass("hover"); } },function(){ $(this).removeClass("hover") });
	// buttons
	$(".buttonA").hover(function(){ if(!$(this).find(">span").hasClass("current")){ $(this).find(">span").addClass("hover"); } },function(){ $(this).find(">span").removeClass("hover") });
	//ProductRegister Table hover
	$("#ProductRegTable tr:gt(0)").hover(function(){ $(this).css("background","#e0f0ff") },function(){ $(this).css("background","") });
	//Product Detail Overview
	$("#MoveList li:not([class~='360'])").click(function(){
		$(this).siblings("li").removeClass("current").end().addClass("current");
		$("#ZoomDiv img").attr("src", $(this).find("a").attr("href").replace("#", ""));
	})
	$("#MoveList li").hover(function(){$(this).addClass("highlight");},function(){$(this).removeClass("highlight");});
	$("#MoveList li[class~='video']").click(function(){
		$("#ZoomDiv").html("<img src=\"" + $(this).find("a").attr("href").replace("#","") + "\" />");
	});
	//Filter
	$("#Filter .filterLinks a").click(function(){
		var allObj = $(this).parent().parent().siblings("span").find("a");
		if($(this).hasClass("current")) {
			$(this).removeClass("current");
			allObj.addClass("current");
		}else{
			$(this).addClass("current").parent().siblings("span").find("a").removeClass("current");
			allObj.removeClass("current");
		}
	});
	$("#Filter .all a").click(function(){
		var aObj = $(this).parent().siblings("div").find("a");
		if(!$(this).hasClass("current")) {
			$(this).addClass("current");
			aObj.removeClass("current");
		}
	});
	//datepicker
	$(".datepicker input").datepicker({
			dateFormat: "mm/dd/yy",
			showOn: "button",
			buttonImage: "/Images/icon/datepicker.jpg",
			buttonImageOnly: true
		}).click(function(){ $(this).next().click() });
});
function Setborder(){
	var lis = $("#HeaderNav > li");
	for(var i = 0; i < lis.length; i++){ 
		if($(lis[i]).hasClass("current")){ 
			if($(lis[i]).next()[0]) { $(lis[i]).next().css("background-position","-1px top"); }
			else{ $(lis[i]).parent().next().css("background-position","-1px top"); }
		}
		lis[i].setAttribute("num", i);
		lis[i].style.zIndex = 10 - i;
	}
}
function InputFocus(_this, string ) {
	var string = string.toLowerCase();
	if( _this.value == "" || _this.value.toLowerCase() == string ) _this.value = "";
}
function InputBlur(_this, string ) {
	_this.value = ( _this.value == "") ? (_this.value = string) : _this.value;
}
var MoveControl = {
	iniUnit : function(wrapId, id, dftNum, intStep) {
		var obj = $(id).find(".moveUnit");
		var objNum = obj.length;
		if (objNum > dftNum) {
			MoveControl.addEvent(wrapId, id, dftNum, "next", intStep)
		}
	},
	addEvent : function(wrapId, id, dftNum, strPN, intStep) {
		var strTitle;
		var objClick = $(wrapId + " ." + strPN + "View");
		var objNumHref = objClick.find("a").length;
		objClick.removeClass(strPN + "Stop");
		
		if (objNumHref != 0)
			objClick = objClick.find("a");
		
		strTitle =  objClick.text();
		objClick.attr("title", strTitle);
		objClick.click(function(){
			MoveControl.moveUnit(wrapId, id, dftNum, strPN, intStep);
			return false;
		});
	},
	moveUnit : function(wrapId, id, dftNum, strPN, intStep) {
		if ($(wrapId).data("currSubName") != null)
			id = $(wrapId).data("currSubName");
		var n = $(id).data("nowUnit");
		if (isNaN(n) || n == null) n = 0;
		
		var objClick = $(wrapId + " ." + strPN + "View");
		var objNumHref = objClick.find("a").length;
		objClick.removeClass(strPN + "Stop");
		if (objNumHref != 0)
			objClick = objClick.find("a");
	
		var obj = $(id).find(".moveUnit");
		var objNum = obj.length;
		
		if (strPN == "next") {
			for (var i = 0; i < intStep; i++) {
				$(obj[n]).hide("fast");
				if (n == 0) {
					$(wrapId + " .prevView").removeClass("prevStop");
					MoveControl.addEvent(wrapId, id, dftNum, "prev", intStep);
				}
				n++;
				if (n == (objNum - dftNum)) {
					n = objNum - dftNum;
					$(wrapId + " .nextView").addClass("nextStop");
					objClick.unbind("click").attr("title", "");
					break;
				}
			}
		}else {
			for (var i = 0; i < intStep; i++) {
				n--;
				if (n >= 0){
					$(obj[n]).show("fast");
				}
				if (n + dftNum + intStep == objNum) {
					$(wrapId + " .nextView").removeClass("nextStop");
					MoveControl.addEvent(wrapId, id, dftNum, "next", intStep);
					if (n < 0) {
						n = 0;
						break;
					}
				}
				if (n <= 0) {
					$(wrapId + " .prevView").addClass("prevStop");
					objClick.unbind("click").attr("title", "");
					if (i + 1 == intStep) n = 0;
				}
			}
		}
		
		$(id).data("nowUnit", n);
	}
}
