﻿$(document).ready(function () {


    /** NAV **/
    if (!$('#top-nav').data('spacing-set')) {
        var cWidth = 0;
        $('#top-nav .title').nextAll().each(function (i, child) {
            cWidth += $(child).outerWidth(true);
        });
        $('#top-nav li:first').next().addClass('first');
        $('#top-nav li:last').addClass('last');

        /* spread out the menu headings */
        var diff = (($('#top-nav').width() - $('#top-nav .title').outerWidth(true) - cWidth));
        var padd = Math.floor(diff / $('#top-nav .title').nextAll().length / 2);
        var lPadd = ((diff - ($('#top-nav .title').nextAll().length * (padd * 2))) / 2) - 1;
        $('#top-nav .title').nextAll().each(function (i, child) {
            if ($(child).hasClass('last')) {
                $(child).css({ 'padding-left': lPadd + padd + 'px' });
                $(child).children('a').css({ 'padding-right': (15 + lPadd + padd) + 'px' });
            } else {
                $(child).css({ 'padding-left': padd + 'px' });
                $(child).children('a').css({ 'padding-right': (15 + padd) + 'px' });
            }
        });
        $('#top-nav').data('spacing-set', true);

        $('#top-nav li').not('.title').bind('mouseenter', function (event) {
            clearTimeout($(this).parent().data('toid'));
            $(this).siblings().removeClass('hover');
            $('.sn', $(this).siblings()).hide();
            $(this).addClass('hover');
            $('.sn', this).fadeIn('fast');
        }).bind('mouseleave', function (event) {
            var self = $(this);
            clearTimeout(self.parent().data('toid'));
            self.parent().data('toid', setTimeout(function () {
                self.removeClass('hover');
                $('.sn', self.parent()).fadeOut('fast');
            }, 500)
	        );
        }).find('.sn').hide().css('visibility', 'visible');

        // reposition the nv drop downs to stay with the page container
        $('#top-nav li:not(.title)').find('.sn').each(function () {
            $(this).css({ 'visibility': 'hidden', 'display': 'block' });
            if ($('#top-nav').offset().left + $('#top-nav').width() < $(this).offset().left + $(this).outerWidth(true)) {
                $(this).css({ left: ((($(this).offset().left - ($('#top-nav').offset().left + $('#top-nav').width() - $(this).outerWidth(true))) + 3) * -1) + 'px' });
            }
            $(this).css({ 'visibility': 'visible', 'display': 'none' });
            $(this).hide();
        });
    }

    /**NAV**/


    /* Vertically align nav items */
    $("dl#nav > ul > li > a").each(function () {
        if ($(this).find("br").length == 0) {
            $(this).css("padding", "15px 18px");
        } else {
            $(this).css("padding", "7px 18px");
        }
    });

    /* Cycle hero images */
    $("div#carousel").cycle({
        pager: ".carousel-pag"
    });


    $(".product-container .bundle-product-gallery").cycle({
        timeout: 0,
        fx: "scrollLeft",
        after: function (currSlideElement, nextSlideElement, options) {
            slideIndex = options.currSlide;
            nextIndex = slideIndex + 1;
            prevIndex = slideIndex - 1;

            if (slideIndex == options.slideCount - 1) {
                nextIndex = 0;
            }
            if (slideIndex == 0) {
                prevIndex = options.slideCount - 1
            }
        }
    });



    // This code handles the enter button for the top login 
    $('#tbPassword').keypress(function (e) {
        if (e.keyCode == 13) {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("lbSubmit", "", true, "Login", "", false, true))

        }
    });




    var bundleCount = $("div.product-container .bundle-product-gallery > div").length;

    if (bundleCount > 1) {
        $(".product-nav-left").click(function () {
            $(".product-container .bundle-product-gallery").cycle(prevIndex, "scrollRight");

            return false;
        });

        $(".product-nav-right").click(function () {
            $(".product-container .bundle-product-gallery").cycle(nextIndex, "scrollLeft");

            return false;
        });
        $(".product-nav-left").show();
        $(".product-nav-right").show();
    }



    /*width of testimonial logo adjustor*/
    var imgWidth = $("div#testimonial-item div.logo img").width();
    var imgheight = $("div#testimonial-item div.logo img").height();
    var setWidth = 105;
    if (imgWidth > setWidth) {
        var height = imgheight - (((imgWidth - setWidth) / imgWidth) * imgheight);

        $("div#testimonial-item div.logo img").css("width", setWidth);
        $("div#testimonial-item div.logo img").css("height", height);

    }

    /* Tabs */
    function hideAll() {
        $("ul.tabs li a").each(function () {
            $($(this).attr("href")).hide(); // Hide all
            $(this).removeClass("selected");
            $(this).parent().removeClass("adjacent");
        });
    }

    hideAll();

    $("ul.tabs li:first a").each(function () {
        $($(this).attr("href")).show();
        $(this).addClass("selected");
        $(this).parent().next().addClass("adjacent");
    });

    $("ul.tabs li a").click(function () {
        hideAll();
        $($(this).attr("href")).show();
        $(this).addClass("selected");
        if ($(this).parent().hasClass("first") || $(this).parent().hasClass("middle")) {
            $(this).parent().next().addClass("adjacent");
        } else {
            $(this).parent().prev().addClass("adjacent-last");
        }
        return false;
    });

    /* Image gallery */
    $(".gallery-thumbs").cycle({
        fx: "scrollLeft",
        timeout: 0,
        after: function (currSlideElement, nextSlideElement, options) {
            slideIndex = options.currSlide;
            nextIndex = slideIndex + 1;
            prevIndex = slideIndex - 1;

            if (slideIndex == options.slideCount - 1) {
                nextIndex = 0;
            }
            if (slideIndex == 0) {
                prevIndex = options.slideCount - 1
            }
        }
    });

    /* Init */
    $(".gallery-thumbs img:first").addClass("selected");

    var total = $(".gallery-thumbs div").length;
    if (total > 1) {

        $(".gallery-nav-left").click(function () {
            $(".gallery-thumbs").cycle(prevIndex, "scrollRight");
            return false;
        });

        $(".gallery-nav-right").click(function () {
            $(".gallery-thumbs").cycle(nextIndex, "scrollLeft");
            return false;
        });

    } else {

        $(".gallery-nav-left, .gallery-nav-right").each(function () {
            $(this).css("background-position", "0 -73px");
        }).click(function () {
            return false;
        });

    }

    $(".gallery-thumbs a").click(function () {
        /* Add selected state */
        $(".gallery-thumbs img").removeClass("selected");
        $(this).children("img").addClass("selected");
        /* Change larger img */
        var med = $(this).attr("href");
        //var lg = med.replace(/medium/gi, '');
        $(".gallery img").fadeOut("fast", function () {
            $(this).remove();
            $("<img src='" + med + "' alt='' />").appendTo(".gallery");
            $(".gallery img").css("display", "none").fadeIn("slow");
        });

        //var caption = $(this).attr("title");
        //$("div.caption p").text(caption);
        return false;
    });

    $(".gallery img").live("mouseover mouseout", function (e) {
        if (e.type == 'mouseover') {
            $("div.caption").fadeIn("slow");
        } else if (e.type == 'mouseout') {
            $("div.caption").fadeOut("slow");
        }
    });

    /* Numeric only inputs */
    $(".numeric").keydown(function (event) {
        if (event.keyCode == 46 || event.keyCode == 8) {
        } else {
            if (event.keyCode < 95) {
                if (event.keyCode < 48 || event.keyCode > 57) {
                    event.preventDefault();
                }
            } else {
                if (event.keyCode < 96 || event.keyCode > 105) {
                    event.preventDefault();
                }
            }
        }
    });

    $(".numeric").focus(function () {
        if ($(this).val() <= 0) {
            $(this).val(1);
        }
    }).blur(function () {
        if ($(this).val() <= 0) {
            $(this).val(1);
        }
    });

    $(".add").click(function () {
        var input = $(this).parent().find("input.numeric");
        var val = input.val();
        val++;
        input.val(val);
    });

    $(".subtract").click(function () {
        var input = $(this).parent().find("input.numeric");
        var val = input.val();
        val--;
        if (val <= 0) {
            val = 1;
            input.val(val);
        } else {
            input.val(val);
        }

    });

 

    /* Equal height cols for .product-feature */
    var tallest = 0;
    $('.product-feature').each(function () {
        if ($(this).height() > tallest)
            tallest = $(this).height();
    });
    if (tallest > 0) {
        $('.product-feature').css('height', tallest);
    }

});

