﻿//var departments_menu = 0;

$(document).ready(function() {

try {
    var strProductOptions = $("#product_options").html();
    try {
        if (strProductOptions.indexOf("xPortabase") != -1) {
            //while (strProductOptions.indexOf("Portabase") != -1) {
            strProductOptions = strProductOptions.replace("Portabase", "<span class='has-tooltip' title='This is a suitable base kit for this shed.' style='color:#09F;text-decoration:underline'>Portabase</span>")
                //alert("This product has a Portabase option.")
            //}
           $("#product_options").html(strProductOptions)
        }
    } catch (err) {
        // no features to style
    }
} catch (err) { };

    try {
        var departments_options = {
            div: "#departments-menu-container",
            controls: "#departments-menu-controls",
            loader: "", //"#departments-loading",
            x: 150,
            y: 200,
            easing: "easeOutBackSmall",
            easeIn: "easeOutBack",
            preloadAll: true
        };

        var menu = new slideMenu(departments_options);
        //setTimeout(function(){menu.switchTo(departments_menu)}, 3500);
    } catch (err) { };

    // $("#search-field").focus(function() { $("#search-border").addClass("search-border-on"); $("#search-border").removeClass("search-border"); $("#search-button").addClass("green-button"); $("#search-button").removeClass("white-button") });
    // $("#search-field").blur(function() { $("#search-border").addClass("search-border"); $("#search-border").removeClass("search-border-on"); $("#search-button").addClass("white-button"); $("#search-button").removeClass("green-button") });
    $("#search-field").focus();
    try { $('#bill[name]').focus() } catch (err) { };

    var strTouchDevice = 'N';

    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        strTouchDevice = 'Y';
    }
    if (strTouchDevice == 'N') {
        $('.has-tooltip').tooltip({ showBody: " - ", track: true, delay: 0, showURL: false });
        $('.has-tooltip-wide').tooltip({ showBody: " - ", track: true, delay: 0, showURL: false, extraClass: "w350" });
        $('.has-left-tooltip').tooltip({ showBody: " - ", track: true, delay: 0, showURL: false, positionLeft: true });
    }

    if ($.browser.msie && parseInt(jQuery.browser.version) <= 6) {
        // not enabled for IE6-
    } else {
        if ($('#sidebar-secure-carousel').length) {
            $('#sidebar-secure-carousel').jcarousel({
                auto: 1,
                scroll: 1,
                size: 1,
                animation: 30000,
                wrap: 'circular'
            });
        }
    }

    try {
        $('#loupe-1').loupe({
            width: 150, // width of magnifier
            height: 150, // height of magnifier
            loupe: 'loupe-viewer' // css class for magnifier
        });
        //$('#loupe-1').loupe();
    } catch (err) {
        // no loupe on page to style
    }

    try {
        $('#expert-tips .expert-tip');
        setInterval(function() {
            $('#expert-tips .expert-tip').filter(':visible').fadeOut(1000, function() {
                if ($(this).next('div.expert-tip').size()) {
                    $(this).next().fadeIn(500);
                } else {
                    $('#expert-tips .expert-tip').eq(0).fadeIn(500);
                }
            });
        }, 7000);
    } catch (err) {
        // no expert tip on page
    }

    //$('#dialog-modal').dialog();

    try {
        //scrollpane parts
        var scrollPane = $(".scroll-pane"),
			scrollContent = $(".scroll-content");

        //build slider
        var scrollbar = $(".scroll-bar").slider({
            animate: true,
            slide: function(event, ui) {
                if (scrollContent.width() > scrollPane.width()) {
                    scrollContent.css("margin-left", Math.round(
						ui.value / 100 * (scrollPane.width() - scrollContent.width())
					) + "px");
                } else {
                    scrollContent.css("margin-left", 0);
                }
            }
        });

        //append icon to handle
        var handleHelper = scrollbar.find(".ui-slider-handle")
		.mousedown(function() {
		    scrollbar.width(handleHelper.width());
		})
		.mouseup(function() {
		    scrollbar.width("100%");
		})
		.append("<span class='ui-icon ui-icon-grip-dotted-vertical'></span>")
		.wrap("<div class='ui-handle-helper-parent'></div>").parent();

        //change overflow to hidden now that slider handles the scrolling
        scrollPane.css("overflow", "hidden");

        //size scrollbar and handle proportionally to scroll distance
        function sizeScrollbar() {
            var remainder = scrollContent.width() - scrollPane.width();
            var proportion = remainder / scrollContent.width();
            var handleSize = scrollPane.width() - (proportion * scrollPane.width());
            scrollbar.find(".ui-slider-handle").css({
                width: handleSize,
                "margin-left": -handleSize / 2
            });
            handleHelper.width("").width(scrollbar.width() - handleSize);
        }

        //reset slider value based on scroll content position
        function resetValue() {
            var remainder = scrollPane.width() - scrollContent.width();
            var leftVal = scrollContent.css("margin-left") === "auto" ? 0 :
				parseInt(scrollContent.css("margin-left"));
            var percentage = Math.round(leftVal / remainder * 100);
            scrollbar.slider("value", percentage);
        }

        //if the slider is 100% and window gets larger, reveal content
        function reflowContent() {
            var showing = scrollContent.width() + parseInt(scrollContent.css("margin-left"), 10);
            var gap = scrollPane.width() - showing;
            if (gap > 0) {
                scrollContent.css("margin-left", parseInt(scrollContent.css("margin-left"), 10) + gap);
            }
        }
    } catch (err) { };

    //change handle position on window resize
    $(window).resize(function() {
        try {
            resetValue();
            sizeScrollbar();
            reflowContent();
        } catch (err) { };
    });
    //init scrollbar size
    setTimeout(sizeScrollbar, 10); //safari wants a timeout

    try {
        //<span class="image{d10}"></span><span class="imageSpace"></span>
        $('#glowingLayout').countdown({ until: new Date("January 30, 2012 23:59:00"), compact: true,
            layout: '<span class="image{d1}"></span>' +
        '<span class="imageDay"></span>' +
        '<span class="image{h10}"></span><span class="image{h1}"></span>' +
        '<span class="imageSep"></span>' +
        '<span class="image{m10}"></span><span class="image{m1}"></span>' +
        '<span class="imageSep"></span>' +
        '<span class="image{s10}"></span><span class="image{s1}"></span>'
        });
    } catch (err) { };
});

