// JavaScript Document
var widget;
var widget_content;
var widget_bar;
var widget_initiated = false;
var load_error = false;
var widget_contents = new Array(0, 0);
var widget_style = '/styles/widget/default/widget.css';
var include_dir = '/scripts/widget/';
var guitools = null;

try {
	if (globals.widget && globals.widget.theme && globals.widget.style_dir) widget_style = globals.widget.style_dir + globals.widget.theme + '/widget.css';
	document.write('<link rel="stylesheet" href="' + widget_style + '" type="text/css">');
	if (globals.widget && globals.widget.include_dir) include_dir = globals.widget.include_dir;
	document.write('<script type="text/javascript" src="' + globals.widget.include_dir + 'drag.js"> </script>');
} catch(e) {
	load_error = true;
}

function load_widget(view_obj) {
	if (!lang_arr) return false;
	if (!widget_initiated) {
		try {
			init_widget();
		} catch(e) {
			return false;
		}
	}
	if (!get_widget_contents(view_obj)) {
		//hide_widget();
		return false;
	}
	if (!show_widget()) return false;
	
	return true;
}

function set_widget_contents() {
	if (!(widget_contents.length == 2) || !widget_content) return false;
	switch (widget_contents[0]) {
		case 'XML':
			if (!(widget_contents[1].length > 0)) return false;
			try {
				widget_content.innerHTML = widget_contents[1];
			} catch(e) {
				return false;
			}
			break;
		case 'DOM':
			if (widget_contents[1].length < 1) return false;
			try {
				for (var count = 0; count < widget_contents[1].length; count++) {
					widget_content.appendChild(widget_contents[1][count]);
				}
			} catch(e) {
				return false;
			}
			break;
	}
	return true;
}

function show_widget() {
	if (!lang_arr || load_error) return false;
	
	if (!widget || (typeof(widget) != 'object')) {
		return false;
	}
	//position the widget + scrolly
	try {
		if (!globals.browser) globals.browser = new Object();
		globals.browser.xy = guitools.window_size();
		globals.browser.scroll_xy = guitools.scroll_xy();
		if ((typeof(globals.browser.xy) == 'object') && (typeof(globals.browser.scroll_xy) == 'object') && (globals.browser.xy.length == 2) && (globals.browser.scroll_xy.length == 2)) {
			globals.browser.x = globals.browser.xy[0];
			globals.browser.y = globals.browser.xy[1];
			globals.browser.scroll_x = globals.browser.scroll_xy[0];
			globals.browser.scroll_y = globals.browser.scroll_xy[1];
		}
		globals.site.xy = guitools.body_size();
		if ((typeof(globals.site.xy) == 'object') && (globals.site.xy.length == 2)) {
			globals.site.x = globals.site.xy[0];
			globals.site.y = globals.site.xy[1];
		}
		if (globals.browser.x <= globals.widget.width) globals.widget.position_locked = true;
		if (((globals.widget.offset.x == 'center') || globals.widget.position_locked) && (globals.site.x > 0)) {
			if (globals.site.width > globals.browser.x) { // if scrollbar (window smaller than site)
				if (globals.widget.scroll_buffer != globals.browser.scroll_xy) {
					globals.widget.offset.x = Math.round(globals.browser.scroll_x + (globals.browser.x - globals.widget.width)/2);
					if (globals.widget.offset.x < 0) globals.widget.offset.x = 0;
					widget.style.left = globals.widget.offset.x + 'px';
					globals.widget.offset.x = 'center';
				}
			}
			else widget.style.left = Math.round((globals.site.x - globals.widget.width)/2) + 'px';
		} else {
			if (globals.widget.scroll_buffer != globals.browser.scroll_xy) {
				globals.widget.offset.x += globals.browser.scroll_x - globals.widget.scroll_buffer[0];
			}
			if (globals.widget.offset.x < 0) globals.widget.offset.x = 0;
			widget.style.left = globals.widget.offset.x + 'px';
		}
		if (globals.widget.scroll_buffer != globals.browser.scroll_xy) {
			if (globals.widget.scroll_buffer && (typeof(globals.widget.scroll_buffer) == 'object') && (globals.widget.scroll_buffer.length == 2)) globals.widget.offset.y += globals.browser.scroll_y - globals.widget.scroll_buffer[1];
			else { //init
				globals.widget.offset.y += globals.browser.scroll_y;
			}
			if (widget.offsetHeight && ((globals.browser.y + globals.browser.scroll_y) < (globals.widget.offset.y + widget.offsetHeight))) {
				globals.widget.offset.y += (globals.browser.y + globals.browser.scroll_y) - (globals.widget.offset.y + widget.offsetHeight);
			}
			if (globals.widget.offset.y < 0) globals.widget.offset.y = 0;
		}
		widget.style.top = globals.widget.offset.y + 'px';
		globals.widget.scroll_buffer = globals.browser.scroll_xy;
	} catch(e) { }
	widget.style.visibility = 'visible';
	/*if (window.location) {
		var loco = '' + window.location;
		try {
			if (loco.indexOf('#widget_bar_link') >= 0) return true;
			else if (loco.indexOf('#') >= 0) {
				var close_link = document.getElementById('widget_bar_link');
				if ((typeof(close_link) == 'object') && (close_link['href'])) {
					close_link.href = loco.substring(loco.indexOf('#'));
				}
				loco = loco.substring(0, (loco.indexOf('#')));
			}
			window.location = loco + '#widget_bar_link';
		} catch(e) { }
	}*/
	
	return true;
}

function hide_widget() {
	if (!lang_arr || load_error || !widget_initiated || !widget) return false;
	widget.style.visibility = 'hidden';
	return true;
}

