﻿/*---------------------------------------------------------
Main jQuery

Revision:      002
Last Updated:  09/11/2009 by kevinmarmolinero@redweb.com
Created:       09/06/2009 by scottdarby@redweb.com
---------------------------------------------------------

Global document elements and initialisations

*/

$(function () {
    $('html').removeClass('noJs'); //add class to body to allow elements to be hidden if js is enabled
    newWindow(); //open all links with rel attribute of _blank in new window to allow validation
    setHeight('#hub-listing p'); //setHeight init
    setHeight('#sliderContent ul'); //setHeight init
    setHeight('ul.simpleNav li a'); //setHeight init
    setHeight('div.items div p'); //setHeight init
    $('a.mapView').parent().css("width", 122);
    $('a.listView').parent().css("width", 146);
    $('#sitemap ul:first').quickTree().show();
    $("body#offline div.imageGallery img").addClass('quickbox'); //image lightbox init
    $('p.sizeSelectorLink a').removeAttr('href');
    $('.offBrand .customerReviews p').after('<span class="bottom"></span>');

    if ($('p:first-child').hasClass('discountBadge')) {
        $('body.Store div.mainContent p:nth-child(2)').addClass('summary');
        $('body.Store div.mainContent p:nth-child(2)').css({ 'width': '530px', 'padding-left': '10px' });
    }
    else {
        $('body.Store div.mainContent p:first-child').addClass('summary');
    }

    $('div.tab img').addClass('pngFix');

    $('input.expanderInput').focus(function () {
        $(this).val('');
    });

    //        $('input.expanderInput').blur(function () {
    //            $(this).val('Enter Postcode, Town or City');
    //        });

    board(); //FAQs init

    if ($('div.tab p').hasClass('question')) {
        $('div.tab p.question').parent().prepend("<a href='#expandContractFAQs' id='expandContractFAQs'><span class='expand'>Expand all questions</span><span class='FAQclose'>Close all questions</span></a>");
        $('#expandContractFAQs').next().addClass('clearLeft');
    }

    $('a#expandContractFAQs').click(function () {
        if ($('a#expandContractFAQs').hasClass('allOpen')) {
            $(this).removeClass('allOpen');
            $('.answer').removeClass('showAll');
            $('.answer').hide();
        }
        else {
            $(this).addClass('allOpen');
            $('.answer').addClass('showAll');
        }
    });
    if ($('.quoteHorizontal').length) {
        var testAreaHeight = $('.storeDiscounts').height() + 180;
        $('.testArea').css("height", testAreaHeight);
        $('div.aside div.testArea div').css("position", "absolute");
    }
});

/*--------------------------------------------------
Script name: 		New window
Author:				Scott Darby
Version:			1.0
Created date:		23/06/2001
jQuery Version:	    1.3.2+ 		
Notes:              open all links with rel attribute of _blank 
in to allow validation
------------------------------------------------*/
function newWindow() {
    $('a[rel="blank"]').attr({
        target: '_blank',
        title: 'This link opens in a new window'
    });
}

/*--------------------------------------------------
Script name: 		quickTree 0.4
Author:				Scott Darby
Version:			1.0
Created date:		23/06/2001
jQuery Version:	    1.3.2+ 		
Notes:              jQuery plugin to create tree-structure navigation 
from an unordered list (used for sitemap in many Episerver projects)
------------------------------------------------*/
jQuery.fn.quickTree = function () {
    return this.each(function () {
        var $tree = $(this);
        var $roots = $tree.find('li');
        $tree.find('li:last-child').addClass('last');
        $tree.addClass('tree');
        $tree.find('ul').addClass('hide');
        $roots.each(function () {
            if ($(this).children('ul').length > 0) {
                $(this).addClass('root').prepend('<span class="expand" />');
            }
        });
        $('span.expand').toggle(
            function (e) {
                var $clicked = $(e.target);
                $clicked.toggleClass('contract').nextAll('ul').removeClass('hide');
            },
            function (e) {
                var $clicked = $(e.target);
                $clicked.toggleClass('contract').nextAll('ul').addClass('hide');
            }
        );
    });
};

