/**
* 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 nude = window.nude = function($) {

    var lightbox, overlay;

    function showStoreLocation(element) {
        var parent = $(element).parent().parent();
        return (
             '<div class="show_map">'
             + '<a href="#" class="lb_close grey_button">&laquo; Close</a><br /><br />'
			 + '<img src="' + element.href + '" alt="" />'
			 + '</div>'
		);
    }

    function showGiftWrap(element) {
        return (
             '<div class="show_gift_wrap">'
             + '<a href="#" class="lb_close grey_button">&laquo; Close</a><br /><br />'
			 + '<img src="' + element.href + '" alt="" />'
			 + '<br /><p class="gift_wrap_text">This beautiful pebble smooth gift box is sustainably sourced and produced. Your products are loving wrapped in monogrammed tissue and finished with a bow.</p>'
			 + '</div>'
		);
    }

    function showMutipleAddressForm(element) {
        var form = $("#multiple_address_form")
        var html = form.html();
        return (
			html + '<br /><br /><a href="#" class="lb_close grey_button">&laquo; Close</a>'
		);
    }

    function showWrongStore(element) {
        var form = $(element)
        var html = form.html();
        return (
			html
		);
    }

    function addToBasket(element) {
        if (!nude.isCheckoutAllowed) {
            return (
               '<div class="warning_mecca">'
               + '<img src="/img/common/logo_nude_dialog.png" alt="Nude" />'
               + '<p>Welcome to Nude Australia and New Zealand, we hope you enjoy browsing our website.<br /><br />If you would like to make a purchase, we take great pleasure in re-directing you to our retail partner <a href="http://www.meccacosmetica.com.au/brand-detail.aspx?b=82" target="_blank">Mecca Cosmetica</a></p>'
               + '<a href="#"><img src="/img/common/btn_back_to_nude.jpg" alt="" class="lb_close purple_button" /></a>'
               + '</div>'
           );
        }
        return (false);
    }

    function openLightBox(element, functionName) {
        var html = nude[functionName](element); // Public object call

        if (html) {

            lightbox.find("#lb_panel_content").html(html);
            lightbox.find(".lb_close").click(closeLightBox);

            overlay.show();
            lightbox.show();
        }
        else {
            return (false);
        }
    }

    function closeLightBox() {
        overlay.hide();
        lightbox.hide();
        return (false);
    }

    function addLightBox() {
        $("body > form").append(
		    '<div id="lb_overlay">'
			+ '<iframe src="/html/iframe.html" height="512px" width="512px" />'
			+ '</div>'
			+ '<div id="lb_panel">'
			+ '<div id="lb_panel_wrapper">'
			+ '<div id="lb_panel_content">'
			+ '</div>'
			+ '</div>'
			+ '</div>'
		);

        overlay = $("#lb_overlay").hide();
        //lightbox = $("#lb_panel").click(closeLightBox).hide();
        lightbox = $("#lb_panel").hide();
        //lightbox.find("#lb_panel_content").click(function() {
        //return (false);
        //});

        $(".lightbox").click(function() {
            if (openLightBox(this, $(this).attr("rel")) != false) {
                return (false);
            }
        });
    }


    function changeCountry() {
        $(".changeCountry").change(function() {
            var sc = $('#ctl00_cphMain_ddlCountries :selected').val();
            if (sc == 16 || sc == 159) {
                openLightBox("#warning_mecca", "showWrongStore");
            }
            else if (nude.siteRegion == 1 && sc != 1) {
                openLightBox('#wrong_store', "showWrongStore");
            }
            else if (nude.siteRegion == 2 && sc != 2) {
                openLightBox("#wrong_store", "showWrongStore");
            }
            else if ((nude.siteRegion == 3 || nude.siteRegion == 4) && (sc == 1 || sc == 2)) {
                openLightBox("#wrong_store", "showWrongStore");
            }
        });
    }


    function toggleBoxes() {

        $(".toggle_box").each(function() {

            var el = $(this);
            var button = el.children(".toggle_button");
            var link = button.wrapInner('<a href="#" class="with_arrow down"></a>').children("a");
            var content = el.children(".content");

            link.click(function() {
                content.toggle();  
                if (link.hasClass("down")) {
                    link.removeClass('down');
                } else {
                    link.addClass('down');
                }
                return (false);
            });

            link.click()
        });
    }

    function regulateRowImageHeights(imgSelector) {
        if (!imgSelector) {
            imgSelector = "img.regulate_height"
        }

        var rows = $(".grid_row:has(" + imgSelector + ")");
        rows.each(function() {

            // Examine heights on a row-by-row basis
            var row = $(this);
            var max_height = false;
            var imgs = row.find(imgSelector);

            // Get the tallest image height
            imgs.each(function() {
                var el_height = $(this).height();
                if (max_height < el_height) {
                    max_height = el_height;
                }
            });

            // Update all images as necessary
            imgs.each(function() {
                var el = $(this);
                var margin_top = max_height - el.height();
                if (margin_top) {
                    el.css("marginTop", margin_top + "px");
                }
            })
        });

    }



    function classInputToggle(selector) {
        $(selector).click(function() {

            var el = $(this);
            var next = el.next();

            if (el.is("[@type='checkbox']")) {
                if (el.is(":checked")) {
                    next.addClass(CLASSES.selected);
                } else {
                    next.removeClass(CLASSES.selected);
                }
            } else if (el.is("[@type='radio']")) {
                var others = el.parent().siblings().find("input[@name='" + el.attr("name") + "'] ~ span");
                others.removeClass(CLASSES.selected);
                if (el.is(":checked")) {
                    next.addClass(CLASSES.selected);
                }
            }

        });
    }

    return {
        // Public variables / methods
        "init": function() {

            toggleBoxes();
            addLightBox();
            changeCountry();

        },

        "addChangeClassToggleToSibling": classInputToggle,
        "regulateRowImageHeights": regulateRowImageHeights,

        "openLightBox": openLightBox,
        "showStoreLocation": showStoreLocation,
        "showGiftWrap": showGiftWrap,
        "showMutipleAddressForm": showMutipleAddressForm,
        "showWrongStore": showWrongStore,
        "addToBasket": addToBasket,

        "isCheckoutAllowed": true,
        "siteRegion": 1
    };

} (jQuery);