var current_page;
var current_page_add;
var section;
var current_id;
var current_page_last;
var login_redirect_page;
var change;
var notes_flip_sub_menu_current = 'edit';
var notes_flip_setting_current = false;
var lightboxOpenTrue = false;
var autosave_time = 1000*60*2; // 2 minutes
var notes_title_last_save;
var notes_text_last_save;
var notes_icon_last_save;
var refresh_hash;
var filter_icon = '';
var synotes_menu;
var prev_hid; 
var hid_title;
var hid_text;
var hid_icon;

var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
$(document).ready(function(){
		Set_Cookie( 'test', 'none', '', '/', '', '' );
		if ( Get_Cookie( 'test' ) )
		{
			Delete_Cookie('test', '/', '');
		}
		else
		{
			window.location = '/?i=nocookies';
		}
		
		
		$.historyInit(pageload, "");
		var current_hash = location.hash.replace(/\?.*$/, '');
		if(current_hash == '')
		{  
			if($('#start_page').html() == ''){ $.historyLoad('notes/');	} else { current_page = $('#start_page').html();  $.historyLoad($('#start_page').html()); } //$('#start_page').html()
		}
		
		
		setTimeout("thread_auto_menu_refresh();", autosave_time);
		
});

function thread_auto_menu_refresh()
{
	notes_bar_menu_refresh();
	setTimeout("thread_auto_menu_refresh();", autosave_time);
}
function notes_bar_menu_refresh()
{

url = "?i=load_notes_menu&filter_icon="+filter_icon;
$.getJSON(url, function(json) {

var home_left_bar_items = '';
	for( var i =0; i <json.length; i++) 
	{
			
			if(json[i]['order_style'] != undefined)
			{
				if(json[i]['order_style'] == 1 && filter_icon == '')
				{
					$('#bottom_order').attr('class','bottom_order');
				}
				else
				{
					$('#bottom_order').attr('class','bottom_order_active_inactive');
					
				}
			}
			else
			{
			var title = json[i]['title'];
			
			
				var uuid = json[i]['uuid'];
				var icon = json[i]['icon'];
				var selected = '';
				
				if(uuid == $('#id').val() )
				{
					var selected = 'home_left_bar_item_selected';
				}
				var home_left_bar_items = home_left_bar_items+'<div id="'+uuid+'-value" ><div class="home_left_grip"> </div><a class="home_left_bar_link" href="#notes/'+uuid+'/" id="menu-link-'+uuid+'" ><div class="home_left_bar_item '+selected+'" id="menu-'+uuid+'" title="'+uuid+'" ><span class="home_left_bar_icon home_left_bar_icon_'+icon+'"> </span><div class="home_left_bar_item_title">'+title+'</div></div></a></div>';
				
		
			}
	
	}
	
	
	$('#home_left_bar_items').html(home_left_bar_items);
	
	generate_notes_lists();
	
	if($('#bottom_order').attr('style') == 'background-position: -42px -40px;' && json[i]['order_style'] == 1)
	{
		$('.home_left_grip').show();
	}
	
});

}

function refresh_notes()
{
	$('#home_content_centre_loading').slideDown('fast');
	notes_bar_menu_refresh();
	var hash = location.hash.replace(/\?.*$/, '');
	var url = hash.split("/");
	$('#id').val("");
	notes_flip_menu(url[1], url[2],url[3]);
	$('#home_content_centre_loading').slideUp('fast');
}
function lightbox_edit_field(id)
{

			$('#lightbox_settings_textbox_'+id).attr("disabled", '');
			$('#lightbox_settings_textbox_'+id).attr("style", 'background:url(/resources/1.1.9/images/lightbox_textbox.png)');
			if(id == 'password')
			{
				$('#lightbox_settings_textbox_'+id).attr("value", "");
				//lightbox_settings_textbox_password_confirm, lightbox_settings_textbox_password_current
				$('#lightbox_settings_textbox_password_confirm').attr("style", 'background:url(/resources/1.1.9/images/lightbox_textbox.png)');
				$('#lightbox_settings_textbox_password_current').attr("style", 'background:url(/resources/1.1.9/images/lightbox_textbox.png)');
				$('#lightbox_form_password').fadeIn("slow"); 
			}
}
	
function settings_submit()
{

	var email = $('#lightbox_settings_textbox_username').val();
	
	var password = $('#lightbox_settings_textbox_password').val();
	var password_confirm = $('#lightbox_settings_textbox_password_confirm').val();
	var password_current = $('#lightbox_settings_textbox_password_current').val();
	
	var font_size = $('#lightbox_settings_select_font_size').val();
	var font_family = $('#lightbox_settings_select_font_family').val();
	var animate = $('#lightbox_settings_select_animate').val();
	var order = $('#lightbox_settings_select_order').val();

	
	if(email == '')
	{
		alert('You must enter an email.');
		return false;
	}
	
	if(password != '' && password_confirm != password)
	{
		alert('You passwords must match.');
		return false;
	}
	if(isValidEmail(email) == false)
	{
		alert('Your must enter a valid email.');
		return false;
	
	}
	$('#lightbox_loading').show();
	$.ajax({
	    url: '?i=settings_save',
	    type: 'POST',
	    dataType: 'html',
	    timeout: 5000,
	 	data: "email="+email+"&password="+password+"&password_current="+password_current+"&font_size="+font_size+"&font_family="+font_family+"&animate="+animate+"&order="+order,
	    error: function(){
	        alert('Error loading document.');  
    	},
	    success: function(html)
		{ 
			$('#lightbox_loading').hide();
			
		   if(html == 1)
		   {
		  // $('.note_input_textarea').attr('style',font_family);
		   $('.note_input_textarea').attr('style','font-size'+font_size);
		   	lightbox_close_first();
		   	refresh_notes();
		   }
		   else
		   {
		   	alert(html);
		   }
		}
		
		});

	
	//lightbox_settings_textbox_username, lightbox_settings_textbox_password, lightbox_settings_select_font_size, lightbox_settings_select_font_family
	//$('#lightbox_settings_select_font_size').val;
}

function lightboxOpen(uri)
{
	$.ajax({
	    url: uri,
	    type: 'GET',
	    dataType: 'html',
	    timeout: 5000,
		beforeSend: function(){ $('#lightbox').fadeIn('slow'); $('#lightbox_loading').fadeIn('slow');  }, 
	    error: function(){ $('#lightbox_loading').hide(); 
	    
	    	alert('Error loading document.');  
	    },
	    success: function(html){ 
	   		$("#lightbox_content").html(html); 
	   		$('#lightbox_loading').hide();
			setTimeout( "$('#lightbox_content').fadeIn('slow');", 50);
			
	    }
	})

	
	lightboxOpenTrue = true;
	
}
function lightbox_close_first()
{
			if(current_page_last == undefined || current_page_last == 'login/')
			{
				current_page_last = 'notes/';
			}
	       $.historyLoad(current_page_last);
}
function lightbox_close()
{

	if(lightboxOpenTrue == true)
	{
	
		$('#lightbox_content').fadeOut('fast');
		setTimeout( "$('#lightbox').fadeOut('fast');", 50);
		lightboxOpenTrue = false;
		return true;
	}
	return false;
}

function toogle_filter_notes()
{
	
	if($('#bottom_filter').attr('class') == 'bottom_filter_on')
	{
		$('#bottom_filter').attr('class', 'bottom_filter');
		$('.home_left_bar_item_all').html('ALL SYNOTES');
		filter_icon = '';
		notes_bar_menu_refresh();
	}
	else
	{
		$.historyLoad('notes/filter/');
	}
}

