﻿///* ---- =VARS --*/

var ease = "easeOutExpo";
var vel = 400;
var speed = 800;

$(function () {

    /* ---- =SELECTORS --*/

    $('.paddy').addClass("clearfix");

    $('.contentHeader p:last-child, .contentHeader h1:only-child, #faq .ans p:last-child').addClass('remove');

    //$('#sec > li:last, #sec > li > ul > li:last').addClass("bord");

    $(".con-list, .list, #nav-foot-left, #nav-foot-right").find("li:last").addClass("remove");
    $("#panels > li:last, #mh .grid-group:last, #related .thumbs :last").addClass("remove");

    $("#con .colours li:nth-child(6n), #con .sizes li:nth-child(6n), #product-colours li:nth-child(11n), , #product-sizes li:nth-child(11n), .photos dd:nth-child(8n)").addClass("mr0"); // Removes margin on product colour and size options
    $("#yb-grid, #review").find("li:nth-child(2n)").addClass("mr0");
    $("#related").children(".thumbs").find("li:nth-child(5n)").addClass("mr0");

    $("#pref li:nth-child(2n)").addClass("neg");

    $(".bc ul").each(function () {
        $(this).children("li:last").addClass("mb0");
    });

    $("#primary .drop").each(function () {
        $(this).children(".col:last").addClass("remove");
    });

    $("#pro-desc .oa").each(function () {
        $(this).children(":only-child").addClass("mb0");
    });

    /* ---- =COLORBOX --*/

    $("#colorbox").appendTo('form:first');
    $(".modal, a[rel='gallery']").colorbox({ opacity: 0.9, close: "close window", speed: vel, overlayClose: false, fixed: false, transition: "elastic", initialWidth: "100", initialHeight: "100" });
    $(".colorbox").colorbox({ innerWidth: '650', innerHeight: '500', initialWidth: "100", initialHeight: "100", overlayClose: false });
    $(".change-address").colorbox({ inline: true, innerWidth: '650', innerHeight: '500', initialWidth: "100", initialHeight: "100", overlayClose: false });

    /* ---- =OPACITY --*/

    $(".off").css({ opacity: 0.3 });

    /* ---- =HOVERS --*/

    $(".con-head").hover(function () {
        $(this).toggleClass('acc-hov');
    });

    $(".sizes a:not(.off, .sing)").hover(function () {
        $(this).toggleClass('size-hov');
    });

    $(".colours li").hover(function () {
        $(this).addClass("over");
    }, function () {
        $(this).removeClass("over");
    });

    $("#pref li, .hl-img, .photos a").hover(function () {
        $(this).stop().fadeTo(vel, 0.7);
    }, function () {
        $(this).stop().fadeTo(vel, 1.0);
    });

    /* ---- =MENU DROP --*/

    $("#primary > li").hover(function () {
        if ($(this).children().size() > 1) {
            $(this).find(".hov").toggle();
            $(this).toggleClass('open');
        }
    });

    /* ---- =TIP --*/

    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
    } else {
        $('#views a, .product-r .sizes .off, .colours a, #related a').qtip({
            position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' }, adjust: { x: 0, y: -5} },
            show: { delay: 0 },
            hide: { when: 'mouseout', fixed: true, delay: 0 },
            content: {
                text: false
            },
            style: { classes: { tooltip: 'tip-top'} }
        });
    }

    /* ---- =FORMS --*/

    $("select").selectBox();

    $("input:checkbox").checkbox({
        cls: 'jquery-safari-checkbox',
        empty: '/img/blank.gif'
    });

    $("input:radio").checkbox({
        cls: 'jquery-safari-radio',
        empty: '/img/blank.gif'
    });

    /* ---- =FAQS --*/

    $("#faqs .faq-tog").click(function () {
        $(this).next().slideToggle(vel, ease);
        $(this).children('a').toggleClass('on');
        return false;
    });

    $("#faqs .faq-wrap").each(function () {
        if ($(this).children().size() < 1) {
            $(this).remove();
        }
    });

    $("input[type='text']:not(.gift-box, .tb-inline), textarea, #inputSearch").each(function () {
        if (!$(this).data("orig")) {
            $(this).data("orig", $(this).val());
            $(this).click(function () {
                $(this).select();
            });
            $(this).blur(function () {
                if ($(this).val() == "") {
                    $(this).val($(this).data("orig"));
                }
            });
        }
    });

    $("#back-to-top").click(function () {
        $('html,body').animate({ scrollTop: 0 }, 1000);
        return false;
    });

    /* ---- =SEARCH --*/

    $(document).click(function () {
        if (!hasFocus && !isScrolling) {
            $('#suggest').hide();
        }
    });

    $('#inputSearch').focus(function () {
        if ($(this).val().length > 2) {
            $('#suggest').show();
        }
        hasFocus = true;
    });

    $('#inputSearch').blur(function () {
        if (!hasFocus) {
            $('#suggest').hide();
        }
        hasFocus = false;
    });

    $('#inputSearch').keydown(function (e) {
        if (e.which || e.keyCode) {
            if ((e.which == 13) || (e.keyCode == 13)) {
                return false;
            }
            return true;
        }
    });

    $('#inputSearch').keyup(function (e) {
        if ($(this).val().length == 0) {
            $('#suggest').hide();
        }
        else if ($(this).val().length == 1) {
            globalGuid = guid();
        }

        if (e.keyCode != 40 && e.keyCode != 38 && e.keyCode != 13) {

            if ($(this).val().length > 2) {

                $.ajax({
                    type: "POST",
                    url: "/AJAX.asmx/SearchBox",
                    data: "{'SearchTerm':'" + $(this).val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        $('#suggest').html(msg.d).show();
                        if ($("#suggest .jspContainer").length == 0) {
                            $('#scroll-content').jScrollPane({ verticalDragMinHeight: 50, verticalDragMaxHeight: 50, showArrows: true });
                        }
                    }
                });

                $.ajax({
                    type: "POST",
                    url: "/AJAX.asmx/SearchBoxRecordInput",
                    data: "{'SearchGuid':'" + globalGuid + "','SearchTerm':'" + $(this).val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        //console.log(msg.d);
                    }
                });
            }
        }

    });

    /* ---- =BIG HIT --*/

    $("#slider li, #car li, #panels li:not(#nl, .video), #squares li").biggerlink({
        hoverclass: "over",
        otherstriggermaster: false
    });

});

$("#suggest .jspTrack").live("mouseover", function () {
    isScrolling = true;
});

$("#suggest .jspTrack").live("mouseout", function () {
    isScrolling = false;
});

///* ---- =SEARCH --*/

var globalGuid = guid();
var hasFocus = false;
var isScrolling = false;

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