function showFullRange() {
    $('#products').show();
//    $('#content').css('margin-top', '0px');
    $('#products-mini-container').hide();
    $('html, body').animate({ scrollTop: 0 }, 'slow');
}

function hideFullRange() {
    $('#products').hide();
//    $('#content').css('margin-top', '90px')
    $('#products-mini-container').show();
    $('html, body').animate({ scrollTop: 0 }, 'slow');
}

function dialogCallback() {
    //$('#dialog-modal').html($('#dialog-callback').html());
    //$('#dialog-modal').dialog({
    $('#dialog-callback').dialog({
        modal: true,
        title: 'Request a Callback',
        minWidth: 600,
        minHeight: 300,
        position: ['center', 83],
        buttons: {
            'Send Request': function() {
                var shed = new Image();
                var d = new Date();
                try {
                    shed.src = 'http://3.shedstore.co.uk/3/actions/callback.aspx?to=' + escape($('#callback-form-type').val()) + '&name=' + escape($('#callback-form-name').val()) + '&number=' + escape($('#callback-form-number').val()) + '&time=' + escape($('#callback-form-time').val()) + '&order=' + escape($('#callback-form-order-number').val()) + '&more=' + escape($('#callback-form-more-information').val()) + '&t=' + d.getTime();
                    alert('Thank you. Your enquiry has been sent and we will deal with it as soon as possible.');
                } catch (err) {
                    //
                };
                $(this).dialog('close');
            },
            'Close': function() {
                $(this).dialog('close');
            }
        }
    });
}

function dialogFeedback() {
    $('#dialog-feedback').dialog({
        modal: true,
        title: 'Feedback',
        minWidth: 600,
        minHeight: 300,
        position: ['center', 83],
        buttons: {
            'Send Feedback': function() {
                var shed = new Image();
                var d = new Date();
                try {
                    shed.src = 'http://3.shedstore.co.uk/3/actions/feedback.aspx?name=' + escape($('#feedback-form-name').val()) + '&email=' + escape($('#feedback-form-email').val()) + '&feedback=' + escape($('#feedback-form-feedback').val()) + '&url=' + escape(location.href) + '&t=' + d.getTime();
                    alert('Thank you, we love feedback! We try to make Shedstore better all the time. If you provided an e-mail address we will notify you if requested.');
                } catch (err) {
                    //
                };
                $(this).dialog('close');
            },
            'Close': function() {
                $(this).dialog('close');
            }
        }
    });
}

function showModalDialog(strTitle) {
    $('#dialog-modal').dialog({
        modal: true,
        title: strTitle,
        minWidth: 600,
        minHeight: 300,
        position: ['center', 83],
        buttons: {
            'OK': function() {
                $(this).dialog('close');
            }
        }
    });
}