function notes_filter_icon(icon)
{

	$('#bottom_filter').attr('class', 'bottom_filter_on');
	$('.home_left_bar_item_all').html(icon.replace('_', ' ').toUpperCase()+' SYNOTES');
	filter_icon = icon;
	lightbox_close_first();
	notes_bar_menu_refresh();
}
function lightbox_hash(what_hash)
{
	$.ajax({
	    url: '?i=lightbox_generate&what_hash='+what_hash,
	    type: 'GET',
	    dataType: 'html',
	    timeout: 5000,
		beforeSend: function(){   }, 
	    error: function(){ },
	    success: function(html){ 
	    	$('#lightbox_settings_textbox_'+what_hash).val(html);
	    	document.getElementById('lightbox_settings_textbox_'+what_hash).focus();
	    	document.getElementById('lightbox_settings_textbox_'+what_hash).select();
	    }
	})
}
function pageload(hash) {
	
		
		lightbox_close();
		if(hash != current_page)
		{
			var url = hash.split("/");
			
			current_page_last = current_page;
			if(url[0] == 'settings' || (url[0] == 'notes' && url[1] == 'filter') )
			{
			}
			else
			{
				current_page = hash;
			}
			
			if($("#login_session").html() == '' && url[0] != 'login')
			{
					login_redirect_page = hash;
					current_page = 'login/';
					$.historyLoad('login/');
					return false;
			}
			if(url[0] == 'notes' && url[1] == 'filter')
			{
				lightboxOpen('?i=notes_filter');
				return true;
			}
			
			
				
			switch(url[0])
			{
				case 'settings' :
					lightboxOpen('?i=settings');
				break;
				case 'logout' :
					logout();
				break;
				case 'notes' :
				
				if(section != 'notes')
				{
					load_inner_wraper('notes');
					section = 'notes';
					var slow = true;
				}
				//alert(url[2]);
					switch(url[1])
					{
						case 'add' :
							if(slow == true){ setTimeout( "notes_flip_menu('add', '"+url[2]+"', '');", 1000); } else { notes_flip_menu('add', url[2], ''); }
						break;
						case '' :
							if(slow == true){ setTimeout( "notes_flip_default();", 1000); } else { notes_flip_default(); }
						break;
						default :
								if(slow == true){ setTimeout( "notes_flip_menu('"+url[1]+"', '"+url[2]+"','"+url[3]+"'); ", 1000); } else { notes_flip_menu(url[1], url[2], url[3]); }
						break;
					}
				break;
				
				case 'login' :
				if($("#login_session").html() == '')
				{
						switch(url[1])
						{
							case 'forgot' :
								forgot_login_swap('?i=forgot');
							break;
							case 'forgot_final' :
								forgot_login_swap('?i=forgot_final');
							break;
							case 'signup' :
								forgot_login_swap('?i=signup');
							break;
							default :
								forgot_login_swap('?i=');
							break;
						
						}
				}
				else
				{
					$.historyLoad('notes/');
					return false;
				}
				break;
				
				case 'checklists' :
				
				if(section != 'checklists')
				{
					load_inner_wraper('checklists');
					var slow = true;
					section = 'checklists';
				}
					
					
					switch(url[1])
					{
						case 'add' :
							if(slow == true){ setTimeout( "checklists_flip_menu('add');", 1000); } else { checklists_flip_menu('add'); }
						break;
						case '' :
							if(slow == true){ setTimeout( "checklists_flip_default();", 1000); } else { checklists_flip_default(); }
						break;
						default :
							if(slow == true){ setTimeout( "checklists_flip_menu('"+url[1]+"'); ", 1000); } else { checklists_flip_menu(url[1]); }
						break;
					
					}
					
				break;
				default :
				$.historyLoad('notes/');
					//alert('d');
				break;
			
			}
		}


		

		/*if(hash) {
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like åäö.
				hash = encodeURIComponent(hash);
			}
			$("#load").load(hash + ".html");
		} else {
			// start page
			$("#load").empty();
		}*/
	}
	
	
	
	
function load_inner_wraper(section)
{
//weraper_inner
 
$.ajax({
	    url: '?i=load_inner_wraper',
	    type: 'GET',
	    dataType: 'html',
	    timeout: 5000,
	 	data: "section="+section,
		beforeSend: function(){ $("#weraper_inner").hide();   }, 
	    error: function(){
	        alert('Error loading document.'); 
    },
    success: function(html){ 

    $("#weraper_inner").hide(); 
    $("#weraper_inner").html(html); 
    setTimeout('$("#weraper_inner").show("slow");',50);
    

	
    }
})




	

}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


jQuery.preLoadImages(
'/resources/1.1.9/images/logo.png',
'/resources/1.1.9/images/content_top.png',
'/resources/1.1.9/images/synotes_home_title.png',
'/resources/1.1.9/images/blog_split.png',
'/resources/1.1.9/images/loading.png',
'/resources/1.1.9/images/content_bottom.png',
'/resources/1.1.9/images/button_email.png',
'/resources/1.1.9/images/button_delete.png',
'/resources/1.1.9/images/checklists_button_new_item.png',
'/resources/1.1.9/images/note_icon_default.png',
'/resources/1.1.9/images/note_icon_home.png',
'/resources/1.1.9/images/note_icon_work.png',
'/resources/1.1.9/images/note_icon_school.png',
'/resources/1.1.9/images/note_icon_web.png',
'/resources/1.1.9/images/note_icon_mail.png',
'/resources/1.1.9/images/note_icon_download.png',
'/resources/1.1.9/images/note_icon_hard_drive.png',
'/resources/1.1.9/images/note_icon_tv.png',
'/resources/1.1.9/images/note_icon_music.png',
'/resources/1.1.9/images/note_icon_star.png',
'/resources/1.1.9/images/note_icon_map.png',
'/resources/1.1.9/images/note_icon_clock.png',
'/resources/1.1.9/images/note_icon_calendar.png',
'/resources/1.1.9/images/note_icon_address_book.png',
'/resources/1.1.9/images/note_icon_tick.png',
'/resources/1.1.9/images/note_icon_info.png',
'/resources/1.1.9/images/note_icon_padlock.png',
'/resources/1.1.9/images/note_icon_numbers.png',
'/resources/1.1.9/images/note_icon_alert.png',
'/resources/1.1.9/images/home_banner_top_logo.png',
'/resources/1.1.9/images/lightbox_cancel.png',
'/resources/1.1.9/images/note_center_top_left.png',
'/resources/1.1.9/images/note_center_top_right.png',
'/resources/1.1.9/images/note_center_edit_active.png',
'/resources/1.1.9/images/note_center_icon.png',
'/resources/1.1.9/images/note_center_info.png',
'/resources/1.1.9/images/note_center_revisions.png',
'/resources/1.1.9/images/note_icon_water.png',
'/resources/1.1.9/images/button_delete.png',
'/resources/1.1.9/images/button_delete_rollover.png',
'/resources/1.1.9/images/button_delete.png',
'/resources/1.1.9/images/button_restore.png',
'/resources/1.1.9/images/button_restore_active.png',
'/resources/1.1.9/images/button_restore.png',
'/resources/1.1.9/images/note_center_bottom_left.png',
'/resources/1.1.9/images/note_center_bottom_right.png',
'/resources/1.1.9/images/button_email.png',
'/resources/1.1.9/images/button_email_rollover.png',
'/resources/1.1.9/images/button_email.png',
'/resources/1.1.9/images/button_delete.png',
'/resources/1.1.9/images/button_delete_rollover.png',
'/resources/1.1.9/images/button_delete.png',
'/resources/1.1.9/images/home_textbox.png',
'/resources/1.1.9/images/home_button_cancel.png',
'/resources/1.1.9/images/home_button_cancel_pressed.png',
'/resources/1.1.9/images/home_button_signin.png',
'/resources/1.1.9/images/home_button_signin_pressed.png',
'/resources/1.1.9/images/home_button_done.png',
'/resources/1.1.9/images/home_button_done_pressed.png',
'/resources/1.1.9/images/home_signup_icon_1.png',
'/resources/1.1.9/images/home_signup_icon_2.png',
'/resources/1.1.9/images/home_signup_icon_3.png',
'/resources/1.1.9/images/home_banner_top_sc.png',
'/resources/1.1.9/images/home_banner_top_sc_active.png',
'/resources/1.1.9/images/home_banner_top_settings.png',
'/resources/1.1.9/images/home_banner_top_settings_active.png',
'/resources/1.1.9/images/home_banner_top_logout.png',
'/resources/1.1.9/images/home_banner_top_logout_active.png',
'/resources/1.1.9/images/home_banner_top_background.png',
'/resources/1.1.9/images/home_bar_background.png',
'/resources/1.1.9/images/home_bar_icon_note.png',
'/resources/1.1.9/images/home_bar_icon_reminders.png',
'/resources/1.1.9/images/home_bar_icon_checklist.png',
'/resources/1.1.9/images/home_bar_icon_cloud.png',
'/resources/1.1.9/images/note_icon_banned.png',
'/resources/1.1.9/images/note_icon_support.png',
'/resources/1.1.9/images/note_icon_paperclip.png',
'/resources/1.1.9/images/note_icon_pedestrain.png',
'/resources/1.1.9/images/note_icon_bubble.png',
'/resources/1.1.9/images/note_icon_sync.png',
'/resources/1.1.9/images/note_icon_note_info.png',
'/resources/1.1.9/images/note_icon_network_error.png',
'/resources/1.1.9/images/note_icon_cogwheel.png',
'/resources/1.1.9/images/note_icon_cloud.png',
'/resources/1.1.9/images/note_icon_window.png',
'/resources/1.1.9/images/note_icon_pencil.png',
'/resources/1.1.9/images/note_icon_lock.png',
'/resources/1.1.9/images/home_left_bar_item_selected.png',
'/resources/1.1.9/images/left_menu_bar.png',
'/resources/1.1.9/images/home_footer_background.png',
'/resources/1.1.9/images/note_center_right.png',
'/resources/1.1.9/images/note_center_top.png',
'/resources/1.1.9/images/note_center_bottom.png',
'/resources/1.1.9/images/note_center_left_bar.png',
'/resources/1.1.9/images/note_center_grid.png',
'/resources/1.1.9/images/filter_box.png',
'/resources/1.1.9/images/filter_box_close.png',
'/resources/1.1.9/images/filter_box_close_pressed.png',
'/resources/1.1.9/images/note_center_icon_current.png',
'/resources/1.1.9/images/button_save_hover.png',
'/resources/1.1.9/images/button_save.png',
'/resources/1.1.9/images/lightbox_top.png',
'/resources/1.1.9/images/lightbox_middle.png',
'/resources/1.1.9/images/lightbox_bottom.png',
'/resources/1.1.9/images/spacer.png',
'/resources/1.1.9/images/lightbox_split.png',
'/resources/1.1.9/images/lightbox_save.png',
'/resources/1.1.9/images/notes_revisions_grad.png',
'/resources/1.1.9/images/drag_grip.png',
'/resources/1.1.9/images/left_box_shadow.png',
'/resources/1.1.9/images/lightbox_textbox.png',
'/resources/1.1.9/images/home_left_bar_add.png',
'/resources/1.1.9/images/button_email.png',
'/resources/1.1.9/images/button_delete.png',
'/resources/1.1.9/images/home_left_bar_add_background.png'
);




