﻿/* selfPopup
 * open popup window with href determined from the href attribute on the anchor:
 * <a href="index.html" onclick="return selfPopup(this,300,400);">Index</a>
 * dominic winsor
 */
function selfPopup( loc, x, y ) { //urgh
   LeftPosition = (screen.width) ? (screen.width-x)/2 : 0;
   TopPosition = (screen.height) ? (screen.height-y)/2 : 0;
   window.open(loc.href,"","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width="+x+",height="+y+',left='  +LeftPosition +  ',top='  +TopPosition +"");
   return false;
}

function showDeliveryRegions() {
    
    var ref = document.getElementById('basketDeliveryRegions');
    ref.style.display = 'block';
    return false;
}

function selectIndexByValue( ele, value ) {
    ele = document.getElementById(ele);
    for(var i=0; i<ele.options.length; i++) {
        ele.options[i].selected = (ele.options[i].value==value);
    }
}

//nav flyout :Dan Wellman 2010
(function ($) {
    
    //vars!
    var jackets = $(".nav-jackets"),
        flyout = jackets.find("#drop"),
        productParent = $("#hs-col-center-top"),
        thumbs = productParent.find(".additional-photos");

    jackets.mouseenter(function() {
        //show flyout
        flyout.stop(true, true).show();
    });

    jackets.mouseleave(function(e) {
        //hide it
        flyout.fadeOut();  
    });

    //thumbnail slider for product page
    if (thumbs.length) {
        
        var links = thumbs.children().not(".ui"),
            tot = links.length * 91,
            maxDistance = tot - (3 * 91),
            wrap = $("<div></div>", {
                id: "slider"
            }).css("width", tot);

        //add active class to first image
        $(links[0]).addClass("active");
        
        //init slider if more than 3 links/images
        if (tot > 273) {
            //add wrapper
            links.wrapAll(wrap);
            
            //enable right button
            $("#right a").removeClass("disabled").addClass("enabled").attr("title", "Move Right");
        };

        //don't do anything when arrows disabled
        $(".ui").delegate("#left a.disabled, #right a.disabled", "click", function(e) {
            e.preventDefault();
            return false;
        });

        //click handler for right arrow
        $(".ui").delegate("#right a.enabled", "click", function(e) {
            e.preventDefault();

            //move slider left by 1 image
            $("#slider").animate({
                marginLeft: "-=91px"
            }, "slow", function() {
                //enable left arrow
                ($("#left a").hasClass("disabled")) ? $("#left a").removeClass("disabled").addClass("enabled").attr("title", "Move Left") : null;
                
                //if at max disable right arrow
                if (-parseInt($(this).css("marginLeft"), 10) === maxDistance) {
                    $("#right a").removeClass("enabled").addClass("disabled").attr("title", "");
                }
            });
        });

        $(".ui").delegate("#left a.enabled", "click", function(e) {
            e.preventDefault();

            //move slider right by 1 image
            $("#slider").animate({
                marginLeft: "+=91px"
            }, "slow", function() {
                //if back to the start disable left arrow
                if ($(this).css("marginLeft") === "0px") {
                    $("#left a").removeClass("enabled").addClass("disabled").attr("title", "");

                }

                //enable right arrow again
                if (parseInt($(this).css("marginLeft"), 10) < 0 && -parseInt($(this).css("marginLeft"), 10) < maxDistance) {
                    $("#right a").removeClass("disabled").addClass("enabled").attr("title", "Move Right");
                }
            });
        });
    }

    //move active class to clicked image
    if (links) {
        links.click(function(e) {
            e.preventDefault();

            $(".active").removeClass("active");
                
            $(this).addClass("active");
        });
    };
})(jQuery);