function showLargePhoto(strImagePath) {
    $('#dialog-modal').html('<div class="center"><img src="'+strImagePath +'" alt="Product Photo" class="product-image-large" /></div>');
    $('#dialog-modal').dialog({
        modal: true,
        title: 'Product Photo',
        minWidth: 600,
        minHeight: 300,
        position: ['center', 83],
        buttons: {
            'OK': function() {
                $(this).dialog('close');
            }
        }
    });
}

function showDialogDelivery() {
    $('#dialog-modal').load('http://www.shedstore.co.uk/3/dialogs/delivery.htm', function() {
        $('#dialog-modal').dialog({
            modal: true,
            title: 'Delivery Options',
            minWidth: 600,
            minHeight: 300,
            position: ['center', 83],
            buttons: {
                'Close': function() {
                    $(this).dialog('close');
                    }
                }
            });
    })
}

function showDialogTypicallyAnticipated() {
    $('#dialog-modal').load('http://www.shedstore.co.uk/3/dialogs/typically-anticipated.htm', function() {
        $('#dialog-modal').dialog({
            modal: true,
            title: 'Typically Anticipated Delivery Times',
            minWidth: 600,
            minHeight: 300,
            position: ['center', 83],
            buttons: {
                'Close': function() {
                    $(this).dialog('close');
                }
            }
        });
    })
}
function selectLetter(strLetter) {
    $(".glossary-letter").css("visibility", "hidden");
    $(".glossary-letter").css("display", "none");
    $(".glossary-letter-hidden").css("visibility", "hidden");
    $(".glossary-letter-hidden").css("display", "none");
    $("#glossary-" + strLetter).css("visibility", "visible");
    $("#glossary-" + strLetter).css("display", "block");
    $(".glossary-link").css("background-color", "#EEE");
    $(".glossary-link").css("border", "1px solid #CCC");
    $(".glossary-link").css("color", "#000");
    $("#glossary-link-" + strLetter).css("background-color", "#999");
    $("#glossary-link-" + strLetter).css("border", "1px solid #999");
    $("#glossary-link-" + strLetter).css("color", "#FFF");
}

function ShowProductTab(intTab) {
    $('.product-tab-on').addClass('product-tab-off');
    $('.product-tab-on').removeClass('product-tab-on');
    $('#product-tab-' + intTab).addClass('product-tab-on');
    $('#product-tab-' + intTab).removeClass('product-tab-off');
    $('#product-tab-' + intTab + 'a').addClass('product-tab-on');
    $('#product-tab-' + intTab + 'a').removeClass('product-tab-off');

    $('.product-tab-content').hide();
    $('#product-tab-content-' + intTab).show();
}

function validateSearch() {
    var strValue = $("#search-field").val();
    if (strValue.replace("  ", "").replace(" ", "") == "") {
        alert('Please enter a search term.');
        return false;
    } else {
        $("#search-form").attr("action", '/itemlist.php/findtext/' + strValue); 
        try {
            //bug.src = 'http://t.shedstore.co.uk/1/track-search.aspx?q=' + escape($("#search-field").val()) + '&referrer=' + escape(document.referrer) + '&t=' + d.getTime()
        } catch (err) { };
        return true;
    }
}
function suggestSearch() {
    var strValue = $("#search-field").val();
    try {
        if (strValue.length > 1) {
            $.get('http://www.shedstore.co.uk/3/ajax/showSearchSuggestions.php?q=' + escape(strValue), function(data) {
                if (data.length > 0) {
                    $('#search-suggestions').show();
                    $('#search-suggestions').html(data);
                } else {
                    $('#search-suggestions').hide();
                }
            });
        } else if (Left(strValue, 1) == '1' || Left(strValue, 1) == '2' || Left(strValue, 1) == '3' || Left(strValue, 1) == '4' || Left(strValue, 1) == '5' || Left(strValue, 1) == '6' || Left(strValue, 1) == '7' || Left(strValue, 1) == '8' || Left(strValue, 1) == '9' || Left(strValue, 1) == '0') {
            $.get('http://www.shedstore.co.uk/3/ajax/showSearchSuggestions.php?q=' + escape(strValue), function(data) {
                if (data.length > 0) {
                    $('#search-suggestions').show();
                    $('#search-suggestions').html(data);
                } else {
                    $('#search-suggestions').hide();
                }
            });
        } else {
            $('#search-suggestions').hide();
        }
    } catch (err) {
    }
}