function put_into_box(box)
{
	$(box).html(box_html);
}

function forgot_login_swap(url)
{
	
	$.ajax({
    url: url,
    type: 'GET',
    dataType: 'html',
    timeout: 5000,
 	data: "ajax=1",
	beforeSend: function(){ 
	
	$("#login_error").hide("slow");  
	$("#login_box_inside").hide();  
	$("#loading_login").show("slow");  
	
	}, 
    error: function(){
        alert('Error loading document.'); 
    },
    success: function(html){ 
	// $("#login_box_inside").hide();
	  box_html = html;
		put_into_box('#login_box_inside');
		setTimeout("$('#login_box_inside').show('slow'); ",50);
		setTimeout("$(\"#loading_login\").hide(\"slow\");  ",50);
			//$("#login_error").hide("slow"); 
		//
	//	$("#loading_login").slideToggle("slow");
	
    }
	})
}

function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}


function forgot_final()
{
	var newpw = $('#forgot_password').val();
	var newpw_confirm = $('#forgot_password_confirm').val();
	if(newpw_confirm != newpw)
	{
		$("#login_error").html('Oops! Your new passwords do not match.');
									setTimeout("$(\"#login_error\").fadeIn(\"slow\");",500);

		return false;
	}
	//forgot_password_confirm
	
	var hash = location.hash.replace(/\?.*$/, '');
	var url = hash.split("/");
	var token = url[2];
	
	
			$.ajax({       
							type: "POST",
							url: "?ajax=1&i=forgot_final_send",
							data: "password="+newpw+"&token="+token,
							timeout: 5000,  
							beforeSend: function(){ 
							//$("#login_error").hide();  $("#login_box_inside").slideToggle("slow");  
							$("#loading_login").slideToggle("slow");  
							}, 
							complete: function(){ }, 
							success: function(html){
						
								 if(html == 1)
								 {
								    //$("#loading_login").slideToggle("fast");
								    //$.historyLoad($('#start_page').html());
									//$("#login_error").html('Password reset email sent.');
									//setTimeout("$(\"#login_error\").fadeIn(\"slow\");",500);
									//$("#login_box_inside").slideToggle("slow");  
									$.historyLoad('login/');
									
									setTimeout("$(\"#login_error\").html('Your password has now been reset.');",10);
									setTimeout("$(\"#login_error\").show('slow');",20);
									
									
								 } else if(html == 2)
								 {
									
									$("#login_error").html('Oops! There appears to have been an error<br> with your link. Please contact syncode support.');
									setTimeout("$(\"#login_error\").fadeIn(\"slow\");",500);
									//$("#login_box_inside").slideToggle("slow"); 
									$("#loading_login").slideToggle("slow");

								 }
								 else
								 {
									alert('Error Code: 6; Please report to synotes@syncode.com.au; details:'+html); 
								 }
							
							// alert(html); 
							}
				 }); //close $.ajax(

}
function isValidEmail(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);


}


function signup()
{


	var email = $('#sign_up_email').val();
	var password = $('#sign_up_password').val();
	var password_confirm = $('#sign_up_password_confirm').val();
	
	
	if(password == '' || email == '')
	{
	
		$("#login_error").html('You must enter a username and password.');
		setTimeout("$(\"#login_error\").fadeIn(\"slow\");",100);
		
		return false;
	}
	
	if(password_confirm != password)
	{
	
		$("#login_error").html('Your passwords must match.');
		setTimeout("$(\"#login_error\").fadeIn(\"slow\");",100);
		
		return false;
	}
	if(isValidEmail(email) == false)
	{
		$("#login_error").html('Your must enter a valid email.');
		setTimeout("$(\"#login_error\").fadeIn(\"slow\");",100);
		return false;
	
	}
	
						$.ajax({       
							type: "POST",
							url: "?i=signup_send",
							data: "ajax=1&email="+email+"&password="+password,
							timeout: 5000,
							beforeSend: function(){ 
							$("#login_error").hide("slow");   
							$("#loading_login").slideToggle("slow");  
						}, 
							complete: function(){ }, 
							success: function(html){
							
							
								 if(html == 1)
								 {
									 $("#main_content").slideUp("slow");
									$("#header_top").slideUp("slow");
									$("#footer").slideUp("slow");
						
									$("#login_session").html(11)
									setTimeout("load_wraper();",800);
									
								 }
								 else
								 {
								    $("#loading_login").slideToggle("slow");
									$("#login_error").html(html);
									setTimeout("$(\"#login_error\").fadeIn(\"slow\");",500);
									
								 	//alert('Error Code: 2; Please report to synotes@syncode.com.au ');
								 }
									
							// alert(html); 
							}
				 }); //close $.ajax(
}
function forgot()
{
	var email = $('#forgot_email').val();
			$.ajax({       
							type: "POST",
							url: "?ajax=1&i=forgot_send",
							data: "email="+email,
							timeout: 5000,
							beforeSend: function(){ 
								$("#login_error").hide("fast");  
							//	$("#login_box_inside").slideToggle("slow");  
								$("#loading_login").slideToggle("slow");  
							}, 
							complete: function(){ }, 
							success: function(html){
							
								 if(html == 1)
								 {
								 	$("#forgot_send_form").hide('fast');
								    $("#loading_login").slideToggle("fast");
								 	$("#login_error").html('Password reset email sent.');
									setTimeout("$(\"#login_error\").fadeIn(\"slow\");",10);
								//
									
								 } else if(html == 2)
								 {
									
									$("#login_error").html('Oops! We are unable to find your email.');
									setTimeout("$(\"#login_error\").fadeIn(\"slow\");",500);
									$("#loading_login").slideToggle("slow");
									
									
									
								 }
								 else
								 {
									alert('Error Code: 1; Please report to synotes@syncode.com.au'); 
								 }
							
							// alert(html); 
							}
				 }); //close $.ajax(

}

