﻿//******************
// Digiscribe/Digi Web Development
// StandardMaster.js - April 8, 2009
//
// Local functions for the Standard.Master template page
//
//******************

//Global variables
var timeout = 500;
var closetimer = 0;
var ddmenuitem = null;

// close layer when click-out
document.onclick = mclose;

//Show arrow on mouse rollover of menu item
function ShowArrow(id) {
    var imgArrow = document.getElementById(id);
    imgArrow.style.visibility = 'visible';
}

//Hide arrow on mouse rollout of menu item
function HideArrow(id) {
    var imgArrow = document.getElementById(id);
    imgArrow.style.visibility = 'hidden';
}

//Reset the menu image to its default off image
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr;
    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) 
        x.src = x.oSrc;
}

//Preload the menu images
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}
MM_preloadImages(ImgFindYourHomeDrop, ImgSmartDesignDrop, ImgContactUsDrop, ImgStepsDrop, ImgFindYourHomeBg, ImgSmartDesignBg, ImgContactUsBg, ImgStepsBg);

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

//On mouse over of a menu item this function swaps the image with it's on image
function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

// open hidden layer
function mopen(id) {
    //Close open SD callouts
    if (typeof (closeSDWindows) == "function") {
        closeSDWindows();
    }
    // cancel close timer
    mcancelclosetime();

    // close old layer
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';


    // get new layer and show it
    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.visibility = 'visible';

    //if any other menus were selected deselect them
    if (id != "bzr_findHome_Menu") {
        $("#" + ImgFindYourHomeID).attr("src", ImgFindYourHomeOff);
        //        imgFindYourHome =  document.getElementById(ImgFindYourHomeID);
        //        imgFindYourHome.src = ImgFindYourHomeOff;
    }
    if (id != "bzr_smartdesign_menu") {
        $("#" + ImgSmartDesignID).attr("src", ImgSmartDesignOff);
        //        imgSmartDesign = document.getElementById(ImgSmartDesignID);
        //        imgSmartDesign.src = ImgSmartDesignOff;
    }
    if (id != "bzr_stepsToOwnership_menu") {
        $("#" + ImgStepsID).attr("src", ImgStepsOff);
        //        imgSteps = document.getElementById(ImgStepsID);
        //        imgSteps.src = ImgStepsOff;
    }
    if (id != "bzr_contactUs_Menu") {
        $("#" + ImgContactUsID).attr("src", ImgContactUsOff);
        //        imgContactUs = document.getElementById(ImgContactUsID);
        //        imgContactUs.src = ImgContactUsOff;
    }
}

// close showed layer
function mclose() {
    if (ddmenuitem) { ddmenuitem.style.visibility = 'hidden'; MM_swapImgRestore(); }
}

// go close timer
function mclosetime() {
    closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

// Keep the menu itemm in a selected state when the menu image is clicked
function emptyClick(e) {
    if (!e) var e = window.event;

    e.cancelBubble = true;

    if (e.stopPropagation) e.stopPropagation();
}


// Dynamic language switcher via SetLanguage.ashx
function SwitchLanguage() {
    var url = setLanguageUrl + "?lang=" + langCode + "&t=" + escape(document.location);
    document.location = url;
}


//Global search handler
function HandleGlobalSearch() {


    var criteria = document.getElementById("txtSearchBeazer").value;
    if (criteria.trim() == "")
        alert(siteSearchBlankMessage);
    else document.location = siteSearchUrl + "?q=" + escape(criteria);
}

function HandleGlobalSearchTextbox(e) {
    var keycode;

    if (window.event) {
        keycode = window.event.keyCode;
    }
    else if (e) {
        keycode = e.which;
    }

    if (keycode == 13) {
        HandleGlobalSearch();
    }
}

//Unbind events from document on unload
$(window).unload(function() {
    $(document).unbind("click");
    document.MM_sr = null;
    document.MM_p = null;
});

//Attach keyup event to textbox
$(document).ready(function() {
    $("#txtSearchBeazer").keypress(function(event) {
        if (event.keyCode == 13) {
            HandleGlobalSearch();
            return false;
        }
    });
});


/* Ad server support */

function GetRnd() {
    return Math.random() * 1000;
}


function ShowAd(locationCode, locationParamName, width, height, align, adServerUrl) {

    var args = ParseQueryString();

    var locationParamValue = "";
    if (locationParamName == "_page_") {
        var locationInfo = window.location.pathname.split('/');
        locationParamValue = locationInfo[locationInfo.length - 1];
    }
    else {
        locationParamValue = args[locationParamName];
    }

    var ad_html = "<iframe id='ifrAd" + locationCode + "' scrolling='no' margintop='0' marginleft='0' style='visibility:hidden;' src='" + adServerUrl + "?LocationCode=" + locationCode + "&LocationParamValue=" + locationParamValue + "&Width=" + width + "&Height=" + height + "&R=" + GetRnd() + "' frameborder='0' border='0' width='" + width + "' height='" + height + "&Align=" + align + "' align='" + align + "'></iframe>";
    document.write(ad_html);

}

function ToggleAdVisibility(isVisible, locationCode) {

    var ifrAd = document.getElementById("ifrAd" + locationCode);


    if (ifrAd == null) {
        return;
    }

    if (isVisible) {
        ifrAd.style.visibility = "visible";
    }
    else {
        ifrAd.style.visibility = "hidden";
    }
}

function ParseQueryString(str) {
    str = str ? str : location.search;

    var query = str.charAt(0) == '?' ? str.substring(1) : str;

    var args = new Object();

    if (query) {
        var fields = query.split('&amp;');

        for (var f = 0; f < fields.length; f++) {

            var field = fields[f].split('=');

            args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
        }

    }

    return args;
}


String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
