﻿var loadStart = (new Date()).getTime();
var loadEnd = 0;
var gridQString = null;

/*
*********************************************
Account functions region
*********************************************
*/
function doLogin(cookie){
    if(cookie){
        createCookie('uName',$('txtUsername').value,60);
        createCookie('loggedIn',true);
    }
    $('linkLogin').set('href','#Logout');
    $('linkLogin').set('html','logout');
    window.location = '#Home';
    if(cookie){
        window.location.reload();
    }
};

function doLogout(e){
    eraseCookie('uName');
    eraseCookie('loggedIn');
    $('linkLogin').set('href','#Login');
    $('linkLogin').set('html','login');
    alert('You are now logged out');
    window.location = '#Home';
    window.location.reload();
};
/*
*********************************************
URL functions region
*********************************************
*/

function fnHashName(url){
    if($chk(url)){
        return $chk(url.split('#')[1]) ? url.split('#')[1] : '';
    } else {
        return $chk(returnDocument().split('#')[1]) ? returnDocument().split('#')[1] : '';
    }
};

function returnDocument() {
    var file_name = document.location.href;
    var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
    return file_name.substring(file_name.lastIndexOf("/") + 1, end);
};

function returnDomain() {
    var file_name = document.location.href;
    var end = (file_name.indexOf("/", 7) == -1) ? file_name.length : file_name.indexOf("/", 7);
    return file_name.substring(0, end);
};

function queryString(strURL) {

    var queryHash = new Hash('');
    var theUrl = window.location.toString();

    if (strURL != '') theUrl = strURL;

    if ($chk(theUrl.split('?')[1])) {
            queryString = theUrl.split('?')[1].split('&');
    } else {
        return queryHash;
    }

    queryHash.empty(); //Make sure it is clean
    queryString.each(function(key) {
        //        alert('key="' + key.split('=')[0] + '" value="' + key.split('=')[1] + '"');
        queryHash.include(key.split('=')[0], key.split('=')[1]);
    })

    return queryHash;

};

function fnRequestQueryString(key){
    var queryString = null;
    if ($chk(window.location.toString().split('?')[1])) {
        queryString = window.location.toString().split('?')[1];
    }
    if(!$chk(queryString))return false;
    queryString.split('&').each(function(item){
        if($chk(item.split('=')[0])){
            if(item.split('=')[0].toLowerCase()==key.toLowerCase()){
                queryString = item.split('=')[1];
            }
        }
    });
    return queryString;
};


function fnForumPath(){
    var path = fnHashName(window.location.toString());
    return '/Legends/Forum/' + path;
};

function doActiveTopicsClick(el){
    var URL = fnHashName(el.get('href'));
    fnOpenLink(URL);
};

function fnOpenLink(URL) {
//    this.event.stop();
    //Check forum frame exists if not then set the page location instead
    if (document.getElementById('ifForum')) {
        //alert('/Legends/Forum/' + URL);
        var el = $('ifForum');
        el.setStyle('height', 10000);
        el.src = '/Legends/Forum/' + URL;
    } else if (window.opener) {
        //window.location = '/Legends/Forum.aspx?url=' + URL;
        openRemoteLink('/Legends/Forum.aspx#' + URL);
    } else {
    window.location = '/Legends/Forum.aspx#' + URL;
    }
    
    return false;
};

function openRemoteLink(url) {
    if (window.opener && !window.opener.closed) {
        window.opener.location = url;
        window.opener.focus();
        //window.close();
    } else {
        window.open('http://www.legendsguild.co.uk' + url, '', '');
        //window.close();
    }
};

function fnPopoutTavern() {
    var myWindow = window.open('/Legends/Popout-Tavern.aspx', 'popoutTavern', 'width=200px,height=500px');
};

/*
*********************************************
Positional functions region
*********************************************
*/

function fnScrollTop() {
    var scrollTop = 0;

    if (document.documentElement && document.documentElement.scrollTop) {
        // IE6 +4.01 and user has scrolled
        scrollTop = document.documentElement.scrollTop;
    }
    else if (document.body && document.body.scrollTop) {
        scrollTop = document.body.scrollTop;
    }

    return scrollTop;
};

function fnScrollBottom() {
    var scrollBottom = 0;

    if (document.documentElement && document.documentElement.scrollBottom) {
        // IE6 +4.01 and user has scrolled
        scrollBottom = document.documentElement.scrollBottom;
    }
    else if (document.body && document.body.scrollBottom){
    scrollBottom = document.body.scrollBottom;
    }

    return scrollBottom;
};

function fnScrollHeight() {
    var scrollHeight = 0;

    if (document.documentElement) {
        // IE6 +4.01 and user has scrolled
        scrollHeight = document.documentElement.scrollHeight;
    }
    else if (document.body){
    scrollHeight = document.body.scrollHeight;
    }

    return scrollHeight;
};

function fnScrollWidth() {
    var scrollWidth = 0;

    if (document.documentElement) {
        // IE6 +4.01 and user has scrolled
        scrollWidth = document.documentElement.scrollWidth;
    }
    else if (document.body) {
    scrollWidth = document.body.scrollWidth;
    }

    return scrollWidth;
};

function getScrollWidth() {
    var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;

    return w ? w : 0;
};

function getScrollHeight() {
    var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;

    return h ? h : 0;
};


function fnScrollToTop() {
    scroll(0,0);
};

function fnScrollToBottom() {
    scroll(0,window.getSize().y);
};

function fnMinimiseMenus(flag){
    var menuCookie = readCookie("menuCookie");
    if(!menuCookie){
        createCookie("menuCookie",false,60);
        menuCookie = false;
    }
    
    if(flag == true){
        if(menuCookie==false||menuCookie=="false"){
            menuCookie = true;
        } else {
            menuCookie = false;   
        }
    }
    
    createCookie("menuCookie",menuCookie,60);
    
    thePage.setPositions();    
};

/*
*********************************************
Cookie functions region
*********************************************
*/

function createCookie(name, value, days) {

    if (name != 'null' && $chk(name)){
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
        //alert(name + "=" + value + expires + "; path=/");
        
        if(name != 'siteCookies') fnSiteCookies(name);
    }
};

function readCookie(name) {
    var nameEQ = name + "=";
    
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
};

function eraseCookie(name) {
    createCookie(name, "", -1);
};

function deleteAllCookies() {
    var strCookies = readCookie('siteCookies');

    if (strCookies) {
        var arrCookies = strCookies.split(',');

        if (confirm("Are you sure you want to reset all cookies?")) {

            for (var i = 0; i < arrCookies.length; i++) {
                eraseCookie(arrCookies[i]);
            }
            
            eraseCookie('siteCookies');

            alert('Cookies deleted');
            window.location.reload();
        }
    }
};

function fnSiteCookies(name){
    var strCookies = readCookie('siteCookies');
    var blFound = false;
    
    if (strCookies) {
        var arrCookies = strCookies.split(',');

        for (var i = 0; i < arrCookies.length; i++) {
            if(arrCookies[i] == name) blFound = true;
        }
    }
    
    if (!blFound){
        strCookies = (!$chk(strCookies) || strCookies == 'null') ? name: strCookies + ',' + name;
        createCookie('siteCookies',strCookies,60);
    }
};

Element.implement({
	hasEvent: function(eventType,fn) {
		//get the element's events
		var myEvents = this.retrieve('events');
		//can we shoot this down?
		return myEvents && myEvents[eventType] && (fn == undefined || myEvents[eventType].keys.contains(fn));
	}
});
