/* config */

var GT; if (!GT) GT = {};
if (!GT.Utils) GT.Utils = {};

var debug = true;

var shaded_button_class = 'gtbutton';
var shaded_button_default_bgcolor = '#e47100';
var shaded_button_default_bgcolor_over = '#ab995b';

var initial_page_size;

/* general functions */

var addEvent = function(o, ename, fn) {
    if (typeof(fn) == 'function') {
        if (window.addEventListener)
            window.addEventListener(ename, fn, false);
        else if (window.attachEvent)
            window.attachEvent('on' + ename, fn, false);
        else if (document.getElementById)
            eval('window.on' + ename) = fn;
        else
            fn();
    };
};

var addOnLoad = function(fn) {
    addEvent(window, 'load', fn);
};

var addOnUnLoad = function(fn) {
    addEvent(window, 'unload', fn);
};

GT.Utils.AddEvent = addEvent;

var QueryString = function(qs) {
    var r = '', s = window.location.search.substring(1), v = s.split('&');
    for (var i = 0; i < v.length; i++) {
        var nv = v[i].split('=');
        if (nv && nv[0].toLowerCase() == qs.toLowerCase()) { r = nv[1]; delete nv; break; }
        delete nv;
    };
    delete v;
    return r;
};

var getCookie = GetCookie = function(name) {
    var r = null;
    if (document.cookie.length > 0) {
        var offset = document.cookie.indexOf(name + '=');
        if (offset != -1) {
            offset += name.length + 1;
            var end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            r = unescape(document.cookie.substring(offset, end));
        };
    };
    return r;
};

var setCookie = SetCookie = function(name, value, lifetime_minutes, path) {
    if (typeof name != 'undefined') {
        var cookie = name + '=' + escape(value);
        if (lifetime_minutes != null) {
            var today = new Date();
            var expires = new Date(today.getTime() + lifetime_minutes * 1000 * 60);
            cookie +=  '; expires=' + expires.toGMTString();
        };
        if (path != null) {
            cookie += '; path=' + path;
        };
        document.cookie = cookie;
    };
};

var jsEscape = function(text) {
    if (text && typeof text == 'string')
        return text.replace(/\'/g, '\\\'');
    else if (text && text.toString)
        return text.toString().replace(/\'/g, '\\\'');
    else if (text)
        return (typeof text).toString().replace(/\'/g, '\\\'');
    else
        return 'undefined';
};

var getURL = getUrl = function(url, target) {
    if (url != '') {
        try {
            var link = document.createElement('a');
            if (link.setAttribute) {
                link.setAttribute('href', url);
                link.setAttribute('target', (target || '_top'));
            } else {
                link.href = url;
                link.target = (target ? target : '_top');
            };
            document.body.appendChild(link);
            link.click();
		} catch(e) {
			document.location.href = url;
		};
    };
};

var selectLanguage = SelectLanguage = function(o) {
    getURL(o.options[o.selectedIndex].value);
};

// getPageSize()
// Returns array with page width, height and window width, height
var getPageSize = GetPageSize = function () {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body && document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.body) { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};

