// JavaScript Document
$(document).ready(function() {




    $("#mapContainer").css("display", "none");
    //trigger teh map after a postal code
    $('#mapGO').click(function() {
        var postalCodeSent = $("#" + mapAddressID).val();
        //alert (postalCodeSent);
        //find this function in the gMaps_forms.js
        showAddress(postalCodeSent);
        //set up the array 
        var detailsArray = "";

        result = $.getJSON(
					"../WebServices/GetClubsByPostalCode.ashx",
					{ PostalCode: postalCodeSent },

					function(data) {
					    var options = "";

					    options += "<option value=\"\">Select a club...</option>";

					    for (var i = 0; i < data.Clubs.length; i++) {
					        options += "<option value=\"" + data.Clubs[i].Lat + "_" + data.Clubs[i].Long + "_" + data.Clubs[i].ClubNo + "\">" + data.Clubs[i].ClubName + "</option>";
					        placeClubMarker(data.Clubs[i].Lat, data.Clubs[i].Long, data.Clubs[i].ClubNo, data.Clubs[i].ClubName, data.Clubs[i].Address1, data.Clubs[i].City, data.Clubs[i].Prov, data.Clubs[i].Gender);

					        //build an array so we can access these once they've clicked
					        //detailsArray[i] = data.Clubs[i].Email+"_"+data.Clubs[i].Address1+"_"+data.Clubs[i].Address2+"_"+data.Clubs[i].City+"_"+data.Clubs[i].Prov+"_"+data.Clubs[i].Postal+"_"+data.Clubs[i].Phone+"_"+data.Clubs[i].Fax+"_"+data.Clubs[i].Gender
					        //use any -- Email, Address1, Address2, City, Prov, Postal, Phone, Fax, Gender

					    }
					    $("#mapClub").html(options);
					    //alert (options);

					    $("select#mapClub option:first").attr("selected", "selected");
					});


        //showAddress(postalCodeSent);
        $("#mapContainer").show();

    });

    // Initialize history plugin.
    // The callback is called at once by present location.hash. 
    $.historyInit(pageload, "");

    // set onlick event for buttons
    $("a[rel='history']").click(function() {
        // 
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        // moves to a new page. 
        // pageload is called at once. 
        // hash don't contain "#", "?"
        $.historyLoad(hash);

        return false;
    });



    //hide the friends list on load
    $("#friendsList").css("display", "none");
    //animate the showing of three boxes on click
    $("#ctl00_ContentPlace_BodyCopy_share_friend").click(function() {

        // If checked
        if ($("#ctl00_ContentPlace_BodyCopy_share_friend").is(":checked")) {
            //show the hidden div
            $("#friendsList").show();
        } else {
            //otherwise, hide it 
            $("#friendsList").hide();
        }
    });

    //cra agendy fields
    $("#revCanDiv").css("display", "none");
    $("#memberDiv").css("display", "none");
    //animate the showing of three boxes on click
    $("#ctl00_ContentPlace_BodyCopy_not_profit").click(function() {

        // If checked
        if ($("#ctl00_ContentPlace_BodyCopy_not_profit_0").is(":checked")) {
            //show the hidden div
            $("#revCanDiv").show();
        } else {
            //otherwise, hide it 
            $("#revCanDiv").hide();
        }
    });

    $("#ctl00_ContentPlace_BodyCopy_member").click(function() {
        // If checked
        if ($("#ctl00_ContentPlace_BodyCopy_member_0").is(":checked")) {
            // show the hidden div
            $("#memberDiv").show();
        } else {
            // otherwise, hide it
            $("#memberDiv").hide();
        }
    });

    function setArrowDirection(currentDisplay, imageName) {

        if (currentDisplay == 'block') {
            $("#" + imageName).attr("src", "/img/arrow.redDown.png");
        } else {
            $("#" + imageName).attr("src", "/img/arrow.red.png");
        }

    }



    /* modal js ##-----------------------------------------## */

    //find out whether the user has chosen a province yet 
    var userPickedProv = readCookie("UserProvince");
    //alert (userPickedProv);
    if (userPickedProv == null) {
        $('a[name=modal]').trigger('click');
    }


    //select all the a tag with name equal to modal  
    $('a[name=modal]').click(function(e) {
        //hide the video player
        $('#slides').hide();
        //Cancel the link behavior  
        e.preventDefault();
        //Get the A tag  
        var id = $(this).attr('href');

        //Get the screen height and width  
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen  
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect       
        $('#mask').fadeIn(500);
        $('#mask').fadeTo("fast", .9);

        //Get the window height and width  
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center  
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect  
        $(id).fadeIn(2000);

    });

    //if close button is clicked  
    $('.window .close').click(function(e) {
        //Cancel the link behavior  
        e.preventDefault();
        $('#mask, .window').hide();
        $('#slides').show();
    });

    //if mask is clicked  
    $('#mask').click(function() {
        $(this).hide();
        $('.window').hide();
        $('#slides').show();
    });
});