$(window).load(function () {


    // create custom animation algorithm for jQuery called "drop" 
    $.easing.drop = function (x, t, b, c, d) {
        return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
    };


    // loading animation
    $.tools.overlay.addEffect("drop", function (css, done) {

        // use Overlay API to gain access to crucial elements
        var conf = this.getConf(),
       overlay = this.getOverlay();

        // determine initial position for the overlay
        if (conf.fixed) {
            css.position = 'fixed';
        } else {
            css.top += $(window).scrollTop();
            css.left += $(window).scrollLeft();
            css.position = 'absolute';
        }

        // position the overlay and show it
        overlay.css(css).show();

        // begin animating with our custom easing
        overlay.animate({ top: '+=55', opacity: 1, width: '+=20' }, 400, 'drop', done);

        /* closing animation */
    }, function (done) {
        this.getOverlay().animate({ top: '-=55', opacity: 0, width: '-=20' }, 300, 'drop', function () {
            $(this).hide();
            done.call();
        });
    }
);

    /* Enlarge Image */
    $("a.enlarge").click(function () {
        var prevItem = $("a.enlarge").parents(".mainimg").children("img:first");
        var itemPathLength = prevItem.attr("src").length;
        var itemNo = prevItem.attr("src").substr(itemPathLength - 7, 1);

      //  $('div.apple_overlay div.content').html("<img src='/" + eval("imageInfo.cv_noColor.sz_default.img_" + itemNo + ".Large") + "'/>");
        $('div.apple_overlay div.content').html("<img src='/" + $("#imgFirstLarge").attr("src") + "'/>");
        $('div.apple_overlay').show();


        $('div.apple_overlay').overlay({
            effect: 'drop',
            mask: '#789',
            load: true
        });
        $("#exposeMask").show();
    });

    $("div.apple_overlay a.closer").click(function () {
        $('div.apple_overlay').hide();
        $("#exposeMask").hide();
    });


    //    $("a.enlarge").colorbox({ width: "50%", inline: true, href: "#inline_example1" });

    /* Equal height product columns */
    /* This happens on window.load because webkit can't get the width of the images on document.ready */

    $(".right-col ul.product-listings").each(function () {
        var tallest = 0;
        $(this).children("li").each(function () {
            var theHeight = $(this).height();
            if (theHeight > tallest) {
                tallest = theHeight;
            }
            $(this).children("#price-box").addClass("price-box");
        });



        $(this).find("li").each(function () {

            var setWidth = 155;

            if ($(this).parent().hasClass("three")) {

                setWidth = 200;

            }

            var imgWidth = $(this).find("img").width();

            var imgheight = $(this).find("img").height();

            if (imgWidth > setWidth) {


                var height = imgheight - (((imgWidth - setWidth) / imgWidth) * imgheight);

                $(this).find("img:first").css("width", setWidth);

                $(this).find("img:first").css("height", height);

            }

        });

        if (tallest > 0) {

            $(this).children("li").css("height", tallest);
        }


    });

    var tallestImg = 0;
    $(".hire ul.product-listings").each(function () {
        $(this).find("div").each(function () {
            var theImgHeight = $(this).height();
            if (theImgHeight > tallestImg) {
                tallestImg = theImgHeight;
            }
        });
        if (tallestImg > 0) {
            $(this).find("div").css("height", tallestImg);
        }
    });

    $(".hire ul.product-listings").each(function () {
        var tallest = 0;
        $(this).children("li").each(function () {
            var theHeight = $(this).height();
            if (theHeight > tallest) {
                tallest = theHeight;
            }
        });
        if (tallest > 0) {
            $(this).children("li").css("height", tallest);
        }
    });

});