/*------------------------------------------------
jQuery FAQ Accordian/slider.
--------------------------------------------------
Script name: 		Episerver FAQ
Author:				Kevin Mar-Molinero
Version:			2.0
Created date:		23/06/2001
jQuery Version:	    1.3.2+		
Notes:              Relies on classes of question/answer being added
classes are pre-defined in the Episerver WYSIWYG (TinyMCE) so can be 
used "out-of-the-box" by users by selecting from a dropdown. 
------------------------------------------------*/

function board() {

    var $boardTitle = $('.question');

    $boardTitle.not(':first').nextAll('.answer').hide();

    $(function () {
        $(".answer").hide();
        $(".question").click(function () {
            $(this).nextAll().each(function () {
                if ($(this).filter('.question').length) {
                    return false;
                }
                $(this).filter('.answer').toggle('slow');
            });
        });
    });

    $boardTitle.hover(
            function () {
                $(this).css({ 'cursor': 'pointer', 'text-decoration': 'none' });
            },
            function () {
                $(this).css({ 'text-decoration': 'underline' });
            }
        );
}

/*---------------------------------------------------
Script name: 		Simple tabs script
Author:				Kevin Mar-Molinero
Version:			1.0
Created date:		23/06/2001
jQuery Version:	    1.3.2+				
---------------------------------------------------*/
//set open tab to firsts
$(document).ready(function () {
    if ($('div.tab').hasClass('current')) {
    }
    else {
        $('div.tab:first').addClass('current');
        //$('body.RegionalSEO div.tabbed:nth-child(even)').addClass('secondaryContent');
        $('div.secondaryContent div.tab:first').addClass('current');
        $('ul.simpleNav li:first').addClass('current');
        $('div.secondaryContent ul.simpleNav li:first').addClass('current');
    }
    
});

//tabbed content script
$(document).ready(function () {
    $('ul.simpleNav a').click(function () {
        var curChildIndex = $(this).parent().prevAll().length + 1;

        $(this).parent().parent().children('.current').removeClass('current');
        $(this).parent().addClass('current');
        $(this).parent().parent().next('.tabContent').children('.current').fadeOut('fast', function () {
            $(this).removeClass('current');
            $(this).parent().children('div:nth-child(' + curChildIndex + ')').fadeIn('fast', function () {
                $(this).addClass('current');
            });
        });
        return false;
    });
});

/* ------------------------------------------------------------------
Script name: 	    Equal height elements	
Author:				Kevin Mar-Molinero (adapated from web)
Version:			1.0
Created date:		23/06/2001
jQuery Version:	    1.3.2		
notes:              match height of selected elements on page load 
function setHeight() declared with selected elements at top of script
---------------------------------------------------------------------*/
var maxHeight = 0;

function setHeight(col) {

    col = $(col);
    col.each(function () {

        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
    });
    col.height(maxHeight);
}

