var menuObj = null;
var timer;
var prodID = 0;
var openLB = '';

$( function() {
	$("li.mainnav").hover(
		function() {
			if (menuObj != null && menuObj.attr("id") != $(this).attr("id")) {
				removeHover();
				menuObj = null;
			} else if (menuObj != null) {
			    clearTimeout(timer);
			}
			
			$(this).find("div.nav-popup").slideDown(300);
			rollover($(this).find("img"));
		},
		function() {
			menuObj = $(this);
			timer = setTimeout("checkHover()", 400);
		});
		
    $("#showroom_link").click(
        function() {
            $("#search-box").animate( {marginLeft: "629px"} , 200, "linear", function() { $("#showroom_popup").show(); });
        });
		
	$("#utility-nav img, #logo img, #tabs-header img").hover( function(){rollover($(this));}, function(){rollout($(this));} );
    $(".tab").click( function() {tabClick($(this));} );

    $(".faq-link").click(
        function() {
            if ($(this).parent().hasClass("question")) $(this).parent().removeClass("question").addClass("question-act");
            else $(this).parent().removeClass("question-act").addClass("question");            
            $(this).parent().siblings(":first").toggle();
        });
        
    $(".alertlink").click(
        function() {
            $(this).parent().siblings(":first").toggle();
        
        });
     
    doSifr();   
    if (prodID != 0) tabInit();
    
    
    if (openLB != '') openLightbox(openLB);
    
    $("a.msnav").hover(
        function() {
            $(this).find("img").attr("src", "/common/images/microsite/" + $(this).attr("rel") + "-on.gif");            
        },
        function() {
            $(this).find("img").attr("src", "/common/images/microsite/" + $(this).attr("rel") + ".gif");        
        });       
});

function checkHover() {	
	if (menuObj) removeHover();
}

function removeHover() {
    menuObj.find("div.nav-popup").queue(
        function() { 
            $(this).hide(); 
            $(this).dequeue(); 
        });
        
	rollout(menuObj.find("img"));
}

function rollover(img) {
    if (img.attr("src").indexOf("-act") != -1) {
        img.attr("src", img.attr("src").replace("-act", "-on")).addClass("active");
    } else if (img.attr("src").indexOf("-on") == -1) {
        img.attr("src", img.attr("src").replace(".gif", "-on.gif"));
    }
}

function rollout(img) {
    if (img.attr("src").indexOf("-on") != -1) {
        if (img.hasClass("active")) {
            img.attr("src", img.attr("src").replace("-on", "-act")).removeClass("active");
        } else {
            img.attr("src", img.attr("src").replace("-on.gif", ".gif"));
        }
    }
}

function tabInit() {
    var cookval = findCookie();
    if (cookval != null) {  
        var values = cookval.split("_");
        if (values.length > 1 && values[0] == prodID && values[1] > 0 && values[1] < 8) {
            var x = $("#tab_" + values[1] + "_" + values[0]);
            tabClick(x);
        }
    }
}

