/*
	Site Javascript
*/

/*
	Ajax Related
*/
var site_ajax = {
	//Shot/hide loading
	show_loading: function(){
		Effect.Appear('loading', { duration: 0.25, queue: { scope: "loading", position: "end" } });
	},
	
	hide_loading: function(){
		Effect.Fade('loading', { duration: 0.25, queue: { scope: "loading", position: "end" } });
	},
	
	ajax_url: function(module){
		return site_url + 'ajax.php?act=' + escape(module);;
	},
	
	show_overlay: function(){
		Effect.Appear('overlay', { duration: 0.25, to: 0.5 , queue: {scope: 'overlay', position: 'end' }});
		$('body_tag').style.overflow = 'hidden';
	},
	
	hide_overlay: function(){
		Effect.Fade('overlay', { duration: 0.25, from: 0.5 , queue: {scope: 'overlay', position: 'end' }});
		$('body_tag').style.overflow = 'auto';
	}
}

/*
	AJAX Handlers
*/
var site_ajaxHandler = {
	onCreate: function(){
		site_ajax.show_loading();
	},

	onComplete: function(obj) {
		if(Ajax.activeRequestCount == 0){
			site_ajax.hide_loading();
		}

	},
	onFailure: function(obj){
		if(Ajax.activeRequestCount == 0){
			site_ajax.hide_loading();
		}
		site_error.do_error("Error, one or more requests to the server has failed. Please try again later or reload the page.");
	},
	onException: function(obj,e){
		if(Ajax.activeRequestCount == 0){
			site_ajax.hide_loading();
		}
		try {console.log(e) } catch(e) { }
		site_error.do_error("Error, one or more requests to the server has failed. Please try again later or reload the page.");
	}
};
Ajax.Responders.register(site_ajaxHandler);

/*
	Error Handler
*/

/*
	Error Object
*/
var site_error = {
	error_queue: new Array,
	showing_error: false,
	fatal_queue: new Array,
	
	do_error: function(msg, fatal_exception){
		//If fatal exception,  add to fatal exception queue
		if (fatal_exception == true){
			this.fatal_queue.push(msg);
			if (this.showing_error == false){
				this.next_error();
			}
		}
		else{
			this.error_queue.push(msg);
			if (this.showing_error == false){
				this.next_error();
			}
		}
	},
	
	next_error: function(){
		if (this.showing_error == false && ( this.fatal_queue.length != 0 || this.error_queue.length != 0 ) ){
			this.showing_error = true;
			
			if (this.fatal_queue.length != 0){
				var msg = this.fatal_queue.shift();
				$('dialog_box_text').update('<img src="' + site_url +'images/widgets/exclamation.png" alt="Error" /> ' + msg + '');
				Effect.Appear("dialog_box");
				$('top').focus();
				
				//Show fatal errors for 30 sec min
				setTimeout('site_error.next_error();', 300000 + msg.length * 100);
			}
			else{
				var msg = this.error_queue.shift();
				$('dialog_box_text').update('<img src="' + site_url +'images/widgets/information.png" alt="Message" /> ' + msg + '');
				Effect.Appear("dialog_box");
				$('top').focus();
				setTimeout('site_error.next_error();', 3000 + msg.length*10);
			}
		}
		else if (this.show_error == false){
			//No errors... just return
			return;
		}
		else{
			if (this.error_queue.length == 0 && this.fatal_queue.length == 0){
				this.hide_error();
			}
			else{
				if (this.fatal_queue.length != 0){
					var msg = this.fatal_queue.shift();
					var html = '<img src="' + site_url + 'images/widgets/exclamation.png" alt="Error" /> ' + msg;
					Effect.Fade('dialog_box_text', { duration: 0.25, queue: { scope: "site_error", position: "end" } } );
					setTimeout(' $("dialog_box_text").update( unescape("'+ escape(html) + '") );', 255);
					setTimeout('Effect.Appear("dialog_box_text", { duration: 0.25, queue: { scope: "site_error", position: "end" } } } );', 260);
					//Show fatal errors for 30 sec min
					setTimeout('site_error.next_error();', 300000 + msg.length * 100);
					$('top').focus();
				}
				else{
					var msg = this.error_queue.shift();
					var html = '<img src="' + site_url + 'images/widgets/information.png" alt="Message" /> ' + msg;
					Effect.Fade('dialog_box_text', { duration: 0.25 } );
					setTimeout(' $("dialog_box_text").update( unescape("'+ escape(html) + '") );', 255);
					setTimeout('Effect.Appear("dialog_box_text", { duration: 0.25, queue: { scope: "site_error", position: "end" } } );', 260);
					setTimeout('site_error.next_error();', 3260 + msg.length*10);
					$('top').focus();
				}
			}
		}
	},
	
	hide_error: function(){
		if (this.showing_error == true){
			Effect.Fade("dialog_box");
			setTimeout('site_error.check_error();', 1001);
		}
	},
	
	check_error: function(){
		this.showing_error = false;
		//Show fatal
		if (this.fatal_queue.length != 0 || this.error_queue.length != 0){
			this.next_error();
		}
	}
}


