// Stage Electrics Javascript
// Global Variables
var iepc, firefox, ns7, ns8, mozilla, safari, OS;
function fnBrowser() {
    var ua = navigator.userAgent.toLowerCase();

    iepc = (((ua.indexOf("msie 7") != -1) || (ua.indexOf("msie 6") != -1) || (ua.indexOf("msie 5.5") != -1)) && (ua.indexOf("windows") != -1) && (ua.indexOf("opera") == -1)) ? true : false;
    firefox = ua.indexOf("firefox") != -1 ? true : false; // pc or mac
    ns7 = ua.indexOf("netscape/7") != -1 ? true : false; // firefox mode
    ns8 = ua.indexOf("netscape/8") != -1 ? true : false; // firefox mode
    mozilla = (ua.indexOf("gecko") != -1) && (ua.indexOf("netscape") == -1) ? true : false;
    safari = ua.indexOf("safari") != -1 ? true : false;
    OS = ua.indexOf("windows") != -1 ? "PC" : "MAC";
    opera = ua.indexOf("opera/9") != -1 ? true : false;
    if (iepc || firefox || ns7 || ns8 || mozilla || safari || opera) {
        return true;
    } else {
        return false;
    }
}
fnBrowser(); // run on load;

// Flash detect. boolean true/false if installed and version number accessed via flash.version
var flash = new Object();
flash.installed = false;

if (navigator.plugins && navigator.plugins.length) {
    for (x = 0; x < navigator.plugins.length; x++) {

        if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
            flash.installed = true;
            flash.version = eval(navigator.plugins[x].description.split('Shockwave Flash ')[1].split('.')[0]);
            break;
        }
    }
}
else if (window.ActiveXObject) {
    for (x = 2; x <= 20; x++) {
        try {
            oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
            if (oFlash) {
                flash.installed = true;
                flash.version = x;
            }
        }
        catch (e) { }
    }
}

/********************************************************************************

Name: 				Flash embed
Description:		Writeout flash code. Fixes Eolas update for IE as well.
@param	swf			String path to swf file
@param	width		Number value of movie width
@param	height		Number value of movie height

*********************************************************************************/
flash.insert = function(swf, width, height, flashVer, nonFlashId) {

    if (!flashVer) { minVer = 6; }
    else { minVer = flashVer; }
    if (!nonFlashId) { nonFlashId = "nonFlashContent"; }

    // Handle errors
    var errMsg = "";
    if (!fnBrowser()) { // NOT supported browser
        errMsg += '<p class="err">Unfortunately, this website cannot be viewed by your browser. Please go to <a href="http://www.getfirefox.com">www.getfirefox.com</a> or <a href="http://www.microsoft.com/windows/ie/">www.microsoft.com/</a> to download a recommended browser.</p>';
    }
    if (!flash.installed) { // NOT flash
        errMsg += '<p class="err">This website requires the adobe&trade; Flash player. Go to the adobe website to install the <a href="http://www.adobe.com/products/flashplayer/">latest version of flash</a></p>';
    }

    swf = swf.replace(/\./g, "%2E") // encode periods as %2e

    if (flash.installed && fnBrowser() && flash.version >= minVer) {
        document.write('<embed src="' + swf + '" menu="false" width="' + width + '" height="' + height + '" scale="noscale" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" salign="T" />');
        fnHide(nonFlashId);
    }
}

function fnHide(id) {
    if (!document.getElementById(id)) { return false };
    var oTarg = document.getElementById(id).style;
    oTarg.position = "absolute";
    oTarg.top = "-5000px";
    oTarg.left = "0";
}

function fnShow(id) {
    if (!document.getElementById(id)) { return false };
    var oTarg = document.getElementById(id).style;
    oTarg.position = "static";
    oTarg.top = "";
    oTarg.left = "";
}

// On load - set up menu hovers to add drop shadow to next element
// Jquery syntax
$(function() {
    // add shadow class
    // on hover and rollout
    $("#mainNav li a").not(".selected").hover(function() {
        $(this).parent().next().addClass("adjacent");
    }, function() {
        $(this).parent().next().removeClass("adjacent");
    });
    // on keyboard nav on
    $("#mainNav li a").not(".selected").focus(function() {
        $(this).parent().next().addClass("adjacent");
    });
    // on keyboard nav off
    $("#mainNav li a").not(".selected").blur(function() {
        $(this).parent().next().removeClass("adjacent");
    });
});


/* Print */
function fnPrint() {
    window.print();
}

/********************************************************************************

Name:     fnPopup
Description: Standard popup, but takes target from href on link.  Scrollbars are 
hidden unless screen resolution is less than 1024 x 768, when scrollbars are present.
 
*********************************************************************************/
function fnPopup(targ, w, h) {
    var width = 450;
    var height = 425;

    if (typeof (w) != "undefined") {
        width = w;
    }

    if (typeof (h) != "undefined") {
        height = h;
    }

    var obj = targ.href;
    var sScreenX = screen.width;
    var sScreenY = screen.height;
    var x = sScreenX - width;
    var y = sScreenY - height;
    var winLeft = x / 2;
    var winTop = y / 2;

    window.open(obj, "", "width=" + width + ",height=" + height + ",scrollbars=yes,left=" + winLeft + ",top=" + winTop + ",resizable=yes, status=no");

    return false;
}


/* Extra Images on product detail */

