function checkForSubmit(event,btn)
{
	if (event.keyCode == 13)
	{
		event.returnValue = false;
		event.cancel = true;    
		document.getElementById(btn).click();
		return false;
	}
	else
	{
		return true;
	}    
}

revealSelectedSubNav = function()
{
    $("#nav li.selected").each(function(index)
    {
        $(this).children("ul").addClass("visible");
    });
}

hideSelectedSubNav = function()
{   
    clearInterval(hide_interval);
    if (hide_selector != null)
    {
        hide_selector.children("ul").removeClass("visible");
    }
    $("#nav li.selected").each(function(index)
    {
    	$(this).children("ul").removeClass("visible");
    });
}

var hide_interval = 0;
var hide_selector;

hideSubNav = function ()
{
    clearInterval(hide_interval);
    if (hide_selector != null)
    {
        hide_selector.children("ul").removeClass("visible");
        revealSelectedSubNav();
    }
}

$(document).ready(
	function()
	{
	    revealSelectedSubNav();
	    
	    $("#nav li").mouseover(function () {
	        hideSubNav();
	    });
	    
	    $("#nav li").not("#nav li.selected").mouseover(function () {
	        var sub_nav = $(this).children("ul");
	        if (sub_nav.length > 0)
	        {
	            clearInterval(hide_interval);
	            hideSelectedSubNav();
	            sub_nav.addClass("visible");
	        }
	    });
	    $("#nav li").mouseout(function (evt) {
	        clearInterval(hide_interval);
	        //var selector = $(this)[0];
	        var sub_nav = $(this).children("ul");
	        if (sub_nav.length > 0)
	        {
	            hide_selector = $(this);
	            hide_interval = setInterval("hideSubNav()",1000);
	        }
	    });	   
	}
);

(function($) {
    $.fn.hint = function(blurClass) {
        if (!blurClass) {
            blurClass = 'blur';
        }
        return $(".hint").each(function() {
            // get jQuery version of 'this'
            var $input = $(this),

            // capture the rest of the variable to allow for reuse
			title = $input.attr('title'),
			$form = $(this.form),
			$win = $(window);

            remove = function() {
                if ($input.val() === title && $input.hasClass(blurClass)) {
                    $input.val('').removeClass(blurClass);
                }
            }
            // only apply logic if the element has the attribute
            if (title) {
                // on blur, set value to title attr if text is blank
                $input.blur(function() {
                    if (this.value === '') {
                        $input.val(title).addClass(blurClass);
                    }
                }).focus(remove).blur(); // now change all inputs to title

                // clear the pre-defined text when form is submitted
                $form.submit(remove);
                $win.unload(remove); // handles Firefox's autocomplete
            }
        });
    };
})(jQuery);

$(function() {
    $('input[title!=""]').hint();
});

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);