﻿$(document).ready(function () {
    // Global Nav Drop down menu toggles
    $('#mainNav li').each(function () {
        //$(this).children('.btn, .btnOver, .second_nav, .second_nav_first').hover(function () {
        //  $(this).parent().children('.btn, .btnOver, .second_nav, .second_nav_first').toggle();
        //});
        $(this).hoverIntent({
            over: function () {
                $(this).children('.btn, .btnOver, .second_nav, .second_nav_first').toggle();
            },
            timeout: 200,
            out: function () {
                $(this).children('.btn, .btnOver, .second_nav, .second_nav_first').toggle();
            }
        });
    });

    //Clear the search field on focus only if the default text is displayed
    $('.globalSearch').focus(function () {
        if ($(this).val() == SearchInitialValue) {
            $(this).val('')
        }
        //Change colour to white on focus
        $('.globalSearch').css("color", "white");
    });

    //hooking up 'enter' key to search box
    $("#txtSearchBox").keyup(function (event) {
        if (event.keyCode == 13) {
            goSearch();
        }
    });

    //Pop ups
    //sign in
    $('.sign-in').live("click", function (e) { //.live not .click so the function will also work on the newly loaded content
        e.preventDefault(); //prevent the default action of clicking the link
        var href = $(this).attr("href"); //grab the links href
        signInpopup(href);
    });
    //forgot password
    $('.password').live("click", function (e) { //.live not .click so the function will also work on the newly loaded content
        e.preventDefault(); //prevent the default action of clicking the link
        clearpopups();
        var href = $(this).attr("href"); //grab the links href
        $('#pop-up #pop-main').load(href); //load the contents of the href info the popup 
        $('#pop-up').show();
    });
    //contact
    $('.contact').live("click", function (e) { //.live not .click so the function will also work on the newly loaded content
        e.preventDefault(); //prevent the default action of clicking the link
        clearpopups();
        var href = $(this).attr("href"); //grab the links href
        $('#Iframe_Contact').attr('src', href);
        $('#pop-up-contact').show();
        window.scroll(0, 0);
    });
    //register
    $('.register').live("click", function (e) { //.live not .click so the function will also work on the newly loaded content
        e.preventDefault(); //prevent the default action of clicking the link
        clearpopups();
        var href = $(this).attr("href"); //grab the links href
        $('#register-frame').attr('src', href); //load the contents of the href info the iframe 
        $('#pop-up-reg').show();
    });
    $('.close-reg').click(function () {
        clearpopups();
        /*
        $.get('/WSGeneral', function (data) {
            var myKeyAndValues = GetWSGeneralResults(data);
            if (myKeyAndValues.length < 1) return;
            for (i = 0; i < myKeyAndValues.length; i++) {
                var a = myKeyAndValues[i];
                //alert(a.Key + "   " + a.Value);
                if (a.Key == "Eamil") ChangeUserEmail(a.Value);
                
            }
        });
        */

    });
    $('.cancel, .close').live("click", function () {
        clearpopups();
    });
    $('.cancel, .close-contact').live("click", function () {
        clearpopups();
    });


});

function GetWSGeneralResults(strValues) {
    var myKeyAndValues = new Array();
    strValues = strValues.replace(/^\s+|\s+$/g, "");
    var Results = strValues.split('\n');
    for (i = 0; i < Results.length; i++) {
        strKeyVlaues = Results[i].split('=');
        var strValue = "";
        if (strKeyVlaues.length > 1) strValue = strKeyVlaues[1];
        myKeyAndValues[i] = new KeyAndValue(strKeyVlaues[0], strValue);
    }
    return myKeyAndValues;
}
function KeyAndValue(Key, Value) {
    this.Key = Key;
    this.Value = Value;
}

function clearpopups() {
    $('#register-frame').attr('src', "/empty");
    $('#Iframe_Login').attr('src', "/empty");
    $('#Iframe_Contact').attr('src', "/empty");

    $('#pop-up').hide();
    $('#pop-up-reg').hide();
    $('#pop-up , #pop-up-contact').hide();
}
function signInpopup(strURL) {
    clearpopups();
    var href = strURL;
    $('#Iframe_Login').attr('src', href);
    $('#pop-up').show();
}
function thankyoupopup(strURL) {
    clearpopups();
    var href = strURL; //grab the links href
    $('#Iframe_Login').attr('src', href);
    $('#pop-up').show();
}
function profilepopups(strURL) {
    clearpopups();
    var href = strURL;
    $('#register-frame').attr('src', href);
    $('#pop-up-reg').show();
}

//utility functions
function htmlEncode(value) {
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

function GoogleTrackEvent(category, action, opt_label, opt_value) {
    if (opt_label == null) {
        _gaq.push(['_trackEvent', category, action]);
    }
    else if (opt_value == null) {
        _gaq.push(['_trackEvent', category, action, opt_label]);
    }
    else {
        _gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
    }
    return true;
}