/*---------------------------------------------------
Script name: 		Region Price Scrolling List
Author:				Giles Butler
Revised:            Luke Guppy
Version:			1.0
Created date:		12/10/2010
jQuery Version:	    1.3.2+			
---------------------------------------------------*/
$(document).ready(function () {
    var pageLinkx = '/price-regions/London-north-of-the-river/';
    $('a.storagePrice').attr('href', pageLinkx);
    //Region Price Scrolling List

    // the default selected li's class

    var regionID = '.region-1';

    //an array of prices from the regionID ul's
    var regionPrices = $('ul' + regionID).find("li").map(function () {
        return $(this).text();
    }).get();

    //set the default prices
    $('span#smallPrice').append(regionPrices[0]);
    $('span#mediumPrice').append(regionPrices[1]);
    $('span#largePrice').append(regionPrices[2]);


    $('.priceRegion li').bind('click', function () {

        // the currently selected li's class

        regionID = '.' + $(this).attr('class');

        //an array of prices from the regionID ul's

        regionPrices = $('ul' + regionID).find("li").map(function () {
            return $(this).text();
        }).get();

        $('a.storagePrice span').empty();

        if ($(this).hasClass('region-1')) {
            pageLinkx = '/price-regions/London-north-of-the-river/';
            $(this).parent().animate({ top: 14 });
        }
        else if ($(this).hasClass('region-2')) {
            pageLinkx = '/price-regions/London-south-of-the-river/';
            $(this).parent().animate({ top: -38 });
        }
        else if ($(this).hasClass('region-3')) {
            pageLinkx = '/price-regions/North-and-Midlands/';
            $(this).parent().animate({ top: -90 });
        }
        else if ($(this).hasClass('region-4')) {
            pageLinkx = '/price-regions/South-and-South-East/';
            $(this).parent().animate({ top: -142 });
        }

        $('a.storagePrice').attr('href', pageLinkx);

        $('span#smallPrice').append(regionPrices[0]).stop().hide().fadeIn();
        $('span#mediumPrice').append(regionPrices[1]).stop().hide().fadeIn();
        $('span#largePrice').append(regionPrices[2]).stop().hide().fadeIn();

    });

});

/*---------------------------------------------------
Script name: 	    Accordion	
Author:				Giles Butler
Revision:           Kevin Mar-Molinero
Version:			1.0
Created date:		23/06/2001
jQuery Version:	    1.3.2+			
Notes:              Seperate versions created to handle different scenarios 
e.g. all open; one at a time; one after the other, as well as handle cross browser
---------------------------------------------------*/
// Accordian for the quote pages
$(function () {
    // assuming we have the open class set on the A when the HTML is delivered
    $('LI.drawer a:not(.open)').next('ul.information').hide();

    $('.drawer-handle').click(function () {
        if ($(this).hasClass('open')) {
            $(this).removeClass('open');
            $(this).next('ul.information').slideUp('slow');
        }

        else {
            $(this).addClass('open');
            $(this).next('ul.information').slideDown('slow');
        }
    });

    // Accordian for the quote pod $('h3').hasClass('reservationConf')
    if ($('body ul#quoteProgressBar li').hasClass('stepOne')) {
        //        $('li.sectionInfo a').next().hide();
        //        $('li.sectionInfo a span.icon').hide();
        //        $('li.sectionInfo a.quote-handle').attr('href', '/Find-a-Store/');
        $('body').addClass('quoteProcessHeader');

    } else if ($('body').hasClass('OnBrandOffline')) {
        $('li.sectionInfo:first-child').hide();
        $('li.sectionInfo:last-child').addClass('OnBrandOffline').show();

    } else if ($('h3').hasClass('reservationConf')) {
        //        $('li.sectionInfo a').next().hide();
        //        $('li.sectionInfo a span.icon').hide();
        //        $('li.sectionInfo a.quote-handle').attr('href', '/Find-a-Store/');
        $('body').addClass('quoteProcessHeader');
    }

    else {
        $('li.sectionInfo a:not(.quoteOpen)').next().hide();
    }

    $('.quote-handle span.icon').click(function () {
        if ($(this).parent().hasClass('quoteOpen')) {
            $('span.icon').addClass('hide');
            $(this).parent().removeClass('quoteOpen').next().slideUp('slow');
        }
        else {
            // find the open drawer, remove the class, move to the next element following it and hide it
            $('.quoteOpen').removeClass('quoteOpen').next().slideUp('slow');
            // add the open class to this A, move to the next element (i.e UL) and show it
            $(this).parent().addClass('quoteOpen').next().slideDown('slow').next('span.icon').removeClass('hide');
        }

    });
});

