
var Girls = {

    constructor: function () {

        //Setup navigation using the igloonav plugin
        $('div.nav > ul').igloonav({
            easing: 'easeOutExpo'
        });

        if ($('#carousel').length) {
            $('#carousel').anythingSlider({
                width: 960,
                height: 380,
                // autoPlay: false,
                delay: 8000,
                hashTags: false,
                animationTime: 1000,
                buildArrows: false,
                easing: 'easeInOutExpo'
            });
        }

        //Setup prettyPhoto for any links that use rel="prettyPhoto"
        if ($("a[rel^='prettyPhoto']").length)
            $("a[rel^='prettyPhoto']").prettyPhoto({ animation_speed: 'normal', theme: 'facebook', show_title: false });

        $(".button-login-to-your-page").click(function (e) {
            e.preventDefault();
            $("#register-dropdown").toggle();
        });

        var hide;

        $("#LoginToYourPage").bind("mouseleave", function (e) {
            e.stopPropagation();
            hide = setTimeout(function () {
                $("#register-dropdown").hide();
            }, 1000);
        });

        $("#LoginToYourPage").bind("mouseenter", function () {
            clearTimeout(hide);
        });

        $(".snapsis-top-menu").bind("mouseenter", function () {
            $("#register-dropdown").hide();
        });

        var timer;

        $("div.leader ul.nav li:not(.social) a").each(function (index, value) {

            $(value).click(function (e) {

                $("div.leader ul.nav li").removeClass("active pink-border");

                var dropdown = $("div.leader div." + $(value).attr("rel"));
                var showing = $(dropdown).is(":visible");

                dropdown.bind("mouseleave", function (e) {
                    e.stopPropagation();
                    timer = setTimeout(function () {
                        dropdown.hide();
                        $("div.leader ul.nav li").removeClass("active pink-border");
                    }, 1000);
                });

                dropdown.bind("mouseenter", function () {
                    clearTimeout(timer);
                });

                $("div.leader div.dropdown").hide();

                if (!showing) {
                    $("div.leader div." + $(value).attr("rel")).show();
                    $(value).parent().addClass("active").next().addClass("pink-border");
                }

                e.preventDefault();

            });

        });

        /*
        State Selects
        --
        This powers the all various state select drop-downs around the site.
        */

        // Defining our state select actions (and their respective URLs here)
        // Values posted in these URLs represented as {value} placeholders in here...
        var actions = {
            register: 'https://secure.cancercouncilfundraising.org.au/registrant/LoginRegister.aspx?eventid={value}&action=register',
            donate: 'https://secure.cancercouncilfundraising.org.au/registrant/startup.aspx?eventid={value}&getpage=donate',
            find: 'https://secure.cancercouncilfundraising.org.au/registrant/startup.aspx?eventid={value}&getpage=tabbedIndividualSearch',
            curves: '/OurGirls/CurvesEvents.aspx'
        };

        // Defining properties of our various state select interface instances
        // containerSelector is the selector of a parent element to the <select>
        var interfaces = [
	        {
	            containerSelector: '.host-a-night',
	            triggerSelector: 'a.button-go',
	            action: 'register'
	        },
	        {
	            containerSelector: '.donate',
	            triggerSelector: 'a.button-go',
	            action: 'donate'
	        },
	        {
	            containerSelector: '.find-an-event',
	            triggerSelector: 'a.button-go',
	            action: 'find'
	        }
	    ];

        // loop through each instance and assign event handler
        var instance, $elem;
        for (var i = 0, j = interfaces.length; i < j; i++) {
            instance = interfaces[i];
            $elem = $(instance.containerSelector);
            if ($elem.length) {
                $elem.find(instance.triggerSelector).bind('click', instance, handleState);
            }
        };

        // our event handler
        function handleState(e) {
            e.preventDefault();
            var v = $(e.data.containerSelector).find('select').attr('value'),
	            url = actions[e.data.action];

            // console.log(v);

            if (v == '') return;

            // yes, we are testing for a condition of the Curves value ('curves') && the action 'find' so we can
            // use a different URL specific for that condition. Yay!
            if (v === 'curves') {
                url = actions.curves;
            }

            url = parseActionURL(url, v);
            window.location.href = url;
        }

        // injects selected value into passed url
        function parseActionURL(url, value) {
            return url.replace('{value}', value);
        }

        // fix white-space:nowrap issue that is injected by DNN
        $(".left-bottom-pane td").css("white-space", "normal");

        // fix skip link in the blog
        $("#dnn_ctr391_ViewCategories_tvCategories_SkipLink").hide();

        // fix form width
        $("#dnn_ctr403_FormMaster_C5SLLC_MainFormTable").width("440px");

    }

}

$(function() {
	
	//On document load run the Girls Night In constructor
	Girls.constructor();
	
});