function findCookie() {
    var nameEQ = "szproducttab=";
	var ca = document.cookie.split(';');	
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function tabClick(tab) {
    $(".detail-tab").css("position", "absolute").css("left", "-5000px");
    $(".tab").each(
        function() {
            if ($(this).attr("src").indexOf("-act") != -1)
                $(this).attr("src", $(this).attr("src").replace("-act", ""));
        });
    
    if (tab.attr("src").indexOf("-on") != -1) {
        tab.attr("src", tab.attr("src").replace("-on", "-act"));
    } else {
        tab.attr("src", tab.attr("src").replace(".gif", "-act.gif"));    
    }
               
    var args = tab.attr("id").split("_");          
    $("#tab_" + args[1] + "_data").css("position", "static");
    //if (args[1] == 6) $("#tab_6a_data").css("position", "static");            
    document.cookie = 'szproducttab=' + args[2] + '_' + args[1] + '; expires=Fri, 2 Jan 2099 02:47:11 UTC; path=/';
}

function doSifr() {
    if (sIFR != null && sIFR.replaceElement != null) {
        sIFR.replaceElement("h1.replace-title",named({sFlashSrc: "/common/flash/helveticaneueplain.swf", sColor:"#333333", sWmode:"transparent"}));
        sIFR.replaceElement("h1.replace-title-white",named({sFlashSrc: "/common/flash/helveticaneueplain.swf", sColor:"#ffffff", sWmode:"transparent"}));
        sIFR.replaceElement("h1.replace-title-green",named({sFlashSrc: "/common/flash/helveticaneueplain.swf", sColor:"#929d74", sWmode:"transparent"}));
        sIFR.replaceElement("h1.replace-title-ltgreen",named({sFlashSrc: "/common/flash/helveticaneueplain.swf", sColor:"#b0bc8b", sWmode:"transparent"}));
        sIFR.replaceElement("h1.replace-title-blue",named({sFlashSrc: "/common/flash/helveticaneueplain.swf", sColor:"#6699cc", sWmode:"transparent"}));
        sIFR.replaceElement("h1.replace-title-orange",named({sFlashSrc: "/common/flash/helveticaneueplain.swf", sColor:"#cc6633", sWmode:"transparent"}));
        
        sIFR.replaceElement("h2.replace-title",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#333333", sWmode:"transparent"}));
        sIFR.replaceElement("h2.replace-title-white",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#ffffff", sWmode:"transparent"}));
        sIFR.replaceElement("h2.replace-title-green",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#929d74", sWmode:"transparent"}));
        sIFR.replaceElement("h2.replace-title-blue",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#6699cc", sWmode:"transparent"}));
        sIFR.replaceElement("h2.replace-title-orange",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#cc6633", sWmode:"transparent"}));
        sIFR.replaceElement("h2.replace-title-gray",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#666666", sWmode:"transparent"}));
        sIFR.replaceElement("h3.replace-title-gray-sml",named({sFlashSrc: "/common/flash/helveticaneuebold.swf", sColor:"#666666", sWmode:"transparent"}));
    }
}

function clickButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt){ 
        if (evt.keyCode == 13){ 
            bt.click(); 
            return false; 
        } 
    } 
}


//detail and flash functions
var isZoomView = false;
function setFrame(frame) {
    currentFrame = frame;	
	if (isZoomView) {
		if (document.getElementById(zoomName)) S7Config.setFlashParam(zoomName, "currentFrame", frame);
		else loadZoomView();
	} else {
		if (document.getElementById(imageName)) S7Config.setFlashParam(imageName, "currentFrame", frame);
		else loadImageView();
	}
}

function loadZoomView() {
	changeSize(935,437, true);
	zoomView.addVariable('initialFrame',currentFrame);
	zoomView.write("zoom");
	isZoomView = true;
}
function loadImageView() {
	changeSize(630,310, true);
	imageView.addVariable('initialFrame',currentFrame);
	imageView.write("zoom");
	isZoomView = false;
}

function loadVideo(url) { 
    var videoPlayer = document.getElementById("player");
    if (videoPlayer) {
        videoPlayer.load(url);
    } else {    
        vid.addVariable('url', url);
        vid.write("zoom")
        window.player = document.forms[0].player;
        changeSize(630, 400);
    }
	isZoomView = false;
    selectControllerBtn(url);
}

function changeSize(_w, _h, moveRightNav) {
    var heightDiff = _h - 310;
    var widthDiff = _w - 630;
    if (moveRightNav == undefined) moveRightNav = false;

    if ($("#productdetails").length != 0) {
        if (heightDiff > 0) {
            $("#productdetails").animate( { marginTop: heightDiff }, 500, "linear"); 
        } else {
	        $("#productdetails").animate( { marginTop: 0 }, 500, "linear"); 	
	    }        
	}
    if ($("#right-nav").length != 0) {
	    if (widthDiff > 0 && moveRightNav) {
	        $("#right-nav").animate( { paddingTop: "457px" }, 500, "linear");
	    } else {
	        $("#right-nav").animate( { paddingTop: "0" }, 500, "linear");
        }   
    }   
    $("#zoom").animate( { height: _h, width: _w }, 500, "linear");    
}


function selectControllerBtn(info) {
    var controller = document.getElementById("zoomCntrl");
    if (controller) controller.selectBtn(info);
}

function trackEvent(eventString) {
    urchinTracker(eventString);
}

function validateForm(submitBtn) {

	var hasError = false;
	var container = submitBtn.parents(".form-container");
	
	var x = container.find("select");
	var y = container.find("select.required");
	var z = container.find("input.required, select.required");
	
	container.find("input.required, select.required").each(
		function() {
			var label = $(this).prevAll("span:first");
			
			if ($(this).val().length == 0) {
				if (!label.hasClass("error")) {
					label.addClass("error");
				}
				hasError = true;		
			} else {
				label.removeClass("error");
			}
		});
	if (hasError) {
	    $("#form_error").show();
	    return false;
	}
	
	$("#form_error").hide();
	return true;
}

function showModalMovie(videoURL) {
    modalVid.addVariable('url', videoURL);
    modalVid.write("modal_zoom")
    window.mplayer = document.forms[0].mplayer;
    document.getElementById(mainPrefix + "BlankBtn").click();
}
function killModalMovie() {
	var obj = document.getElementById("modal_zoom")
	if (null != obj) {
		obj.innerHTML = '';
	}
}

var emailRegEx= "^[a-zA-Z0-9][\w'\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";

function isDefined(property) {return (typeof property != 'undefined');}