/*
	Login Form
*/

var login_form = {
	toggle_form: function(){
		if (Element.visible('login_relative')){
			this.hide_form();
		}
		else{
			this.show_form();
		}
	},
	
	show_form: function(){
		//$('login_form').show();
		Effect.Grow('login_relative', { direction: 'top-right', duration: 0.5, queue: { scope: 'login_form', position: 'end' } });
	},
	
	hide_form: function(){
		Effect.Shrink('login_relative', { direction: 'top-right', duration: 0.5, queue: { scope: 'login_form', position: 'end' } });
	}
}

/*
	Site Menu
*/

var site_menu = {
	toggle_submenu: function(item){
		if (Element.visible('menu_item_' + item + '_sub')){
			Effect.Fade('menu_item_' + item + '_sub', {duration: 0.5, queue: { position: 'end', scope: 'menu_item_' + item } });
		}
		else{
			Effect.Appear('menu_item_' + item + '_sub', {duration: 0.5, queue: { position: 'end', scope: 'menu_item_' + item } });
		}
	}
}

/*
	Tooltip
*/
var site_tooltip = {
	tooltip_shown: false,
	show_tooltip: function(text){
		$('tooltip').update(text);
		var offset_x = mouse_x + 0;
		var offset_y = mouse_y + 20;
		
		$('tooltip').style.left = offset_x + "px";
		$('tooltip').style.top = offset_y + "px";
		
		var queue = Effect.Queues.get('tooltip');
		queue.each(function(e) { e.cancel() });
		Effect.Appear('tooltip', { duration: 0.25, to: 0.85, queue: { scope: 'tooltip', position: 'end' } } );
		this.tooltip_shown = true;
	},
	
	hide_tooltip: function(){
		var queue = Effect.Queues.get('tooltip');
		queue.each(function(e) { e.cancel() });
		Effect.Fade('tooltip', { duration: 0.25, from: 0.85, queue: { scope: 'tooltip', position: 'end' } } );
		
		this.tooltip_shown = false;
	},
	harvest_tooltips: function(){
		var tooltips = document.getElementsByClassName('tooltip');
		for (var i = 0; i <= tooltips.length - 1; i++){
			tooltips[i].setAttribute('tooltip', tooltips[i].innerHTML);
			tooltips[i].update('&nbsp;&nbsp;&nbsp;&nbsp;');
			tooltips[i].onmouseover = function(){
				site_tooltip.show_tooltip(this.getAttribute('tooltip'));
			}
			tooltips[i].onmouseout = function(){
				site_tooltip.hide_tooltip();
			}
		}
		Event.observe(document, 'mousemove', 
						function(event){
							if (site_tooltip.tooltip_shown == true){
								var offset_x = mouse_x + 0;
								var offset_y = mouse_y + 20;
								
								$('tooltip').style.left = offset_x + "px";
								$('tooltip').style.top = offset_y + "px";
							}
					});
	}
}

var mouse_x, mouse_y;
function site_init(){
	site_ajax.hide_loading();
	
	//Check Mouse Location
	Event.observe(document, 'mousemove', 
					function(event){
						mouse_x = Event.pointerX(event);
						mouse_y = Event.pointerY(event);
				});
	
	site_tooltip.harvest_tooltips();
}