function showReviews() {
    location.href = 'http://www.google.co.uk/products/seller?zmi=shedstore.co.uk';
}


function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}



/**
* jQuery Slide Menu
* ------------------------
*
* Copyright (c) 2011 Chris Nanney
* http://cnanney.com/journal/code/creating-apples-new-slide-menu-with-jQuery/
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*/

var slideMenu = function(options) {

    // Default values
    var defaults = {
    div: "#departments-menu-container",
    controls: "#departments-menu-controls",
        loader: false,
        x: 150,
        y: 150,
        start: 0,
        speed: 300,
        delay: 60,
        easing: '',
        easeIn: '',
        preloadAll: false
    };

    var o = options || {};

    for (var opt in defaults) o[opt] = (opt in o) ? o[opt] : defaults[opt];

    // In case someone types in an easing name wrong
    if (!jQuery.easing.hasOwnProperty(o.easing) && o.easing != '') o.easing = "easeOutBounce";
    if (!jQuery.easing.hasOwnProperty(o.easeIn) && o.easeIn != '') o.easeIn = "easeOutBack";

    // In case easing plugin is missing
    if (!jQuery.easing.hasOwnProperty("easeOutBounce")) o.easing = o.easeIn = '';

    var v = {
        middle: jQuery(document).width() / 2,
        active: o.start,
        c_w: jQuery(o.div).width(),
        c_o: jQuery(o.div).offset(),
        count: 0,
        x: 0,
        s_l: jQuery("ul:eq(" + o.start + ") li").length,
        animating: false,
        loading: {},
        loop_started: false
    };

    this.switchTo = function(to) {
        do_animate(to, v.active);
    }

    function do_animate(to, from) {
        if (from < to) out_left(from, to);
        if (from > to) out_right(from, to);
        v.active = to;
        highlight_active();
    }

    function in_left(id, go) {
        if (go == false) return;
        v.count = jQuery("ul:eq(" + id + ") li").length;
        if (v.count > 0) {
            if (jQuery("ul:eq(" + id + ") li:eq(0)").offset().left > v.middle) {
                // Move to left side first if isn't there
                jQuery("ul:eq(" + id + ") li").css("left", "-" + (v.c_w + o.x) + "px");
            }
            jQuery("ul:eq(" + id + ") li").each(function(index) {
                v.x = (v.c_w / 2) + ((v.count / 2) * o.x) - (o.x * index);
                jQuery("ul:eq(" + id + ") li:eq(" + (v.count - index - 1) + ")").delay((index + 2) * o.delay).animate({ left: "+=" + v.x }, o.speed, o.easing, function() {
                    if (index + 1 == v.count) v.animating = false;
                });
            });
        }
    }

    function in_right(id, go) {
        if (go == false) return;
        v.count = jQuery("ul:eq(" + id + ") li").length;
        if (v.count > 0) {
            if (jQuery("ul:eq(" + id + ") li:eq(0)").offset().left < v.middle) {
                // Move to right side first if isn't there
                jQuery("ul:eq(" + id + ") li").css("left", "0");
            }
            jQuery("ul:eq(" + id + ") li").each(function(index) {
                v.x = (v.c_w / 2) + ((v.count / 2) * o.x) - (o.x * index);
                jQuery(this).delay((index + 1) * o.delay).animate({ left: "-=" + v.x }, o.speed, o.easing, function() {
                    if (index + 1 == v.count) v.animating = false;
                });
            });
        }
    }

    function out_left(id, to) {
        v.animating = true;
        v.count = jQuery("ul:eq(" + id + ") li").length;
        if (v.count > 0) {
            jQuery("ul:eq(" + id + ") li").each(function(index) {
                v.x = jQuery(this).offset().left - v.c_o.left + o.x;
                var last = (index + 1 == v.count) ? true : false;
                jQuery(this).delay((index + 1) * o.delay).animate({ left: "-=" + v.x }, o.speed, function() { in_right(to, last); });
            });
        }
        else {
            in_right(to, true);
        }
    }

    function out_right(id, to) {
        v.animating = true;
        v.count = jQuery("ul:eq(" + id + ") li").length;
        if (v.count > 0) {
            jQuery("ul:eq(" + id + ") li").each(function(index) {
                v.x = v.c_w - jQuery(this).offset().left + v.c_o.left;
                var last = (index + 1 == v.count) ? true : false;
                jQuery(this).delay((v.count - index) * o.delay).animate({ left: "+=" + v.x }, o.speed, function() { in_left(to, last); });
            });
        }
        else {
            in_left(to, true);
        }
    }

    // Move first list to top center and add load handlers to first set of images
    jQuery("ul:eq(" + o.start + ") li").animate({ left: "-=" + ((v.c_w / 2) + (o.x / 2)) + "px", top: -o.y }, 0);

    // Image load check
    var img_load = o.preloadAll == false ? "ul:eq(" + o.start + ") img" : o.div + " img";
    jQuery(img_load).each(function(index) {
        v.loading[index] = true;
        v.loop_started = true;
        jQuery(this).load(function() {
            delete v.loading[index];
        });
        if (this.complete) jQuery(this).trigger("load");
        // http://stackoverflow.com/questions/2392410/jquery-loading-images-with-complete-callback/2392448#2392448
    });

    // If first set of images is loaded, animate it in
    function try_animate_in(index) {
        var count = 0;
        for (e in v.loading) { count++; }
        if (v.loop_started == true && count == 0) {
            clearInterval(try_start);
            // Hide loader
            if (o.loader != false && o.loader != '') jQuery(o.loader).hide();
            // Animate in
            jQuery("ul:eq(" + o.start + ") li").each(function(index) {
                v.x = (o.x / 2) + ((v.s_l / 2) * o.x) - (o.x * (index + 1));
                jQuery(this).delay(o.delay).animate({ left: "-=" + v.x, top: 0 }, o.speed, o.easeIn);
            });
        }
    }


    // Highlight active menu button
    function highlight_active() {
        jQuery(o.controls + " a.active").removeClass("active");
        jQuery(o.controls + " a:eq(" + v.active + ")").addClass("active");
    }

    // Handle menu clicks
    jQuery(o.controls + " a").click(function(e) {
        expandSlider();
        //alert("'" + (e.srcElement.offsetLeft + parseInt(e.srcElement.offsetWidth / 2)) + "px'")
        //document.getElementById('control-indicator').style.left = (e.srcElement.offsetLeft + parseInt((e.srcElement.offsetWidth-10) / 2)) + "px"

        //if (window.event) e = window.event;
        var srcEl = e.srcelement ? e.srcelement : e.target;

        jQuery("#departments-control-indicator").animate({ left: (srcEl.offsetLeft + parseInt((srcEl.offsetWidth - 12) / 2)) }, 500, function() { })
        e.preventDefault();
        if (v.animating == false) {
            do_animate(jQuery(this).data("target"), v.active);
        }
    });

    // Update values in case of resize
    window.onresize = function() {
        v.middle = jQuery(document).width() / 2,
		v.c_o = jQuery(o.div).offset();
    }

    // Keep checking until first set of images is laoded
    var try_start = setInterval(try_animate_in, 50);
}

