/**
 * Namespaced object. Encapsulating private methods, exposing these 
 * as public via an object proxy. 
 *
 * @author 		Zone
 * @email		info@zonecontent.com
 * @url 		http://www.zonecontent.com/
 * @copyright 	Copyright (c) 2009, zonecontent.com. All rights reserved.
 * @version		0.0.1
 */
var namespace = window.welshfarmhouse = function($) {

    // Private methods
    var zoomFull, zoomImg, zoomWrap, zoomArea, zoomBox, zoomRatioX, zoomRatioY, zoomOffset, zoomAreaMaxX, zoomAreaMaxY;
    function onZoomMouseMove(event) {
        var x = event.pageX - zoomOffset.left
        y = event.pageY - zoomOffset.top;

        zoomArea.css({
            left: Math.max(0, Math.min(zoomAreaMaxX, x - (zoomArea.width() / 2))),
            top: Math.max(0, Math.min(zoomAreaMaxY, y - (zoomArea.height() / 2)))
        });

        zoomBox.css({
            backgroundPosition: "-" + Math.max(0, (x * zoomRatioX) - (80 * zoomRatioX)) + "px -" + Math.max(0, (y * zoomRatioY) - (50 * zoomRatioY)) + "px"
        });
    }

    function getTotalElementPadding(el, paddingTypes) {
        var total = 0;
        for (var i = 0; i < paddingTypes.length; i++) {
            total += Number(el.css("paddingRight").split("px")[0]);
        }
        return (total);
    }

    function openZoomWindow(fullImageSrc) {

        // Configure proportional image variations
        function imageLoadCallback() {
            zoomRatioX = zoomWrap.width() / zoomArea.width();
            zoomRatioY = zoomWrap.height() / zoomArea.height();
            zoomOffset = zoomWrap.offset();
            zoomAreaMaxX = zoomFull.width() + getTotalElementPadding(zoomFull, ["paddingRight", "paddingLeft"]) - zoomArea.width() - 2;
            zoomAreaMaxY = zoomFull.height() + getTotalElementPadding(zoomFull, ["paddingTop", "paddingBottom"]) - zoomArea.height() - 1;

            // Set size of 
            zoomArea.css({
                height: 100,
                width: 100
            });
            zoomArea.show();

            // Show box
            if ($.browser.msie && ($.browser.version < 7 || $.browser.version == 7)) {
                var fieldsetHeight = $("fieldset.product").height()
                zoomBox.css({
                    left: "358px",
                    top: 74 + fieldsetHeight + 29 + "px"
                });
            } else {
                zoomBox.css({
                    left: "358px",
                    top: "74px"
                });
            }

            zoomBox.show();
        }

        // Apply image
        var src = String(fullImageSrc).replace("_280.jpg", ".jpg");
        zoomBox.css({ backgroundImage: "url(" + src + ")" });

        // Load image in preloading call
        var img = new Image();
        img.src = src;
        img.onLoad = imageLoadCallback();
    }

    function closeZoomWindow(event) {
        zoomBox.hide();
        zoomArea.hide();
    }

    return {

        initProductZoom: function() {

            // Define and add elements
            zoomFull = $("#product-img-full");
            zoomImg = zoomFull.children("img");
            zoomWrap = zoomImg.wrap('<div id="product-zoom-wrap"></div>').parent();
            zoomBox = zoomWrap.parent().append('<div id="product-zoom-box"></div>').children(":last");
            zoomArea = zoomWrap.append('<div id="product-zoom-area"></div>').children(":last");

            // Resize the wrap for overflow
            zoomWrap.height(zoomImg.height());
            zoomWrap.width(zoomImg.width());

            // Hide those
            zoomBox.hide();
            zoomArea.hide();

            // Delegate event methods
            zoomWrap.mouseover(function(event) {
                openZoomWindow(zoomImg.attr("src"));
                zoomWrap.bind("mousemove", onZoomMouseMove);
                zoomArea.bind("mousemove", onZoomMouseMove);
            });
            zoomWrap.mouseout(function(event) {
                closeZoomWindow();
                zoomWrap.unbind("mousemove", onZoomMouseMove);
                zoomArea.unbind("mousemove", onZoomMouseMove);
            });

        },

        initProductVariations: function() {

            var img = $("#product-img-full img");
            $("#product-variations .grid_row .grid_4 a").each(function() {
                var el = $(this);
                el.click(function() {
                    var imghref = $("#product-img-full img").attr("src");
                    var imgalt = $("#product-img-full img").attr("alt");
                    var eli = $("img", this);
                    var elspan = $("span", this);
                    img.attr("src", el.attr("href"));
                    img.attr("alt", elspan.text());
                    el.attr("href", imghref);
                    eli.attr("src", imghref.replace("_280.jpg", "_80.jpg"));
                    eli.attr("alt", imgalt);
                    elspan.html(imgalt);
                    return (false);
                });
            });

        },

        initCarousel: function(images) {

            var outer = $("#homepage_promo_box"),
			inner = outer.find("#homepage_promo_box_inner"),
			int = setInterval(setCarouselImage, 6000);

            function setCarouselImage() {
                var b = "backgroundImage";
                for (var i = 0; i < images.length; i++) {

                    if (String(inner.css(b)).indexOf(images[i]) > -1) {

                        // Set new images
                        outer.css(b, 'url(' + images[i] + ')');
                        var img = (i >= images.length - 1) ? images[0] : images[i + 1];
                        inner.css(b, 'url(' + img + ')');

                        // Transition
                        var msie = false/*@cc_on || @_jscript_version < 5.7@*/;
                        if (msie == false) {
                            inner.css("opacity", .1);
                            inner.animate({
                                "opacity": .99
                            }, 2048);
                        }
                        break;
                    }
                }
            }
        },

        handleSearchLabel: function() {

            $("#ctl00_txtSearch").focus(function() {

                $(this).filter(function() {

                    // We only want this to apply if there's not
                    // something actually entered
                    return $(this).val() == "" || $(this).val() == "Search products"

                }).removeClass("watermark").val("");

            });

            $('#ctl00_txtSearch').blur(function() {

                if ($(this).val() == "") {
                    $(this).val("Search products");
                    $(this).addClass("watermark");
                }

            });

        }

    };

} (jQuery);