﻿$.fn.search1 = function() {
    return this.focus(function() {
    if (this.value == "Input Search Text") {
            this.value = "";
        }
    }).blur(function() {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
};

$.fn.search2 = function() {
    return this.focus(function() {
    if (this.value == "Enter Your Email for Upcoming Promotions") {
            this.value = "";
        }
    }).blur(function() {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
};


$('#edit-search-theme-form-1').search1();



function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function preLoader() {

    //preload images
    jQuery.preloadImages = function() {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }

    $.preloadImages("/images/featureEdge.jpg", "/images/featureNetbook.jpg", "/images/featureXRay.jpg", "/images/featureSwitch.jpg");
}


function referralPromo() {

    var referrer = document.referrer;
    var var1 = new RegExp("bing.com");
    var link = referrer;

    if (var1.test(link) == true) {
        $(".bodyCont").before('<div class="specialTop">You found us on <b>Bing.com</b> This qualifies you for a <b>15% discount</b>. Use code <b class="blue">BING</b> at checkout. Welcome to Brenthaven.</div>');
        $(".specialTop").slideDown();
    }

}


//creates drop down menu
var menuItems = new Array('Protection', 'Create', 'Macbook', 'PC', 'Netbook', 'XRay', 'Size');
var menuItemAmount = menuItems.length;

function menuDropDown(i) {
    $("#dropMenu" + menuItems[i]).hoverIntent(
				function() {
				    $(".dropMenu" + menuItems[i]).css("opacity", .95);
				    $(".dropMenu" + menuItems[i]).slideDown("fast");
				    $(".menuBtn" + menuItems[i]).attr("src", "/images/menuBtn" + menuItems[i] + "-hover.png");
				},
			    function() {
			        $(".dropMenu" + menuItems[i]).slideUp("fast");
			        $(".menuBtn" + menuItems[i]).attr("src", "/images/menuBtn" + menuItems[i] + ".png");
			    })
}


//swap background images in the buckets

function swapXRay() {

    var xray = Math.floor((Math.random() * 2));
    var xraySrc = "/images/bucketXRay-" + xray + ".jpg";
    $('#homeBucket1').css('backgroundImage', 'url(' + xraySrc + ')');
}

function swapCreate() {
    var create = Math.floor((Math.random() * 4));
    var createSrc = "/images/bucketCreate-" + create + ".jpg";
    $('#homeBucket2').css('backgroundImage', 'url(' + createSrc + ')');
}


//sets functions for the feature menu

function featureMenu() {

    $(".btn1").click(function() {
        clearShow();
        $("#feature1").css("display", "block");
    });
    $(".btn2").click(function() {
        clearShow();
        $("#feature2").css("display", "block");
    });
    $(".btn3").click(function() {
        clearShow();
        $("#feature3").css("display", "block");
    });
    $(".btn4").click(function() {
        clearShow();
        $("#feature4").css("display", "block");
    });
	$(".btn5").click(function() {
        clearShow();
        $("#feature5").css("display", "block");
    });
	$(".btn6").click(function() {
        clearShow();
        $("#feature6").css("display", "block");
    });
}




//slideshow functionality on features

var rotateInterval = 9000; // seconds between slide rotation
var m;
var n;
var slideShow = 1; //enables slideshow- this disables when a feature menu item is clicked


function next(m) {

    if (slideShow == 1) {

        $("#feature" + m).fadeOut("slow");
        m = (m + 1);
        $("#feature" + m).fadeIn("slow");
        n = m;

        if (n > 5) {
            $("#feature" + n).fadeOut("slow");
            next(0);
        }
        else {
            a = setTimeout("next(n)", rotateInterval);
        }
    }
}

// clears the slideshow function and eliminates content for clean population when menu item is clicked.
function clearShow() {
    slideShow = 0;
    $("#feature1").css("display", "none");
    $("#feature2").css("display", "none");
    $("#feature3").css("display", "none");
    $("#feature4").css("display", "none");
	$("#feature5").css("display", "none");
}


function productImages() {
    var percent = "100%"
    
   

    //if javascript is enabled, create next and previous buttons under image
    $(".main-product-image").append("<div class=\"imageNext\">Next</div><div class=\"imagePrevious\">Previous</div>");
    $(".more-product-images").css("display", "none");

    //create variables to run through all images
    var imagePath = $(".imagecache-product").attr("src");
    var imageItems = new Array('-Hero.jpg', '-Front.jpg', '-Left.jpg', '-Back.jpg', '-Right.jpg');
    var imageAmount = imageItems.length;
    var imageNumber = 0;

    //create preloader of all images
    jQuery.preloadImages = function() {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }
    $.preloadImages(imagePath.replace('-Hero.jpg', '-Front.jpg'), imagePath.replace('-Hero.jpg', '-Left.jpg'), imagePath.replace('-Hero.jpg', '-Back.jpg'), imagePath.replace('-Hero.jpg', '-Right.jpg'), imagePath);

    //create click functions for next and previous buttons
    $(".imageNext").click(function() {
        imageNumber = (imageNumber + 1);
        if (imageNumber == 5) {
            imageNumber = 0;
        }
        $(".imagecache-product").css("float", "right");
        $(".imagecache-product").attr("src", imagePath.replace('-Hero.jpg', imageItems[imageNumber]));
        $(".imagecache-product").attr("height", " ");
        $(".imagecache-product").attr("width", " ");
        $(".imagecache-product").css("height", "auto");
        $(".imagecache-product").css("width", "auto");

        $(".imagecache-product").css("float", "left");
    });

    $(".imagePrevious").click(function() {
        imageNumber = (imageNumber - 1);
        if (imageNumber == -1) {
            imageNumber = 4;
        }
        $(".imagecache-product").css("float", "right");
        $(".imagecache-product").attr("src", imagePath.replace('-Hero.jpg', imageItems[(imageNumber)]));
        $(".imagecache-product").attr("height", " ");
        $(".imagecache-product").attr("width", " ");
        $(".imagecache-product").css("height", percent);
        $(".imagecache-product").css("width", percent);
        $(".imagecache-product").css("float", "left");
    });

}


function checkoutPage() {

    //this displays opt in value on checkout page
    $("#edit-panes-customer-primary-email-wrapper").append(" <input id=\"addMe\" class=\"addMe\" type=\"checkbox\"></input> Do not send me marketing and promotional e-mail. ");
    var addMeMessage = "Do not send me marketing and promotional e-mail.";
    $(".addMe").click(function() {
        var addMeChecked = $(".addMe").attr("checked");
        var commentsString = $("#edit-panes-comments-comments").val();

        if (addMeChecked == true) {
            $("#edit-panes-comments-comments").val(addMeMessage + commentsString);
        }
        if (addMeChecked == false) {
            $("#edit-panes-comments-comments").val(commentsString.replace(addMeMessage, ''));
        }
    });

    //this replaces user login option under email address
    var customerLeaveThis = $("#customer-pane .description").html();
    var customerRemoveThis = " or <a href=\"/user/login\">click here</a> to login with an existing account and return to checkout.";
    $(".description").html(customerLeaveThis.replace(customerRemoveThis, '.'));

}

function cyc() {

    $("#edit-attributes-4-23-wrapper").attr("title", "Our patented CORE™ protection system provides 6 sides of foam for maximum drop protection");
    $("#edit-attributes-4-24-wrapper").attr("title", "Removable sleeve has our high density foam protection system and is removable for days when you don't need your laptop");
    $("#edit-attributes-4-23-wrapper .option").append("™");
    $(".attribute-9").attr("title", "Leather luggage tag with clear window for your business or identity card.  Enter your initial below and we personalize the tag with your initials.");
    $(".attribute-11").attr("title", "Leather luggage tag with clear window for your business or identity card.  Enter your initial below and we personalize the tag with your initials.");
    $("#edit-attributes-10-37-wrapper").attr("title", "Multimedia Kit includes: Accessory pouch, tripod, wireless mouse, TSA lock, and a 52-in-1 memory card reader.");
    $("#edit-attributes-10-36-wrapper").attr("title", "Road Warrior Kit includes: Accessory pouch, wireless mouse, TSA lock, and a cable key lock.");
    $("#edit-attributes-10-35-wrapper").attr("title", "Security Kit includes: Accessory pouch, base plate, TSA lock, and a cable key lock.");
    
    
    $("#edit-attributes-11").attr("maxlength", "3");

    //$(".radioNylon").click();
    //$(".radioMultimedia").click();
    //$(".radioSleek").click();
    //$(".radioBlack").click();

    var cycImagePath = "/images/cyc-sc/";
    
   

    //var customOptions = new Array('Fabric', 'Pocket', 'Handle', 'Hardware');
    //var customAmount = customOptions.length;
    var finalFilename = "1111.jpg";
    var fabricImageID = "1";
    var pocketImageID = "1";
    var handleImageID = "1";
    var hardwareImageID = "1";
    $(".input-finalFilename").val(finalFilename);
    $(".radioTraditional").attr("disabled", "disabled");

    //Fabric JS
    var fabricOptions = new Array('Nylon', 'Leather');
    var fabricID = new Array('5-25', '5-26');
    var fabricAmount = fabricOptions.length;

    for (x = 0; x <= (fabricAmount - 1); x++) {
        $(".radio" + fabricOptions[x]).click(function() { //creates a clicking function for each thumbnail

            fabricImageID = $(this).attr("alt");
            finalFilename = (fabricImageID + pocketImageID + handleImageID + hardwareImageID + ".jpg");
            $(".input-finalFilename").val(finalFilename);
            $(".bagPreview").attr("src", cycImagePath + finalFilename);
            $("#edit-attributes-" + fabricID[(fabricImageID - 1)]).click();

            if (fabricImageID == "1") {
                handleImageID = "1";
                $(".radioTraditional").attr("disabled", "disabled");
                $(".radioSleek").click();
            }
            else {
                $(".radioTraditional").attr("disabled", "");

            }

        });
    }

    //pocket JS
    var pocketOptions = new Array('Multimedia', 'Roadwarrior', 'Vanity');
    var pocketID = new Array('6-28', '6-27', '6-29');
    var pocketAmount = pocketOptions.length;

    for (x = 0; x <= (pocketAmount - 1); x++) {
        $(".radio" + pocketOptions[x]).click(function() { //creates a clicking function for each thumbnail

            pocketImageID = $(this).attr("alt");
            finalFilename = (fabricImageID + pocketImageID + handleImageID + hardwareImageID + ".jpg");
            $(".input-finalFilename").val(finalFilename);
            $(".bagPreview").attr("src", cycImagePath + finalFilename);
            $("#edit-attributes-" + pocketID[(pocketImageID - 1)]).click();

        });
    }

    //handle JS
    var handleOptions = new Array('Sleek', 'Traditional');
    var handleID = new Array('7-31', '7-30');
    var handleAmount = handleOptions.length;


    for (x = 0; x <= (handleAmount - 1); x++) {
        $(".radio" + handleOptions[x]).click(function() { //creates a clicking function for each thumbnail

            handleImageID = $(this).attr("alt");
            finalFilename = (fabricImageID + pocketImageID + handleImageID + hardwareImageID + ".jpg");
            $(".input-finalFilename").val(finalFilename);
            $(".bagPreview").attr("src", cycImagePath + finalFilename);
            $("#edit-attributes-" + handleID[(handleImageID - 1)]).click();


        });
    }


    //hardware JS
    var hardwareOptions = new Array('Black', 'Silver');
    var hardwareID = new Array('8-32', '8-33');
    var hardwareAmount = hardwareOptions.length;


    for (d = 0; d <= (hardwareAmount - 1); d++) {
        $(".radio" + hardwareOptions[d]).click(function() { //creates a clicking function for each thumbnail

            hardwareImageID = $(this).attr("alt");
            finalFilename = (fabricImageID + pocketImageID + handleImageID + hardwareImageID + ".jpg");
            $(".input-finalFilename").val(finalFilename);
            $(".bagPreview").attr("src", cycImagePath + finalFilename);
            $("#edit-attributes-" + hardwareID[(hardwareImageID - 1)]).click();


        });
    }














}

function filterPages() {

    $(".filterImgPanelMenu-close").click(
					function() {

					    $(".view-filters").slideUp();
					    $(".filterImgPanelMenu-close").fadeOut();
					    $(".filterImgPanelCollapse-close").fadeIn();

					}
				);

    $(".filterImgPanelCollapse-close").click(
					function() {
					    $(".view-filters").slideDown();
					    $(".filterImgPanelCollapse-close").fadeOut();
					    $(".filterImgPanelMenu-close").fadeIn();
					}
				);

					var parentClicks = new Array('edit-size-170-wrapper', 'edit-case-type-wheeled-wrapper', 'edit-protection-ezgrip-wrapper', 'edit-price-150-199-wrapper', 'edit-color-other-wrapper', 'edit-material-leather-wrapper', 'edit-xray-no-wrapper');
    var parentAmount = parentClicks.length;

    for (x = 0; x <= (parentAmount - 1); x++) {
        $("#" + parentClicks[x]).parent().click(
						function() {
						    $("#edit-submit-uc-products-FILTER-MAC").click();
						    $("#edit-submit-uc-products-FILTER-PC-Cases").click();
						    $("#edit-submit-uc-products-FILTER-NETBOOK").click();
						    $("#edit-submit-uc-products-FILTER-XRAY").click();
						    $("#edit-submit-uc-products-FILTER").click();
						    $("#edit-submit-uc-products-FILTER-IPAD").click();
						}
					);
		}


		$("#edit-submit-uc-products-FILTER-MAC").css("opacity", ".0");
		$("#edit-submit-uc-products-FILTER-PC-Cases").css("opacity", ".0");
		$("#edit-submit-uc-products-FILTER-NETBOOK").css("opacity", ".0");
		$("#edit-submit-uc-products-FILTER-XRAY").css("opacity", ".0");
		$("#edit-submit-uc-products-FILTER").css("opacity", ".0");
		$("#edit-submit-uc-products-FILTER-IPAD").css("opacity", ".0");
		//$("#edit-submit-uc-products-FILTER-MAC").parent().append('<div class="autoFilter"><input type="radio" checked />auto-filter results</div>');

    
    $("#edit-size-170-wrapper").parent().css("left", "0px").css("width", "62px").css("white-space", "nowrap").css("color","#000");
    $("#edit-case-type-wheeled-wrapper").parent().css("left", "34px").css("width", "110px").css("white-space", "nowrap").css("color", "#5F6062");
    $("#edit-protection-ezgrip-wrapper").parent().css("left", "28px").css("width", "80px").css("white-space", "nowrap").css("color", "#5F6062");
    $("#edit-price-150-199-wrapper").parent().css("left", "50px").css("width", "93px").css("white-space", "nowrap").css("color", "#5F6062");
    $("#edit-color-other-wrapper").parent().css("left", "48px").css("width", "53px").css("white-space", "nowrap").css("color", "#5F6062");
    $("#edit-material-leather-wrapper").parent().css("left", "70px").css("width", "65px").css("white-space", "nowrap").css("color", "#5F6062");
    $("#edit-xray-no-wrapper").parent().css("left", "104px").css("width", "44px").css("white-space", "nowrap").css("color", "#5F6062");


    $(".view-header").prependTo(".view-content");
    $("h1.title").prependTo(".view-content");
    

    $("#edit-protection-newcore-wrapper").attr("title", "Our highest level of protection system that offers 6-sided protection and wraps your notebook computer in 1.5\" of high-density foam");
    $("#edit-protection-hdf-wrapper").attr("title", "The High Density Foam Protection system offers a lightweight cradle system that wraps the notebook up to 1\" of foam.");
    $("#edit-protection-ezgrip-wrapper").attr("title", "This patent-pending handle system offers protection without bulk and keeps your Brenthaven sleeve from slipping out of your hands.");
    $("#edit-xray-no-wrapper").parent().attr("title", "X-Ray Friendly cases allow you to go through airport security without removing your laptop from its case!");

}


function str_replace(search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/str_replace    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
    f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array; s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            } 
        }
    }
    return sa ? s : s[0];
}