function collapseSlider() {
    $("#departments-menu-container").slideUp("fast")

    $("#departments-menu-controls").css("background-image", "url('http://3.img.shedstore.co.uk/slider/2/controls-collapsed.png')")
    $("#departments-control-indicator").hide()

    $("#departments-menu-controls").css("-moz-border-radius-topleft", "5px")
    $("#departments-menu-controls").css("-webkit-border-top-left-radius", "5px")
    $("#departments-menu-controls").css("border-top-left-radius", "5px")
    $("#departments-menu-controls").css("-moz-border-radius-topright", "5px")
    $("#departments-menu-controls").css("-webkit-border-top-right-radius", "5px")
    $("#departments-menu-controls").css("border-top-right-radius", "5px")

    $("#departments-control-collapse").hide()
    $("#departments-control-expand").show()
}

function expandSlider() {
    $("#departments-menu-container").slideDown("fast")

    $("#departments-menu-controls").css("background-image", "url('http://3.img.shedstore.co.uk/slider/2/controls.png')")
    $("#departments-control-indicator").show()

    //-moz-border-radius-bottomleft:5px;-webkit-border-bottom-left-radius:5px;border-bottom-left-radius:5px;
    //-moz-border-radius-bottomright:5px;-webkit-border-bottom-right-radius:5px;border-bottom-right-radius:5px;
    $("#departments-menu-controls").css("-moz-border-radius-topleft", "0px")
    $("#departments-menu-controls").css("-webkit-border-top-left-radius", "0px")
    $("#departments-menu-controls").css("border-top-left-radius", "0px")
    $("#departments-menu-controls").css("-moz-border-radius-topright", "0px")
    $("#departments-menu-controls").css("-webkit-border-top-right-radius", "0px")
    $("#departments-menu-controls").css("border-top-right-radius", "0px")

    $("#departments-control-collapse").show()
    $("#departments-control-expand").hide()
}