function init_widget() { // append the widget tags
	widget = document.getElementById('widget');
	if (!widget) {
		try {
			widget = new Object();
			widget.wrap = document.createElement('div');
			widget.bar = document.createElement('div');
			widget.content = document.createElement('div');
			widget.wrap.setAttribute('id', 'widget');
			widget.bar.setAttribute('id', 'widget_bar');
			widget.content.setAttribute('id', 'widget_content');
			widget.wrap.appendChild(widget.bar);
			widget.wrap.appendChild(widget.content);
			document.body.appendChild(widget.wrap);
			widget = document.getElementById('widget');
			if (typeof(widget) != 'object') return false;
			
		} catch(e) {
			try {
				document.write('<div id="widget" class="mh_size_100"><div id="widget_bar"></div><div id="widget_content"></div></div>');
			} catch(e) { return false; }
		}
	}
	try {
		widget = document.getElementById('widget');
		widget_content = document.getElementById('widget_content');
		widget_bar = document.getElementById('widget_bar');
		if (!write_widget_bar()) return false;
		guitools = new window_methods();
		if (globals && (typeof(globals) == 'object') && (typeof(globals.site) == 'object') && !isNaN(globals.site.width)) {
			var widget_bar_title = document.getElementById('widget_bar_title');
			if (widget_bar_title && (typeof(widget_bar_title) == 'object')) {
				globals.widget.width = Math.round(0.75 * globals.site.width);
				widget.style.width = globals.widget.width + 'px';
				widget_bar_title.style.width = (globals.widget.width - 58) + 'px'; // button + border + padding = 58 = 40 + 2 + 16
				globals.widget.offset = new Object();
				globals.widget.offset.x = 'center';
				globals.widget.offset.y = 100;
				globals.widget.position_locked = false;
				globals.widget.scroll_buffer = null;
			}
		}
		widget_initiated = true; 
	} catch(e) { return false; }
	
	return true;
}

function write_widget_bar() {
	/*widget_bar setup: 
		<div id="widget_bar">
			-------------->
			<span id="widget_bar_title">_BAR-TITLE_</span>
			<a href="#widget_link" title="_LINK-TITLE_" name="widget_link" onclick="hide_widget();">
				<span>_LINK-TITLE_</span>
			</a>
			<--------------
		</div>
	*/
	if (!widget || !widget_bar || !lang_arr) return false;
	try { //DOM XML
		var close_link;
		var link_title;
		var bar_title;
		close_link = document.createElement('a');
		close_link.setAttribute('href', '#');
		close_link.setAttribute('name', 'widget_link');
		close_link.setAttribute('id', 'widget_bar_link');
		//close_link.setAttribute('onclick', 'hide_widget();');
		close_link.onclick = function() { try { hide_widget(); return false; } catch(e) { } }; //IE6 bugfix
		close_link.setAttribute('title', lang_arr[1]);
		link_title = document.createElement('span');
		bar_title = document.createElement('span');
		bar_title.setAttribute('id', 'widget_bar_title');
		if (!bar_title || !link_title) return false;
		link_title.appendChild(document.createTextNode(lang_arr[1]));
		bar_title.appendChild(document.createTextNode(lang_arr[0]));
		if (!close_link || !link_title || !bar_title) return false;
		close_link.appendChild(link_title);
		widget_bar.appendChild(bar_title);
		widget_bar.appendChild(close_link);
	} catch (e) { // (IE), Opera < 9
		try {
			widget_bar.innerHTML = '<span id="widget_bar_title">'+lang_arr[0]+'</span><a href="#" title="'+lang_arr[1]+'" name="widget_link" onclick="hide_widget();"><span>'+lang_arr[1]+'</span></a>';
		} catch (e) {
			return false;
		}
	}
	bar_title.onmousedown = function () { init_drag(widget); };
	return true;
}

var window_methods = function() {
	this.window_size = function() {
		try {
			var window_width = 0, window_height = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				window_width = window.innerWidth;
				window_height = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				window_width = document.documentElement.clientWidth;
				window_height = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				window_width = document.body.clientWidth;
				window_height = document.body.clientHeight;
			}
			if ((window_width == 0) || isNaN(window_width)) return false;
			return new Array(window_width, window_height);
		} catch(e) {
			return false;
		}
	}
	
	this.body_size = function() {
		try {
			var body_width = 0, body_height = 0;
			var obj = document.getElementsByTagName('body');
			if (typeof(obj) != 'object') var obj = document.body;
			else {
				var count;
				for (count in obj) {
					if ((typeof(obj[count]) == 'object') && obj[count].offsetWidth && obj[count].offsetHeight) obj = obj[count];
					break;
				}
			}
			
			if ((typeof(obj) == 'object') && obj.offsetWidth && obj.offsetHeight) {
				body_width = obj.offsetWidth;
				body_height = obj.offsetHeight;
				if ((body_width == 0) || isNaN(body_width)) return false;
				return new Array(body_width, body_height);
			}
			return false;
		} catch(e) { return false; }
	}
	
	this.scroll_xy = function() {
		try {
			var scroll_x = 0, scroll_y = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				scroll_y = window.pageYOffset;
				scroll_x = window.pageXOffset;
			} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
				scroll_y = document.body.scrollTop;
				scroll_x = document.body.scrollLeft;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE6 standards compliant mode
				scroll_y = document.documentElement.scrollTop;
				scroll_x = document.documentElement.scrollLeft;
			}
			if (isNaN(scroll_x)) return false;
			return [ scroll_x, scroll_y ];
		} catch(e) { return false; }
	}
}