var getScrollOffset = GetScrollOffset = function() {
    var x,y;
    if (self.pageYOffset) // all except Explorer
    {
	    x = self.pageXOffset;
	    y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
	    // Explorer 6 Strict
    {
	    x = document.documentElement.scrollLeft;
	    y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
	    x = document.body.scrollLeft;
	    y = document.body.scrollTop;
    }
    return new Array(x, y);
};

var getKeyCode = GetKeyCode = function(e) {
    var keycode;
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	return keycode;
};

var getKey = GetKey = function(e) {
    return String.fromCharCode(getKeyCode(e));
};

var showElement = ShowElement = function(id) {
    var element = document.getElementById(id);
    if (element) {
        if (!element.style) element.style = new Object();
        element.style.display = 'block';
    };
};

var hideElement = HideElement = function(id) {
    var element = document.getElementById(id);
    if (element) {
        if (!element.style) element.style = new Object();
        element.style.display = 'none';
    };
};

var toggleElement = ToggleElement = function(id) {
    var element = document.getElementById(id);
    if (element) {
        if (!element.style) element.style = new Object();
        if (element.style.display == 'none')
            element.style.display = 'block';
        else
            element.style.display = 'none';
    };
};

if (!Array.prototype.push) {
	Array.prototype.push = function() {
		for(var i = 0; i < arguments.length; i++){
			this[this.length] = arguments[i];
		};
       	return this.length;
    };
};

if (!String.prototype.trim) {
	String.prototype.trim = function () {
	    return this.replace(/^\s*/, "").replace(/\s*$/, "");
	};
};

/* ajax callback functions */

var xml_to_string = function(xml) {
    return (new XMLSerializer()).serializeToString(xml);
};

var xml_node_value = function(node) {
    if (typeof(node.textContent) != 'undefined') {
        return node.textContent;
    } else if (typeof(node.innerText) != 'undefined') {
        return node.innerText;
    } else if (typeof(node.text) != 'undefined') {
        return node.text;
    } else {
        switch (node.nodeType) {
            case 3:
            case 4:
                return node.nodeValue;
                break;
            case 1:
            case 11:
                var innerText = '';
                for (var i = 0; i < node.childNodes.length; i++) {
                    innerText += xml_node_value(node.childNodes[i]);
                };
                return innerText;
                break;
            default:
                return '';
        };
    };
};

var init_window = function() {
    // get page size
    initial_page_size = getPageSize();
};

/* images */

var a_preload_images = new Array();
var add_preload_image = function(url) {
    a_preload_images[a_preload_images.length] = url;
};
var preload_images = function() {
    for (var i = 0; i < a_preload_images.length; i++) {
        eval('var i_' + i + ' = new Image();');
        eval('i_' + i + '.src = \'' + a_preload_images[i] + '\';');
    };
};

init_grayscale_images = function() {
    var a_elements = document.getElementsByTagName('IMG');
    for (var i = 0; i < a_elements.length; i++) {
        if (a_elements[i].className && a_elements[i].className.indexOf('grayscale') > -1) {
            //var href = a_elements[i];
            a_elements[i].onmouseover = function(e) { this.src = this.src.replace(/_gray\.gif/, '.gif'); };
            a_elements[i].onmouseout = function(e) { this.src = this.src.replace(/\.gif/, '_gray.gif'); };
            a_elements[i].onclick = function(e) { this.blur(); return true; };
        };
    };
};

/* shaded buttons */

var init_shaded_buttons = function() {
    var input_elements = document.getElementsByTagName('INPUT');
    for (var i = 0; i < input_elements.length; i++) {
    	var btn = input_elements[i];
        if (shaded_button_class != '' && btn.className && btn.className == shaded_button_class) {
		    btn.onmouseover = function() { try { this.style.background = (eval('btn_bgcolor_over_' + this.id) || shaded_button_default_bgcolor); } catch(ex) {} };
		    btn.onmouseout = function() { try { this.style.background = (eval('btn_bgcolor_' + this.id) || shaded_button_default_bgcolor_over); } catch(ex) {} };
		    btn.onfocus = btn.onmouseover;
		    btn.onblur = btn.onmouseout;
        };
    };
};

/* firefox error console */
var console; if (!console) console = {};
if (BrowserDetect && BrowserDetect.browser == 'Firefox') {
    console.do_log = function(text) {
        throw text;
    };
    console.log = function(text) {
        if (debug) window.setTimeout("console.do_log('" + jsEscape(text).replace(/\n/g, '') + "')", 1);
    };
} else {
    console.log = function(text) {
        if (debug) window.status = text;
    };
}

/* script and css requirements */
GT.scripts = new Array();
GT.RequireJS = function(js) {
    var f = false;
    for (var i = 0; i < GT.scripts.length; i++) {
        if (GT.scripts[i] == js) { f = true; break; }
    }
    if (!f) {
        GT.scripts[GT.scripts.length] = js;
        document.write('<scr' + 'ipt type="text/javascript" src="' + js + '">\n</scr' + 'ipt>');
    }
}
GT.styles = new Array();
GT.RequireCSS = function(css, media) {
    var f = false;
    for (var i = 0; i < GT.styles.length; i++) {
        if (GT.styles[i] == css) { f = true; break; }
    }
    if (!f) {
        GT.styles[GT.styles.length] = css;
        document.write('<link rel="stylesheet" href="' + css + '" type="text/css" media="' + media + '" />');
    }
}

/* this will stop image flickering in IE6 when elements with images are moved */
try {
    if (document.execCommand)
	    document.execCommand("BackgroundImageCache", false, true);
} catch(e) {};

/* add functions to onLoad */

addOnLoad(init_window);
addOnLoad(init_grayscale_images);
addOnLoad(init_shaded_buttons);
addOnLoad(preload_images);