/*---------------------------------------------------
Script name: 	    jQuery Slider	
Author:				Giles Butler
Version:			1.0
Created date:		23/06/2001
jQuery Version:	    1.3.2+			
---------------------------------------------------*/
function unitSlider() {

    if ($('body').hasClass('UnitSizeSelector')) {

        uivalue = 0;

        $(function () {

            //vars
            var value = 0;
            var viewer = 920;
            var conveyor = $(".content-conveyor", $("#sliderContent"));
            var item = $(".item", $("#sliderContent"));
            var item_width = 230;
            var item_total_width = (item.length * item_width);
            var item_percent = ((item_width / item_total_width) * 100);
            var item_scroll = Math.round((((item.length * item_width) - viewer) / item.length));
            var slider = $("#slider");

            //set length of conveyor
            conveyor.css("width", item.length * parseInt(item.css("width")));

            /*config*/
            var sliderOpts = {
                max: ((item.length * item_width) - viewer),
                //step: (((item.length * item_width) - viewer) / item.length),
                slide: function (e, ui) {
                    value = Math.round(ui.value);
                    conveyor.css("left", "-" + value + "px");
                }
            };
            //  //create slider
            $("#slider").slider(sliderOpts);

            //The handler
            var handle = $(".ui-slider-handle", $("#slider"))

            $("#slideNext").live('click', function () {
                if (value > ((item_scroll * item.length) - item_width)) {
                    slider.slider("option", "value", (item_scroll * item.length));
                    conveyor.css("left", "-" + (item_scroll * item.length) + "px");
                    value = (item_scroll * item.length);
                    return false;
                }
                else {
                    slider.slider("option", "value", (value + item_scroll));
                    conveyor.css("left", "-" + (value + item_scroll) + "px");
                    value = (value + item_scroll);
                    return false;
                }
            });
            $("#slidePrev").live('click', function () {
                if (value < item_scroll) {
                    slider.slider("option", "value", 0);
                    conveyor.css("left", "-" + 0 + "px");
                    value = 0;
                    return false;
                }
                else {
                    slider.slider("option", "value", (value - item_scroll));
                    conveyor.css("left", "-" + (value - item_scroll) + "px");
                    value = Math.round(value - item_scroll);
                    return false;
                }
            });
        });
    }
    else { }
};

$(document).ready(function () {
    unitSlider($);
});

$(window).load(function () {
    $('.viewer').css("backgroundColor", "transparent");
});
/*-------------------------------------------------------------------       
Script name:    Quickbox (extended)
Version:        v2.0
jQuery:         jQuery v1.4.2 +
Author:         Luke Guppy
Modified:       Kevin Mar-Molinero
Modified date:  08-09-2010
Notes:          Script seperated in two parts so as to target off brand and onbrand templates,
also set up to treat images and links to lower level pages differently.
-------------------------------------------------------------------*/