$(function() {
    var imgLength = $('.imagesWrap .img').length;
    var wrapper = $('.imagesWrap');
    var fChild = $('.imagesWrap .img:first');
    var lChild = $('.imagesWrap .img:last');

    if (imgLength > 1) {
        $('.imagesWrap .img:first').addClass('active');
        wrapper.append('<div class="hozDots"></div>');
        wrapper.append('<div class="imgNav"><a href="#" class="prev">Previous Image</a> <a href="#" class="next">Next Image</a> </div> <div class="clr"></div>');



        $('.imagesWrap .prev').hide();

        $('.imagesWrap .next').click(
			function() {

			    $('.imagesWrap .active').hide().removeClass('active').next('.img').addClass('active').show();

			    if (lChild.hasClass('active')) {
			        $(this).hide();
			    };

			    if (fChild.hasClass('active')) {
			        $('.imagesWrap .prev').hide();
			    } else {
			        $('.imagesWrap .prev').show();
			    };

			    return false;

			});

        $('.imagesWrap .prev').click(
			function() {

			    $('.imagesWrap .active').hide().removeClass('active').prev('.img').addClass('active').show();




			    if (fChild.hasClass('active')) {
			        $(this).hide();
			    };

			    if (lChild.hasClass('active')) {
			        $('.imagesWrap .next').hide();
			    } else {
			        $('.imagesWrap .next').show();
			    };

			    return false;

			});

    };
});

// Javascript only functionality
$(document).ready(function() {
    $(".jsHide").css("display", "block");
    $(".jsHideInline").css("display", "inline");

    $("#contentSearch input[name='k']").val("Search our site");
    $("#contentSearch input[name='k']").focus(function() {
        $("#contentSearch input[name='k']").val("");
    });
});

function fnAjaxAddToBasket(form) {
    $("#" + form.id + " > input[type=\"image\"]").attr("src", $("#" + form.id + " > input[type=\"image\"]").attr("src").replace("btnAddToBasket", "btnAddingItem"));

    var productCode = form["product"].value;
    var quantity = form["quantity"].value;
    var parentCode = "";
    var basketType = "";
    if (form["parent"])
        parentCode = form["parent"].value;
    if (form["basketType"])
        basketType = form["basketType"].value;

    $.post("/stageElectricsAssets/ajax/ajaxBasket.aspx", { basket: "add", product: productCode, quantity: quantity, parent: parentCode, basketType: basketType }, function(data) {
        var response = eval('(' + data + ')');
        if (response.itemCount != "0") {
            $("#bsItemsText").html('<strong>' + response.itemText + '</strong>');
            $("#bsItemsText").attr("title", response.itemText);
            $("#bsTopItemsText").text(response.itemText);
            $("#bsItemsTotal").text(response.totalText);
            if (response.type == "Hire") {
                $("#myBasketWrap h3").text("Hire Basket Summary");
                $("#myBasketWrap .subTotal").css("display", "none");
                $("#myBaskBot a").attr("class", "Hire");
            }
            else {
                $("#myBasketWrap h3").text("Sales Basket Summary");
            }
            $(".basketHolder").attr("style", "");
            $("#nMyAcWrap").attr("class", "open");

        }
        $("#" + form.id + " > input[type=\"image\"]").attr("src", $("#" + form.id + " > input[type=\"image\"]").attr("src").replace("btnAddingItem", "btnAddToBasket"));
    });
}

function fnAjaxMultipleAddToBasket(form) {
    $(".addAll").attr("src", "/stageElectricsAssets/images/btnAddingItems.gif");

    var postValues = new Object();
    var productCodes = $("input[name=productCodes]").map(function() {
        postValues["qty" + $(this).val()] = $("input[name=qty" + $(this).val() + "]").val();
        return $(this).val();
    }).get().join(",");
    postValues.basket = "multipleadd2";
    postValues.productCodes = productCodes;
    postValues.basketType = form["basketType"].value;
    $.post("/stageElectricsAssets/ajax/ajaxBasket.aspx", postValues, function(data) {
        var response = eval('(' + data + ')');
        $("#bsItemsText").html('<strong>' + response.itemText + '</strong>');
        $("#bsItemsText").attr("title", response.itemText);
        $("#bsTopItemsText").text(response.itemText);
        $("#bsItemsTotal").text(response.totalText);
        if (response.type == "Hire") {
            $("#myBasketWrap h3").text("Hire Basket Summary");
            $("#myBasketWrap .subTotal").css("display", "none");
            $("#myBaskBot a").attr("class", "Hire");
        }
        else {
            $("#myBasketWrap h3").text("Sales Basket Summary");
        }
        $(".basketHolder").attr("style", "");
        $("#nMyAcWrap").attr("class", "open");
        $(".addAll").attr("src", "/stageElectricsAssets/images/addAll.gif");
    });

}

function fnAjaxGetAccessories(form) {

    var productCode = form["product"].value;
    var quantity = form["quantity"].value;
    $.get("/stageElectricsAssets/ajax/accessories.aspx", { p: productCode, q: quantity, s: "" }, function(data) {
        $("body").append(data);
        $("#overlayop").css({ height: $(document).height() + "px", opacity: 0.5 });
        if (document.documentElement.scrollTop) {
            scrollY = document.documentElement.scrollTop;
        } else {
            scrollY = window.pageYOffset;
        }
        ypos = scrollY == undefined ? 100 : parseFloat(scrollY) + 100;
        $("#accessoriesPanel").css({ top: ypos + "px" }).fadeIn();
        $("select").hide();
    });

}

//20091207a DCB
function navigateTo(strUrl) {
    document.location = strUrl;

}