var notes_menu_nums=new Array();
var notes_menu_ids=new Array();
notes_menu_nums[1]='edit';
notes_menu_nums[2]='icon';
notes_menu_nums[3]='info';
notes_menu_nums[4]='revisions';

notes_menu_ids['edit'] = 1;
notes_menu_ids['icon'] = 2;
notes_menu_ids['info'] = 3;
notes_menu_ids['revisions'] = 4;

function load_keyboard(position)
{

	var current_id = $('#id').attr("value");


	if(position == 'down')
	{
		
		if(current_id == '' || notes_nums[notes_ids[current_id]*1] == notes_nums[notes_nums.length*1-1])
		{
			if(notes_nums[1] == '')
				{
					var i=2;
					for (i=2;i<=10;i++)
					{
						var pg = notes_nums[i];
						if(pg != 'undefined' && pg != undefined && pg != '')
						{
							i = 100;
						}
					}
				}
				else
				{
					var pg = notes_nums[1];
				}
				
				$.historyLoad('notes/'+pg+'/');
				
				
		}
		else
		{
	
				if(notes_nums[notes_ids[current_id]*1+1] == '')
				{
					var i=2;
					for (i=2;i<=10;i++)
					{
						var pg = notes_nums[notes_ids[current_id]*1+i];
						if(pg != 'undefined' && pg != undefined && pg != '')
						{
							i = 100;
						}
					
					}
				}
				else
				{
					var pg = notes_nums[notes_ids[current_id]*1+1];
				}
				
				$.historyLoad('notes/'+pg+'/');
				
			//$.historyLoad('notes/'+notes_nums[notes_ids[current_id]*1+1]+'/');
		}
		
	}
	
	if(position == 'up')
	{
		if(current_id == '' || notes_ids[current_id]*1-1 == 0)
		{
				if(notes_nums[notes_nums.length*1-1] == '')
				{
					var i=2;
					for (i=2;i<=10;i++)
					{
						var pg = notes_nums[notes_nums.length-i];
						if(pg != 'undefined' && pg != undefined && pg != '')
						{
							i = 100;
						}
					
					}

				}
				else
				{
					var pg = notes_nums[notes_nums.length*1-1];
				}
				$.historyLoad('notes/'+pg+'/');

		}
		else
		{	
			
				if(notes_nums[notes_ids[current_id]*1-1] == '')
				{
					var i=2;
					for (i=2;i<=10;i++)
					{
						var pg = notes_nums[notes_ids[current_id]-i];
						if(pg != 'undefined' && pg != undefined && pg != '')
						{
							i = 100;
						}
					
					}
				}
				else
				{
					var pg = notes_nums[notes_ids[current_id]*1-1];
				}
				
				$.historyLoad('notes/'+pg+'/');

	}
			//$.historyLoad('notes/'+notes_nums[notes_ids[current_id]*1-1]+'/');
			
	}
	
	if(position == "right")
	{

		if(current_id != '')
		{
			if(notes_menu_nums[notes_menu_ids[notes_flip_sub_menu_current]*1] == notes_menu_nums[notes_menu_nums.length*1-1])
			{
				$.historyLoad('notes/'+current_id+'/');
			}
			else
			{ 
				
				$.historyLoad('notes/'+current_id+'/'+notes_menu_nums[notes_menu_ids[notes_flip_sub_menu_current]*1+1]);
			}
		}
		
	}
	
	if(position == "left")
	{

		if(current_id != '')
		{
			if(notes_flip_sub_menu_current == 'edit')
			{
				
				$.historyLoad('notes/'+current_id+'/'+notes_menu_nums[notes_menu_nums.length-1]);
			}
			else
			{ 
				$.historyLoad('notes/'+current_id+'/'+notes_menu_nums[notes_menu_ids[notes_flip_sub_menu_current]*1-1]);
			}
		}
		
	}
}

function logout()
{

	$.ajax({       
							type: "POST",
							url: "?i=logout",
							timeout: 5000,
							data: "ajax=1",
							beforeSend: function(){ 
								//$("#login_error").hide("slow");  $("#loading_login").slideToggle("slow");  
							}, 
							complete: function(){ }, 
							success: function(html){
							
								box_html = html;
							  	$("#wraper").fadeOut('slow');
							  	
								setTimeout("put_into_box('#wraper');",1000);
								setTimeout("$(\"#wraper\").show('slow');",1005);
								$("#login_session").html('')
								login_redirect_page = 'notes/';
								setTimeout("$.historyLoad('login/');",1020);  
								section = '';
							
							/*
								 if(html == 1)
								 {
								 	
									$("#main_content").slideUp("slow");
									$("#header_top").slideUp("slow");
									$("#footer").slideUp("slow");
									  									
									$("#login_session").html(1)
									if(login_redirect_page == '' || login_redirect_page == undefined){ login_redirect_page = 'notes/'; }
									setTimeout("load_wraper();",500);
									setTimeout("$.historyLoad('"+login_redirect_page+"');",1000);  
									
								 }
								 else if(html == 2)
								 {
									
									
									$("#login_box_inside").slideToggle("slow"); 
									$("#loading_login").slideToggle("slow");
									setTimeout("$(\"#login_error\").slideToggle('slow'); ",800);
									
									
								 }
								 else
								 {
								 	alert('Error Code 3: '+html);
								 }*/
									
							// alert(html); 
							}
				 }); //close $.ajax(
			
}

function login()
{
	
	var email = $('#login_email').val();
	var password = $('#login_password').val();
	var visitortime = new Date();
	var timezoneoffset = visitortime.getTimezoneOffset();
			$.ajax({       
							type: "POST",
							url: "?i=login",
							timeout: 5000,
							data: "ajax=1&timezoneoffset="+timezoneoffset+"&email="+email+"&password="+password+"&remember_me="+$('#remember_me_input').attr('checked'),
							beforeSend: function(){ 
								$("#loading_login").slideToggle("slow");
								//$("#login_error").hide("slow");  $("#loading_login").slideToggle("slow");  
							}, 
							complete: function(){ }, 
							success: function(html){
							
								 if(html == 1)
								 {
								 	
									$("#main_content").slideUp("slow");
									$("#header_top").slideUp("slow");
									$("#footer").slideUp("slow");
															
									$("#login_session").html(1)
									setTimeout("load_wraper();",500);
									
									
								 }
								 else if(html == 2)
								 {
									
									
								//	$("#login_box_inside").slideToggle("slow"); 
									$("#loading_login").slideToggle("slow");
									setTimeout("$(\"#login_error\").slideToggle('slow'); ",800);
									
									
								 }
								 else
								 {
								 	alert('Error Code 3: '+html);
								 }
									
							// alert(html); 
							}
				 }); //close $.ajax(

}

function load_wraper()
{

	$.ajax({
    url: '?i=load_wraper',
    type: 'GET',
    dataType: 'html',
    timeout: 5000,
 	data: "",
	beforeSend: function(){ $("#wraper").hide(); }, 
    error: function(){
        alert('Error loading document.'); 
    },
    success: function(html){ 
	 	box_html = html;
	  	
		put_into_box('#wraper');
		setTimeout("$(\"#wraper\").fadeIn('slow');",5);
		
		if(login_redirect_page == '' || login_redirect_page == undefined){ login_redirect_page = 'notes/'; }
		setTimeout("$.historyLoad('"+login_redirect_page+"');",5);  
    }
	})
}