$(function () {
    var winH;
    var winW;
    var imageWidth;
    var popUpHeight;

    var backgroundHeight = $("body").innerHeight();

    winH = $("body").innerHeight();
    winW = $(window).width();

    if ($('#quickboxParent').length) {
        $(this).attr('id', '');
    }

    $("div.aside div.imageGallery ul li img").addClass('quickbox');

    $(".quickbox").click(function () {
        $("#quickbox").removeClass().children().remove();

        $('#quickboxParent').attr('id', '');

        $(this).parent().attr('id', 'quickboxParent');

        if ($(this).is('img')) {
            var imageLarge = $(this).attr("src");

            $('#quickbox').append("<a id='closeme' href='#'>Close</a><img src='" + imageLarge + "' alt=''/>");
            $("#quickbox").addClass('imagePopUp');

            $("#quickbox").load();

            if ($("#background").length) {
            }

            else {
                $("#quickbox").before("<div id='background'></div>");
            }

            $("#background").css({ backgroundColor: '#004b8d' }).fadeIn("slow");
            $('#background').addClass('fireFix');
            $('.fireFix').css({ height: backgroundHeight });
            $("#quickbox").fadeIn("slow");




            var imageWidth = $('div.imagePopUp img').innerWidth();

            popUpHeight = ($("div#quickbox").innerHeight());

            var marginMath = (100 / 3);

            $("div.imagePopUp").css({ 'margin-left': marginMath + '%' });

            var popOffset = $('#quickbox').offset();

            $("div#quickbox").css({ 'margin-top': '50px' });

            var bringMeHome = (popOffset.top + popUpHeight - (winH / 2));
            $(window).scrollTop(bringMeHome);

            $("div.imagePopUp").css('width', imageWidth);

            $("#closeme").click(function () {
                $("a#closeme").attr('href', "#quickboxParent");
                closeWin();
                $("#quickbox img").fadeOut("slow").remove();
            });
            return false;
        }

        else {
            if ($(this).attr("href")){
                    $("#quickbox").load($(this).attr("href"));
            }
            else{
                    $("#quickbox").load($(this).attr("name"));
            }
        }

        if ($("#background").length) {
        }

        else {
            $("#quickbox").before("<div id='background'></div>");
        }

        $("#background").css({ backgroundColor: '#004b8d' }).fadeIn("slow");
        $('#background').addClass('fireFix');
        $('.fireFix').css({ height: backgroundHeight });
        $("#quickbox").fadeIn("slow");


        var popUpHeight = ($("div#quickbox").innerHeight() / 2);

        $("div#quickbox").css({ 'margin-top': '50px' });

        var popOffset = $('#quickbox').offset();
        var bringMeHome = (popOffset.top + popUpHeight - (winH / 2));
        $(window).scrollTop(bringMeHome);

        return false;
    });
});


//fade out #quickbox
function closeWin() {
    $("#quickbox").fadeOut("slow");
    $("#background").fadeOut("slow").remove();
    $("a#closeme").css({ display: 'none' });
};

$(function () {
    $("#closeme").click(function () {
        closeWin();
    });
});

/*-------------------------------------------------------------------       
Script name:    N/A
Author:         Scott Reed
Notes:          Ties into .net javascript
-------------------------------------------------------------------*/

function numbersonly(myfield, e, dec) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27))
        return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    // decimal point jump
    else if (dec && (keychar == ".")) {
        myfield.form.elements[dec].focus();
        return false;
    }
    else
        return false;
}

/*-------------------------------------------------------------------------------
Script name: Googlemaps loading gif
Author:         Kevin Mar-Molinero  
Notes:          Adds overlay and load for googlemaps
-------------------------------------------------------------------------------*/

$(function () {
    $('div#map').before("<div class='loadingGif'><img src='/templates/i/loadingGifs/mapLoading.gif' alt='store map loading' /></div>");
    $('ul.promotionsHolder li:first-child').addClass('firstChild');
});

function sizeCalculator() {
    win3 = window.open("/Quote--Reservation-process-pages/Select-the-size-of-storage-you-need/Size-Calculator-Tool/", "", "width=1075, height=1070, scrollbars = 1")
    win3.moveTo(screen.width / 2 - 537, screen.height / 2 - 535)
}
/*-------------------------------------------------------------------------------
Script name: Existing customer form field toggle
Author:         Luke Guppy
Date:           25/11/2011  
Notes:          Hides and show the existing customer form fields within the contact forms
-------------------------------------------------------------------------------*/
$('.emailForm .customer').hide();
$('.ContactUs .simpleNav a').bind('click', function () {
    $('.emailForm .customer').hide();
    $('.emailForm .radio input').removeAttr('checked');
    return false;
});
$('.emailForm .radio input').bind('click', function () {
    var showFields = $(this).val();
    if (showFields == 'true') {
        $('.emailForm .customer').show();
    }
    else {
        $('.emailForm .customer').hide();
    }
});
