﻿/*********Wilson Specific***************************************/
$(document).ready(function() {


    /*******************Features************************/
    //try { loadRelated(); } catch (err) { }
    try { loadLinks(); } catch (err) { }
    //try { } catch (err) { }
    topnav();
    loadCartLinks();
    try { showFeatures(); } catch (err) { }
    try { autoComplete() } catch (err) { }
    showHideProduct();
    $('.slideshowsingle').css({ visibility: "visible" });

    loadCartSave();
    $('.slideshowsingle').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 6000,
        pause: 1
    });
});
function loadCartLinks() {
    $('#logindwn').unbind("click");
    $('#logindwn').click(function() {
        //var list = $(this).parent().find("ul");
        $('#minicart').hide();
        $('#login').toggle(268);
    });
    $('#cartdwn').unbind("click");
    $('#cartdwn').click(function() {
        //var list = $(this).parent().find("ul");
        $('#login').hide();
        $('#minicart').toggle(268);
    });
}
function loadCartSave() {
    $('#ctl04_ctl00_btnSavePop').click(function() {
        $('#saveCartAs').css({ marginTop: ($(window).scrollTop() + ($(window).height() / 2) - 44) + 'px', display: 'block' });
        return false;
    });
}
function formatCurrency(amount) { var i = Math.ceil(parseFloat(amount) * 100) / 100; if (isNaN(i)) { i = 0.00; } var minus = ''; if (i < 0) { minus = '-'; } i = Math.abs(i); i = parseInt((i + .005) * 100); i = i / 100; s = new String(i); if (s.indexOf('.') < 0) { s += '.00'; } if (s.indexOf('.') == (s.length - 2)) { s += '0'; } s = minus + s; return s; }
function swapImg(id, imgname) {
    //alert("swapping");
    //alert($('#' + id).src);
    $('#' + id).attr('src', imgname);
}

function openClose(id) {
    if ($('#' + id).css("display") == "none") {
        $('#' + id).css("display", "block");
    } else {
        $('#' + id).css("display", "none");
    }

}
function hideMe(id) { $('#' + id).css("display", "none"); }

function loadRelated() {
    var relatedboxCount = $("#relatedbox ul li").size();
    if (relatedboxCount > 2) {
        $("#relatedbox").jCarouselLite({ auto: 5000, speed: 1000, circular: true, btnNext: ".right", btnPrev: ".left", mouseWheel: true });
    } 
    $("#relatedbox").show();
}
function loadLinks() {
    $('.show-product').click(function() { $('#hid-product-state').val('showing'); });
    
}
function topnav() {
    $("#topnav ul").css({ display: "none" }); // Opera Fix
    $("#topnav li").hover(function() {
        $(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(400);
        $(this).find('a:first').addClass('hover');
    }, function() {
        $(this).find('ul:first').css({ visibility: "hidden" });
        $(this).find('a:first').removeClass('hover');
    });
    $("#topnav a").removeAttr("title");
}
function showFeatures() {
    var featuresCount = $("#specialbox ul li").size();
    if (featuresCount > 2) {
        $("#specialbox").jCarouselLite({ auto: 5000, speed: 1000, circular: true, btnNext: ".right", btnPrev: ".left", mouseWheel: true });
    }
}
function leftNav() {

    $(document).ready(function() {
        $("#" + leftNaveId).addClass("active");
        if ($("#hd-hardware").attr("class") == "active") {
            $("ul#productgroups li#a2").show();
        } else {
            $("ul#productgroups li#a1").show();
        }
        $("#hd-timber").click(function() {
            $("ul#productgroups li#a2").hide();
            $("ul#productgroups li#a1").show();
            $(this).toggleClass("active");
            $("#hd-hardware").toggleClass("active");

        });
        $("#hd-hardware").click(function() {
            $("ul#productgroups li#a1").hide();
            $("ul#productgroups li#a2").show();
            $(this).toggleClass("active");
            $("#hd-timber").toggleClass("active");
        });
        return false;
    });
}
function autoComplete() {
    $(loadSearchName()).autocomplete("webservice/ProductSearchAutoComplete.asmx/SuggestedProducts", {
        parse: function(data) {
            var parsed = [];

            $(data).find("string").each(function() {
                parsed[parsed.length] = {
                    data: [$(this).text()],
                    value: $(this).text(),
                    result: [$(this).text()]
                };
            });
            return parsed;
        },
        dataType: "xml"
    });
}
function showHideProduct() {
    //alert($('#hid-product-state').val());
    if ($('#hid-product-state').val() == 'showing') {

        $('#product-back').css({ height: $(document).height(), width: $(document).width(), display: 'block' });
        $('#product-view').css({ marginTop: ($(window).scrollTop() + ($(window).height() / 2) - 189)+ 'px', display:'block' });
        $('#product-shadow').css({ marginTop: ($(window).scrollTop() + ($(window).height() / 2) - 199) + 'px', height: $('#product-view').outerHeight()+10 + "px", display: 'block' });

        loadRelated();
    } else {
        $('#product-back').hide();
        $('#product-shadow').hide();
        $('#product-view').hide();
    }
}
function ValidateAndSubmit(evt) {
    // Ascend from the button that triggered this click event 
    //  until we find a container element flagged with 
    //  .validationGroup and store a reference to that element.
    var $group = $(evt.currentTarget).parents('.validationGroup');

    var isValid = true;

    // Descending from that .validationGroup element, find any input
    //  elements within it, iterate over them, and run validation on 
    //  each of them.
    $group.find(':input').each(function(i, item) {
        if (!$(item).valid()) {
            isValid = false;
        }
    });

    // If any fields failed validation, prevent the button's click 
    //  event from triggering form submission.
    if (!isValid)
        evt.preventDefault();
}
