/***************************************************************************\
|* © A Virtual Assistant in Paradise ***************************************|
|* This code was written to run on www.vainparadise.com *ONLY* *************|
|* developer.yahoo.com/yui/            *************************************|
\***************************************************************************/
 
/***************************************************************************\
|* General functions	*******************************************************|
\***************************************************************************/

/*********************************************************************
	 * Get an object, this function is cross browser
	 * *** Please do not remove this header. ***
	 * This code is working on my IE7, IE6, FireFox, Opera and Safari
	 * 
	 * Usage: 
	 * var object = get_object(element_id);
	 *
	 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
	**/
	function get_object(id) {
		var object = null;
		if( document.layers )	{			
			object = document.layers[id];
		} else if( document.all ) {
			object = document.all[id];
		} else if( document.getElementById ) {
			object = document.getElementById(id);
		}
		return object;
	}
	/*********************************************************************/
	 
	function replace_html(id, new_content) {
		$('#' + id).html(new_content);
	}
	
	function clear_html(id) {
		$('#' + id).html('<span></span>');
	}

	function rand(_min, _max) {
		return Math.floor(Math.random() * _max);
	}
	
	function findPosY(obj) {
		var curtop = 0;
		if(obj.offsetParent)
		  while(1)
		  {
			 curtop += obj.offsetTop;
			 if(!obj.offsetParent)
				break;
			 obj = obj.offsetParent;
		  }
		else if(obj.y)
		  curtop += obj.y;
		return curtop;
	}
	
	function scroll_window(x, y) {
		$('html,body').animate({scrollTop: y}, 300);
	}

/***************************************************************************\
|* Progress bar ************************************************************|
\***************************************************************************/
	
	function show_progress_bar(id) {
		$('#' + id).css('display', 'block');
	}
	
	function show_small_progress_bar(id) {
		$('#' + id).css('display', 'block');
	}
	
	function hide_progress_bar(id) {
		$('#' + id).css('display', 'none');
	}
	
	$(document).ready(function() {
		$(document).mousemove(function(e){
			$('#progress-bar').css('left', e.pageX - 2);
			$('#progress-bar').css('top',  e.pageY + 25);
		});
	});
	
/***************************************************************************\
|* Form handlers ***********************************************************|
\***************************************************************************/
	
	function get_page(the_page) {
		if( the_page == '' ) {
			return false;
		}
		show_progress_bar('progress-bar');
		$.ajax({
			 url: base_url + '?output=json&page=' + the_page + '&cache=' + new Date().getTime(),
			 type: 'get',
			 dataType: 'json',
			 timeout: connection_time_out,
			 error: function(){
				  handle_server_response_error();
			 },
			 success: function(data){
				  handle_server_response_success(data);
			 }
		});
		/**
		 * Track it!
		**/
		urchinTracker('/page/' + the_page);
		return false;
	}
	
	function send_form(form, action, the_page) {
		show_progress_bar('progress-bar');
		$(form).ajaxForm({
			 url: base_url + '?output=json&page=' + the_page + '&action=' + action + '&cache=' + new Date().getTime(),
			 type: 'post',
			 dataType: 'json',
			 timeout: connection_time_out,
			 error: function(){
				  handle_server_response_error();
			 },
			 success: function(data){
				  handle_server_response_success(data);
			 }
		});
		return false;
	}
	
	function showRequest(formData, jqForm, options) { 
		 // formData is an array; here we use $.param to convert it to a string to display it 
		 // but the form plugin does this for you automatically when it submits the data 
		 var queryString = $.param(formData); 
	 
		 // jqForm is a jQuery object encapsulating the form element.  To access the 
		 // DOM element for the form do this: 
		 // var formElement = jqForm[0]; 
	 
		 alert('About to submit: \n\n' + queryString); 
	 
		 // here we could return false to prevent the form from being submitted; 
		 // returning anything other than false will allow the form submit to continue 
		 return true; 
	} 

	function handle_server_response_error() {
		hide_progress_bar('progress-bar');
		clear_html('contents');
		replace_html('contents', error(timeout_message));
	}
	
	function handle_server_response_success(response) {
		hide_progress_bar('progress-bar');
		if( response.status == 'Error' ) {
			if( response.message ) {
				replace_html('contents', error(response.message));
			}
			if( response.form_fields_messages ) {
				for(var i = 0; i < response.form_fields_messages.length; i++) {
					if( response.form_fields_messages[ i ].type == 'error' ) {
						replace_html(response.form_fields_messages[ i ].name, response.form_fields_messages[ i ].error);
					} else {
						clear_html(response.form_fields_messages[ i ].name);
					}
				}
			}
		} else {
			replace_html('contents', response.page);
			document.title = response.title;
			$('#logo').attr('alt', response.title);
		}
		if( response.javascript_code ) {
			eval(	response.javascript_code );
		}
		scroll_window(0, findPosY(get_object('home-nav')) - 10);
		$('#contents').find('a.remote').click(function() {
				get_page($(this).attr('id'));
				return false;
		});
	}
	
/***************************************************************************\
|* Server response parser **************************************************|
\***************************************************************************/

	function parse_JSON(json) {
		return eval("(" + json + ")");
	}
	
/***************************************************************************\
|* Output formatting *******************************************************|
\***************************************************************************/
	
	function error(message) {
		return '<span class="red-text">' + message + '<span>';
	}
	
	function format_overlay_message(message) {
		return '<div style="text-align:justify;padding:5px;padding-right:20px;">' + message + '</div>';
	}
	
/***************************************************************************\
|* Bind remote link/form events ********************************************|
\***************************************************************************/
	
	jQuery.bind_remote_objects = function() {
		$('a.remote').each(function() {
			$(this).click(function() {
				try {
					YAHOO.util.History.navigate("page", $(this).attr('id'));   
				} catch (e) {
					get_page(page);
				}
				return false;
			});
		});
		current_page = YAHOO.util.History.getCurrentState("page");
		if ( location.hash.substr(1).length > 0 ) {
			get_page(current_page);
		}
		$('form.remote').each(function() {
			$(this).submit(function() {
				send_form($(this).attr('id'));
				return false;
			});
		});
	}

/***************************************************************************\
|* Browser history, back button, bookmarks etc. ****************************|
\***************************************************************************/
	
	$(document).ready(function() {
		var bookmarked_page = YAHOO.util.History.getBookmarkedState("page");
		var query_page 	  = YAHOO.util.History.getQueryStringParameter("page");
		var initial_page 	  = bookmarked_page || query_page || default_page;
		
		YAHOO.util.History.register("page", initial_page, function(page) {
			 get_page(page);
		});
		
		YAHOO.util.History.onReady(function () {
			$.bind_remote_objects();
		});
		
		try {
			YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
		} catch (e) {
			get_page(initial_page);
		}
	});
	
/***************************************************************************\
|* © A Virtual Assistant in Paradise ***************************************|
\***************************************************************************/