function make_title(id)
{
	var val = document.getElementById(id);
	if(val.value == ''){ val.style.color="#7a7a7a"; val.value='Untitled';  }
}
function remove_title_no_color(id)
{
	var val = document.getElementById(id);
	if(val.value == 'Untitled'){  val.value='';  }
}
function remove_title(id)
{
	var val = document.getElementById(id);
	if(val.value == 'Untitled'){  val.value=''; val.style.color="#222";  }
}
function check_title(id)
{
	var val = document.getElementById(id);
	if(val.value == 'Untitled'){  val.style.color="#7a7a7a";  } else { val.style.color="#222";   }
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


function notes_flip_default()
{
	var current_id = $('#id').attr("value");
	$('#home_content_centre_message').fadeIn();
	$('#note_centre_box').hide();
	$('#menu-'+current_id).attr("class", 'home_left_bar_item');
	$('#id').attr("value", '');
}

function checklists_flip_default()
{
	var current_id = $('#id').attr("value");
	$('#home_content_centre_message').fadeIn();
	$('#note_centre_box').hide();
	$('#menu-'+current_id).attr("class", 'home_left_bar_item');
	$('#id').attr("value", '');
	$('#checklist').html('');
		$('#checklist_items').html('');
		$('#checklist_items_new').html('');
		$('#notes_centre_message').html("");
}


// this fine function is from the great people at php.js
function prepare_text(text)
{
	text = (text+'').toString();
	return encodeURIComponent(text).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
	                                                                replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
	
}
function notes_form_submit(action)
{

	if($('#id').val() == 'add')
	{
		var action = 'add';
		var autosave = true;
	}


	switch(action)
	{
		case 'add' :
		
			var current_hash = location.hash.replace(/\?.*$/, '');
			if(current_hash == '#notes/add/' ||  current_hash == '#notes/add/icon' || current_hash == '#notes/add/info' || current_hash == '#notes/add')
			{
				$('#id').val('');
				$.ajax({       
							type: "POST",
							url: "?ajax=1&i=notes",
							timeout: 5000,
							data: "action=add_send&title="+prepare_text( $('#title').val() )+"&text="+prepare_text( $('#text').val() )+"&icon="+$('#icon_current').val(),
							beforeSend: function(){  
							//$('.note_add_button').attr("disabled", 'disabled'); 
							$('#notes_centre_message').html("<img src=\"/resources/1.1.9/images/loading.png\" />"); }, 
							complete: function(){ }, 
							success: function(html){
							
							$('#bottom_add').attr("class", 'bottom_add');
							
							$('.notes_buttons_bar_left').show();
							$('#note_center_link_info').attr("href", '#notes/'+html+'/info');
							$('#note_center_link_edit').attr("href", '#notes/'+html+'/');
							$('#note_center_link_icon').attr("href", '#notes/'+html+'/icon');
							$('#note_center_link_revisions').attr("href", '#notes/'+html+'/revisions');
							
							//alert(html);
							 //$('.note_add_button').attr("disabled", ''); 
							
							$('#id').val(html)
							current_page = 'notes/'+html+"/";
							$.historyLoad('notes/'+html+"/");
							$('#home_left_bar_add').attr("class", 'home_left_bar_item_bottom_inner home_left_bar_item');
							
							var count = $('#title').val().length;
							var title = $('#title').val(); 
							
							
							notes_bar_menu_refresh();
							
						
							
							var objDiv = document.getElementById("home_left_bar");
							objDiv.scrollTop = objDiv.scrollHeight;
							
							generate_notes_lists();
							
							var currentTime = new Date()
							var text = $('#text').val();
							var date = currentTime.getDate();
							var month_d = currentTime.getMonth();
							var month_c = month[month_d];
							var year = currentTime.getFullYear();
							$('#info_note_title').html($('#title').val());
							$('#info_note_last_modified').html(month_c+" "+date+" "+year);
							$('#info_note_total_updates').html(1);
							$('#info_note_words').html(word_count(text) );
							$('#info_note_characters').html(text.length);
							$('#info_note_lines').html(countlines(text) );
							$('#info_note_paragraphs').html(paragraph_count(text));
							$('#info_note_created_at').html(month_c+" "+date+" "+year);
							$('#info_note_created_from').html('Synote Web');
							 if(autosave == true)
							 {
							 	
										var currentTime = new Date()
										var hours = currentTime.getHours()
										var minutes = currentTime.getMinutes()
										if (minutes < 10){
										minutes = "0" + minutes
										}
										var time_ = hours + ":" + minutes + " ";
										
										if(hours > 11){
											var after_ = 'PM';
											hours = hours - 12;
											if(hours == 0)
											{
												hours = 12;
											}
										} else {
											var after_ = 'AM';
										}

										 $('#notes_centre_message').html("Auto Created at " + hours + ":" + minutes + " " +after_+"!"); 
											setTimeout("notes_form_submit('autosave');", autosave_time);
							 }
							 else
							 {
							 	$('#notes_centre_message').html("Added!"); 
							 }

							// alert(html); 
							}
						}); //close $.ajax(
				}
		break;
		case 'mail' :
			var text = $('#text').val().replace("\n", "%0D%0D");
			var body = 'Note Title: '+$('#title').val()+' %0D%0DNote: %0D'+text;
			window.location = 'mailto:?body='+body+'&subject=Note: '+$('#title').val();
		break;
		case 'delete' :
		if(confirm('Are you very sure you want to delete this note?'))
		{
		var id_c = $('#id').val();
		var num = notes_ids[id_c]; 
		notes_ids[id_c] = '';
		notes_nums[num]='';
		
		//return false;
		$.ajax({       
							type: "POST",
							url: "?ajax=1&i=notes",
							timeout: 5000,
							data: "action=delete&id="+id_c,
							beforeSend: function(){  
							//$('.note_add_button').attr("disabled", 'disabled'); 
							$('#notes_centre_message').html("<img src=\"/resources/1.1.9/images/loading.png\" />"); }, 
							complete: function(){ }, 
							success: function(html){
							$('.notes_buttons_bar_left').show();
							$('#'+id_c+'-value').hide('slow');
							$('#'+id_c+'-value').attr('id','');
							generate_notes_lists();
							$.historyLoad('notes/'); 
							notes_bar_arr[id_c] = '';
							
							
							}
							
							
							//notes_nums[notes_nums.length]=html;
				}); //close $.ajax(
							 
		}
		//$('#id').val()
		break;
		default:
		
		if($('#id').val() != '' && $('#id').val() != 'add')
		{
		
		if($('#title').val() == notes_title_last_save && $('#text').val() == notes_text_last_save && notes_icon_last_save == $('#icon_current').val())
		{
			if(action != 'autosave')
			{
				$('#notes_centre_message').html("Saved!"); 
			}
		}
		else
		{
		
				$.ajax({       
							type: "POST",
							url: "?ajax=1&i=notes",
							timeout: 5000,
							data: "action=edit&id="+$('#id').val()+"&title="+prepare_text($('#title').val())+"&text="+prepare_text($('#text').val())+"&icon="+$('#icon_current').val(),
							beforeSend: function(){  
							
							
							
							//$('.note_add_button').attr("disabled", 'disabled'); 
				
							$('#notes_centre_message').html("<img src=\"/resources/1.1.9/images/loading.png\" />"); 
					
							}, 
							complete: function(){ }, 
							success: function(html){
							
							notes_title_last_save = $('#title').val();  
							notes_text_last_save = $('#text').val(); 
							notes_icon_last_save = $('#icon_current').val();
							
							
							//notes_bar_menu_refresh();
							
							var title = $('#title').val();
														
							$('#menu-'+$('#id').val()).html('<span class="home_left_bar_icon home_left_bar_icon_'+$('#icon_current').val()+'"> </span><div class="home_left_bar_item_title">'+title+'</div>');
														
														
							
							
							$('#info_note_title').html($('#title').val());
							var currentTime = new Date()
							var text = $('#text').val();
							var date = currentTime.getDate();
							var month_d = currentTime.getMonth();
							var month_c = month[month_d];
							var year = currentTime.getFullYear();
							$('#info_note_last_modified').html(month_c+" "+date+" "+year);
							var old_num = $('#info_note_total_updates').html()*1;
							$('#info_note_total_updates').html(old_num+1);
							$('#info_note_words').html(word_count(text) );
							$('#info_note_characters').html(text.length);
							$('#info_note_lines').html(countlines(text) );
							$('#info_note_paragraphs').html(paragraph_count(text));
							
								 if(html == 1)
								 {
								 	var currentTime = new Date()
									var text = $('#text').val();
									var date = currentTime.getDate();
									var month_d = currentTime.getMonth();
									var month_c = month[month_d];
									var year = currentTime.getFullYear();
							
									if(action == 'autosave')
									{
										var currentTime = new Date()
										var hours = currentTime.getHours()
										var minutes = currentTime.getMinutes()
										if (minutes < 10){
										minutes = "0" + minutes
										}
										var time_ = hours + ":" + minutes + " ";
										
										if(hours > 11){
											var after_ = 'PM';
											hours = hours - 12;
											if(hours == 0)
											{
												hours = 12;
											}
										} else {
											var after_ = 'AM';
										}

										 $('#notes_centre_message').html("Auto Saved at " + hours + ":" + minutes + " " +after_+"!"); 
										 setTimeout("notes_form_submit('autosave');", autosave_time);
									}
									else
									{
										 $('#notes_centre_message').html("Saved!"); 
									}
								 }
								 else
								 {
									alert('Error Code: 3.1; Please report to synotes@syncode.com.au; '+html); 
								 }
							
					}
							
				 }); 
				 
				 }
				 
				 
			break;
				
		}
		
		
		}
		

	   	
}


function clear_info_notes()
{
			$('#info_note_title').html('');
			$('#info_note_last_modified').html('');
			$('#info_note_total_updates').html('');
			$('#info_note_words').html('');
			$('#info_note_characters').html('');
			$('#info_note_lines').html('');
			$('#info_note_paragraphs').html('');
			$('#info_note_created_at').html('');
			$('#info_note_created_from').html('');
		
}

function countlines(area) 
{ 
  var text = area.replace(/\s+$/g,"") 
  var split = text.split("\n") 
  return split.length 
} 

function paragraph_count(area) 
{ 
  var text = area.replace(/\s+$/g,"") 
  var split = text.split("\n\n") 
  return split.length 
} 

function word_count(y){
var r = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
return r;
} 

function checklists_form_submit(action)
{



	switch(action)
	{
		case 'add' :
				$('#id').val('');
				var checklist_items = $('#checklist_items').html();
				var i=1;
				var items = '';
				
				
				var checklist_items_arr = checklist_items.split(",");

				for(i = 0; i < checklist_items_arr.length; i++)
				{
					if(checklist_items_arr[i] == ''){ } else { var items = items+"checklist-item-input-"+checklist_items_arr[i]+"="+$('#checklist-item-input-'+checklist_items_arr[i]).val()+"&"+"checklist-item-input-ticked-"+checklist_items_arr[i]+"="+$('#checklist-item-input-ticked-'+checklist_items_arr[i]).val()+"&"; }
				}

				
				var order = $('#checklist').sortable('serialize');
				
				
						$.ajax({       
							type: "POST",
							url: "?ajax=1&i=checklists&",
							timeout: 5000,
							data: order+"&action=add_send&title="+$('#title').val()+"&checklist_items="+checklist_items+"&"+items,
							beforeSend: function(){  
							
							//$('.note_add_button').attr("disabled", 'disabled'); 
							$('#notes_centre_message').html("<img src=\"/resources/1.1.9/images/loading.png\" />"); }, 
							complete: function(){ }, 
							success: function(json){
							
							var uuid = $.evalJSON(json).uuid; 
							var id = $.evalJSON(json).id; 
							$('.notes_buttons_bar_left').show();
							//alert(html);
							 //$('.note_add_button').attr("disabled", ''); 
							
							
							$('#id').val(uuid);
							$('#id_').val(id);
							
							$.historyLoad('checklists/'+uuid);
							$('#home_left_bar_add').attr("class", 'home_left_bar_item_bottom_inner home_left_bar_item');
							$("#home_left_bar").append('<a class="home_left_bar_link" href="#checklists/'+uuid+'" ><div class="home_left_bar_item home_left_bar_item_selected"  id="menu-'+uuid+'" title="'+uuid+'" >'+$('#title').val()+'</div></a>'); 
							
							var objDiv = document.getElementById("home_left_bar");
							objDiv.scrollTop = objDiv.scrollHeight;
							
							$('#notes_centre_message').html("Added!"); 
							 $('#notes_centre_message').fadeIn('slow'); 
							
							// alert(html); 
							}
					}); //close $.ajax(
		break;
		case 'mail' :

				var checklist_items = $('#checklist_items').html();
				var i=1;
				var body = 'Checklist Title: '+$('#title').val()+' %0D%0D Checklist Items: %0D';
					
				var checklist_items_arr = checklist_items.split(",");
			
				for(i = 0; i < checklist_items_arr.length; i++)
				{
					if(i == '')
					{ } else
					{
						var inIt = $('#checklist-item-input-'+checklist_items_arr[i]).val();
						body = body+" "+i+". "+inIt+"%0D";
						
					}
				}
		
		window.location = 'mailto:?body='+body+'&subject=Checklist: '+$('#title').val();
		break;
		case 'delete' :
		if(confirm('Are you very sure you want to delete this checklist?'))
		{
		var id_c = $('#id').val();
		$.ajax({       
							type: "POST",
							url: "?ajax=1&i=checklists",
							data: "action=delete&id="+id_c,
							timeout: 5000,
							beforeSend: function(){  $('#notes_centre_message').html("<img src=\"/resources/1.1.9/images/loading.png\" />"); }, 
							complete: function(){ }, 
							success: function(html){
							if(html == 1)
							{
								$('.notes_buttons_bar_left').show();
								$('#menu-'+id_c).hide("slow");
								$.historyLoad('checklists/');
							}
							else
							{
								alert('Error Code: 9; Please report to synotes@syncode.com.au'); 

							
							}
							}
						}); //close $.ajax(
							 
		}
		break;
		default:
		
		
			
				var checklist_items = $('#checklist_items').html();
				var i=1;
				var items = '';
				
				
				var checklist_items_arr = checklist_items.split(",");

				for(i = 0; i < checklist_items_arr.length; i++)
				{
					if(checklist_items_arr[i] == ''){ } else { var items = items+"checklist-item-input-"+checklist_items_arr[i]+"="+$('#checklist-item-input-'+checklist_items_arr[i]).val()+"&"+"checklist-item-input-ticked-"+checklist_items_arr[i]+"="+$('#checklist-item-input-ticked-'+checklist_items_arr[i]).val()+"&"; }				}

				
				var order = $('#checklist').sortable('serialize');
				//alert(items);
				
							$.ajax({       
							type: "POST",
							url: "?ajax=1&i=checklists&",
							timeout: 5000,
							data: order+"&action=edit&checklist_items_new="+$('#checklist_items_new').html()+"&checklist_id="+$('#id_').val()+"&id="+$('#id').val()+"&title="+$('#title').val()+"&checklist_items="+checklist_items+"&"+items,
							beforeSend: function(){  
							
							//$('.note_add_button').attr("disabled", 'disabled'); 
							$('#notes_centre_message').html("<img src=\"/resources/1.1.9/images/loading.png\" />"); }, 
							complete: function(){ }, 
							success: function(json){
							//alert(json);
							$('#checklist_items_new').html('');
							$('#menu-'+$('#id').val()).html($('#title').val())
								 if(json == 1)
								 {
									 
									 $('#notes_centre_message').html("Saved!"); 
								 }
								 else
								 {
								
									alert('Error Code: 8; Please report to synotes@syncode.com.au'); 
								 }
							
							// alert(html); 
							}
							
				 }); //close $.ajax(
			break;
				
		}

	   	
}

function checklists_flip_menu(id)
{
	//alert(id);
		$('#checklist').html('');
		$('#checklist_items').html('');
		$('#checklist_items_new').html('');
		$('#notes_centre_message').html("");
		var current_id = $('#id').attr("value");
		if(current_id == '' || current_id == false)
		{ 
			$('#home_content_centre_message').fadeOut();
		} else { 
			$('#home_content_centre_message').fadeOut();
			$('#note_centre_box').hide();	
			$('#menu-'+current_id).attr("class", 'home_left_bar_item');
			if(current_id == 'add'){ $('#home_left_bar_add').attr("class", 'home_left_bar_item_bottom_inner home_left_bar_item'); }
			
		} 
	if(id == 'add')
	{
		//alert('add');
		$('#home_left_bar_add').attr("class", 'home_left_bar_item_bottom_inner home_left_bar_item home_left_bar_item_selected');
		$('#title').attr("value", 'Untitled');
		$('#id_').val('x');
		//check_title('title');
		//$('#text').val('');
		checklists_new_item();  
		$('#note_centre_box').fadeIn();
		$('.notes_buttons_bar_left').hide();
		$('#id').val('add');
		
	}
	else
	{
		if(current_page_add == 'add'){ } else {
		$('.notes_buttons_bar_left').show();
		$('#home_content_centre_loading').slideToggle('fast');
		}
		url = "?i=checklists&ajax=1&checklist_uuid="+id;
		//alert(url);
		$('#menu-'+id).attr("class", 'home_left_bar_item home_left_bar_item_selected');
		
		$.getJSON(url, function(json) {
		if(json == 2)
		{
			$('#home_content_centre_loading').slideToggle('fast');
			$.historyLoad('checklists/');
		}
		else
		{
		//title
				//alert(json[0]["title"]);
				if(current_page_add == 'add'){ } else {
				$('.notes_buttons_bar_left').show();
				$('#note_centre_box').fadeIn();
				}
				$('#menu-'+json[0]["uuid"]).attr("class", 'home_left_bar_item home_left_bar_item_selected');
				$('#id').attr("value", json[0]["uuid"]);
				$('#id_').attr("value", json[0]["checklist_id"]);
				//checklist_id
				$('#title').attr("value",json[0]["title"]);
				check_title('title');
				
				var checklist_items='';

				
					for( var i =0; i <json.length; i++) 
					{
						if(json[i]['text'] != undefined)
						{ 
						var checklist_items = checklist_items+','+json[i]['item_id'];
						checklists_create_item(json[i]['item_id'], json[i]['text'], json[i]['ticked']);
						}
					}
				
				//alert(checklist_items);
				//$('#text').val(json[0]["text"]);
				//checklist_items
				$('#checklist_items').html(checklist_items);
				$('#home_content_centre_loading').slideToggle('fast');
	   		
	   	}
	   	
	   	 });
   	}
   	
	return false;
}
function revision_restore()
{
	if(prev_hid != '') { 
	
		$('#title').attr("value",hid_title);
		$('#text').val(hid_text);
		notes_form_submit($('#id').val());
		$.historyLoad('notes/'+$('#id').val()+'/');
		$('#notes_centre_message').html("Restored");
	}
	 
		
}
function revision_delete()
{
	
	if(prev_hid != '') 
	{ 	
		if( confirm("Are you sure you want to delete this revision?") )
		{
			$('#notes_history_menu_'+prev_hid).hide('slow');
			
			$('#revisions_content_text').html('');
			$('#revisions_content_title').html('');
			$('#revisions_content_title').attr('class', '');
			
								$.ajax({       
										type: "GET",
										url: "?i=delete_note_history_item&hid="+prev_hid,
										timeout: 5000,
										success: function(html){
										
										if(html == 1)
										{
										
											
										}
										else
										{
											alert('Error Code: 10; Please report to synotes@syncode.com.au.'); 
			
										
										}
										}
									}); //close $.ajax(
									
									$.historyLoad('notes/'+$('#id').attr("value")+'/revisions');
									
									
		}
	}
	
}
function note_swap_icon(icon)
{
	$('#icon_current_img').attr('src','/resources/1.1.9/images/note_icon_'+icon+'.png');
	$('#icon_current').val(icon);
	
}

function notes_flip_menu(id, menu, hid)
{
 	var notes_flip_sub_menu_current_old = notes_flip_sub_menu_current;
 	
	var current_id = $('#id').attr("value");
	

	if(current_id == id)
	{
		if( (hid == null || hid == '' || notes_flip_sub_menu_current != 'revisions' ) && menu != notes_flip_sub_menu_current)
		{
			if(menu == ''){ menu = 'edit' }
			$('#note_center_'+notes_flip_sub_menu_current+'_img').attr('src',"/resources/1.1.9/images/note_center_"+notes_flip_sub_menu_current+".png");
			$('#note_center_'+notes_flip_sub_menu_current+'_area').fadeOut("slow");
			$('#note_center_'+menu+'_img').attr('src',"/resources/1.1.9/images/note_center_"+menu+"_active.png");
		
			if(menu != 'revisions')
			{ 
				$('#note_center_'+menu+'_area').fadeIn("slow");
			}
			
			notes_flip_sub_menu_current = menu;
		}
	}
	else
	{
		 
	
		$('#notes_centre_message').html("");

		$('#home_content_centre_message').fadeOut();
	
		if(current_id == '' || current_id == false)
		{ 
		} else { 
			$('#note_centre_box').hide();	
			$('#menu-'+current_id).attr("class", 'home_left_bar_item');
			if(current_id == 'add'){ $('#home_left_bar_add').attr("class", 'home_left_bar_item_bottom_inner home_left_bar_item'); }
			
		}
	
	if(id == 'add')
	{

		$('#bottom_add').attr("class", 'bottom_add_on');
		$('#home_left_bar_add').attr("class", 'home_left_bar_item_bottom_inner home_left_bar_item home_left_bar_item_selected');
		$('#title').attr("value", 'Untitled');
		check_title('title');
		$('#text').val('');
		$('#note_centre_box').fadeIn();
		$('.notes_buttons_bar_left').hide();
		$('#id').val('add');
		note_swap_icon('default');
		menu = 'edit';
		if(menu != notes_flip_sub_menu_current)
		{
			
			$('#note_center_'+menu+'_img').attr('src',"/resources/1.1.9/images/note_center_"+menu+"_active.png");
			$('#note_center_'+notes_flip_sub_menu_current+'_img').attr('src',"/resources/1.1.9/images/note_center_"+notes_flip_sub_menu_current+".png");
			$('#note_center_'+menu+'_area').fadeIn("slow");
			$('#note_center_'+notes_flip_sub_menu_current+'_area').fadeOut("slow");
			notes_flip_sub_menu_current = menu;
		}
		$('#note_center_link_info').attr("href", '#notes/add/info');
		$('#note_center_link_edit').attr("href", '#notes/add/');
		$('#note_center_link_icon').attr("href", '#notes/add/icon');
		$('#note_center_link_revisions').attr("href", '#notes/add/revisions');
		clear_info_notes();
	}
	else
	{
 		$('#bottom_add').attr("class", 'bottom_add');
		$('.notes_buttons_bar_left').show();
		$('#home_content_centre_loading').slideDown('fast');
		url = "?i=notes&ajax=1&id="+id;
		$('#menu-'+id).attr("class", 'home_left_bar_item home_left_bar_item_selected');
		$.getJSON(url, function(json) {
		if(json == 2)
		{
			$.historyLoad('notes/');
		}
		else
		{
		if(menu == ''){ menu = 'edit' }
		if(notes_flip_sub_menu_current == ''){ notes_flip_sub_menu_current = 'edit'; }
		
		if(menu != notes_flip_sub_menu_current)
		{
		
			$('#note_center_'+menu+'_img').attr('src',"/resources/1.1.9/images/note_center_"+menu+"_active.png");
			$('#note_center_'+notes_flip_sub_menu_current+'_img').attr('src',"/resources/1.1.9/images/note_center_"+notes_flip_sub_menu_current+".png");
			$('#note_center_'+notes_flip_sub_menu_current+'_area').hide();
			if(menu != 'revisions')
			{ 
				$('#note_center_'+menu+'_area').fadeIn("slow");
			}
			notes_flip_sub_menu_current = menu;
		}
			
				$('.notes_buttons_bar_left').show();
				
				$('#note_centre_box').fadeIn();
				//note_center_link_info
				note_swap_icon(json["icon"]);
			
			
				notes_title_last_save = json["title"];
				notes_text_last_save = json["text"];
				notes_icon_last_save = json["icon"];
				
			
				$('#info_note_title').html(json["info_note_title"]);
				$('#info_note_created_at').html(json["info_note_created_at"]);
				$('#info_note_created_from').html(json["info_note_created_from"]);
				$('#info_note_last_modified').html(json["info_note_last_modified"]);
				$('#info_note_total_updates').html(json["info_note_total_updates"]);
				$('#info_note_words').html(json["info_note_words"]);
				$('#info_note_characters').html(json["info_note_characters"]);
				$('#info_note_lines').html(json["info_note_lines"]);
				$('#info_note_paragraphs').html(json["info_note_paragraphs"]);
				
				$('#note_center_link_info').attr("href", '#notes/'+json["uuid"]+'/info');
				$('#note_center_link_edit').attr("href", '#notes/'+json["uuid"]+'/');
				$('#note_center_link_icon').attr("href", '#notes/'+json["uuid"]+'/icon');
				$('#note_center_link_revisions').attr("href", '#notes/'+json["uuid"]+'/revisions');
				
				$('#menu-'+json["uuid"]).attr("class", 'home_left_bar_item home_left_bar_item_selected');
				$('#id').attr("value", json["uuid"]);
				$('#title').attr("value",json["title"]);
				check_title('title');
				$('#text').val(json["text"]);
	   		
	 		
	   	}
	   	
	   	 });
	   	 
   	}
   	
   	
   	}
   
   	if(menu == 'revisions' && (notes_flip_sub_menu_current_old != menu || notes_flip_sub_menu_current_old == '') )
   	{
   	
   	$('#home_content_centre_loading').slideDown('fast');
   	$('#revisions_items').html('');
   		url = "?i=load_note_history_list&ajax=1&note_uuid="+id;
   		$.getJSON(url, function(notes_histroy) {
   			
  
   			for ( var histroy in notes_histroy )
   			{
   			if(notes_histroy[histroy]['saved_date'] != undefined)
   			{
   			
   				var updated_from = notes_histroy[histroy]['updated_from'];
   			
	   			if(updated_from == 2)
	   			{
	   				var text = 'from iPhone/iPod touch';
	   			}
	   			else if(updated_from == 3)
	   			{
	   				var text = 'from API';
	   			}
	   			else
	   			{
	   				var text = 'from web';
	   			}
   
	   				$('#revisions_items').append('<a href="#notes/'+id+'/revisions/'+notes_histroy[histroy]['hid']+'" id="notes_history_menu_'+notes_histroy[histroy]['hid']+'"><div class="revisions_menu_icon  home_left_bar_icon_'+notes_histroy[histroy]['icon']+'"> </div><div class="revisions_menu_title">'+notes_histroy[histroy]['saved_date']+'</div><div class="revisions_menu_from">'+text+'</div></a>');

	   			if(hid == notes_histroy[histroy]['hid'])
	   			{
	   				$('#notes_history_menu_'+hid).attr('style','font-weight:bold;');
	   			}
	   			
	   			} 
	   			$('#note_center_'+menu+'_area').fadeIn("slow");
   			
   			}
   			
   		});
   		
   	}
   	$('#revisions_content_title').hide();
   	$('#revisions_content_text').hide();
   	$('#notes_history_menu_'+prev_hid).attr('style','');
   	if(hid != '' && hid != null)
   	{
   		url = "?i=load_note_history_item&ajax=1&hid="+hid;
   		$('#home_content_centre_loading').slideDown('fast');
   		
   		
   		$('#notes_history_menu_'+prev_hid).attr('style','');
   		$('#notes_history_menu_'+hid).attr('style','font-weight:bold;');
   		
   		$.getJSON(url, function(note_histroy) {
   			$('#revisions_content_text').html(note_histroy['text']);
   			$('#revisions_content_title').html(note_histroy['title']);
  		 	$('#revisions_content_title').attr('class', 'home_left_bar_icon_'+note_histroy['icon'])
  		 	
  		 	$('#revisions_content_title').fadeIn('slow');
  		 	$('#revisions_content_text').fadeIn('slow');
  		 	
  		 	hid_title = note_histroy['title'];
  		 	hid_text = note_histroy['text'];
  		 	hid_icon = note_histroy['icon'];
  		
   		});
   		
   		prev_hid = hid;
   		
   	}
   	else
   	{
   		prev_hid = '';
   	}
   	$('#home_content_centre_loading').slideUp('fast');
   	
   	
   	return false;
}

function checklist_item_swap(id, where)
{
	txt = $('#checklist-item-input-'+id).val();
	
	if(where == 1) //=tick
	{  
	 update_to = 0;
	  $('#checklist-item-input-ticked-'+id).val('1');
	 color = 'dark';
	 tick = '_tick';
	}
	
	if(where == 0)//=no tick
	{
	 update_to = 1;
	 $('#checklist-item-input-ticked-'+id).val('0');
	 color = 'light';
	 tick = '';
	}
	
	update = '<div class="checklists_item_checked_left checklists_item_checked_left_'+color+'" onclick="checklist_item_swap(\''+id+'\', '+update_to+');" > </div> <div class="checklists_item_checked_right checklists_item_checked_right_'+color+' handle"> </div><div class="checklists_item_checked checklists_item_checked_'+color+'"> <input onfocus="remove_title_no_color(\'checklist-item-input-'+id+'\');" id="checklist-item-input-'+id+'" type="text" name="item_text" value="'+txt+'" class="checklists_item_input" /></div>';
	$('#checklist-item-'+id).html(update);
	
}
function checklists_new_item()
{

	id_ran = randomString(4);
	id =  $('#id_').val()+'_'+id_ran;
	$('#checklist_items').append(','+id);
	$('#checklist_items_new').append(','+id);
	checklists_create_item(id, 'Untitled', 0);
	
	
	
	$("#checklist_input_outer").attr({ scrollTop: $("#checklist_input_outer").attr("scrollHeight") }); 
}

function checklists_create_item(id, text, ticked)
{
if(ticked == 1)
{
	 update_to = 0;
	 colour = 'dark';
	
}
else 
{	
	 update_to = 1;
	colour = 'light';
}
update = '<input id="checklist-item-input-ticked-'+id+'" type="hidden" value="'+ticked+'"  /><div id="checklist-item-'+id+'"><div class="checklists_item_checked_left checklists_item_checked_left_'+colour+'" onclick="checklist_item_swap(\''+id+'\', '+update_to+');" > </div> <div class="checklists_item_checked_right checklists_item_checked_right_'+colour+' handle"> </div><div class="checklists_item_checked checklists_item_checked_'+colour+'"> <input onfocus="remove_title_no_color(\'checklist-item-input-'+id+'\');" id="checklist-item-input-'+id+'" type="text" name="item_text" value="'+text+'" class="checklists_item_input" /></div></div>';
	$('#checklist').append(update);
	
	
}

function randomString(string_length) {

	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function toogle_edit_order()
{

	if($('#bottom_order').attr('class') != 'bottom_order_active_inactive')
	{
		if($('#bottom_order').attr('class') == 'bottom_order')
		{
			$('.home_left_grip').show('slow');
			$('#bottom_order').attr("class", 'bottom_order_on');
		}
		else
		{
			$('#bottom_order').attr("class", 'bottom_order');
			$('.home_left_grip').hide('slow');
		}
	}
}
function generate_notes_lists()
{

	var result = $('#home_left_bar_items').sortable('toArray');
	notes_nums=new Array();
	notes_ids=new Array();
	
	for (i=0;i<result.length;i++)
	{	
		var current = result[i].replace("-value", "");
		notes_nums[i+1]=current;
		notes_ids[current]=i+1;
	}

}
// Cookie functions below:
function Set_Cookie( name, value, expires, path, domain, secure )
{
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