function setSplashRegion(prov) {

    $('#mask').hide();
    $('.window').hide();
}


/* drop naviagtion animations */

var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;


function nav_open() {

    nav_canceltimer();
    nav_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
    //alert(ddmenuitem);
}

function nav_close() {
    //alert((ddmenuitem));
    if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function nav_timer()
{ closetimer = window.setTimeout(nav_close, timeout); }

function nav_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(document).ready(function() {
    $('.hidden').hide();


    $('#nav > li').bind('mouseover', nav_open);
    $('#nav > li').bind('mouseout', nav_timer);

    $('#regionSplash > li').bind('mouseover', nav_open);
    $('#regionSplash > li').bind('mouseout', nav_timer);


});

document.onclick = nav_close;


$("#amenLink01").click(function() {
    //alert('clicked');
    $("#amen01").toggle();
    var current = $("#amen01").css("display");
    setArrowDirection(current, "amen01Arrow");
});
$("#amenLink02").click(function() {
    $("#amen02").toggle();
    var current = $("#amen02").css("display");
    setArrowDirection(current, "amen02Arrow")
});
$("#amenLink03").click(function() {
    $("#amen03").toggle();
    var current = $("#amen03").css("display");
    setArrowDirection(current, "amen03Arrow")
});
$("#amenLink04").click(function() {
    $("#amen04").toggle();
    var current = $("#amen04").css("display");
    setArrowDirection(current, "amen04Arrow")
});


function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

//sets up the back button history with an ajax load

// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
    // alert("pageload: " + hash);
    // hash doesn't contain the first # character.
    if (hash) {
        // restore ajax loaded state
        if ($.browser.msie) {
            // jquery's $.load() function does't work when hash include special characters like aao.
            hash = encodeURIComponent(hash);
        }
        $("#ArticleContainer").load(hash + ".html");

    } else {
        // start page
        //window.location.reload();
        //$("#ArticleContainer").empty();
    }
}





function setWomensNav() {
    //alert ('turn on womens nav');
    //$('#mask').hide();  
    //$('.window').hide();
    //set cookie for province
    //alert (prov);
    createCookie('ForWomen', 1, 365);
    //location.reload();

}
/* womens nav visible or not */
var womenClicked = readCookie("ForWomen");
if (womenClicked == null) {
    var ourLocation = (document.URL);
    var isHomePage = ourLocation.indexOf('Default');
    $("#bodyCopy").css("margin-top", "31px");
    $("#module1").css("margin-top", "31px");
    $("#thirdNavBacker").css("margin-top", "31px");
    if (isHomePage == -1) {
        //$('#FWNav').hide();
    }

} else {
    $('#FWNav').show();
    $("#bodyCopy").css("margin-top", "31px");
    $("#module1").css("margin-top", "31px");
    $("#thirdNavBacker").css("margin-top", "31px");





}