// SETUP
var oldsettings = new Array();
var settings = new Array();
var themes = new Array();
settings['theme'] = '';
settings['autosave'] = false;
var standardTheme = 'Casbarro';

var MindPostingVersion = '0.9 BETA';
// END SETUP

var siteFocussedForm = '';


var dragging_element = '';
var to_delete_element = '';
var editing = '';
var selected = '';

var mousex = 0;
var mousey = 0;

if(window['console'] === undefined)
	window.console = { log: function(){return;} };


function init()
{
	var check = 0;
	
	
	$("body").width($(window).width());
	$("body").height($(window).height());
	
	$(window).resize(function(){
		$("body").width($(window).width());
		$("body").height($(window).height());
	});
	$().mousemove(function(e){
		mousex = e.pageX;
		mousey = e.pageY;
		
		if(e.pageX > ($(window).width()-85) && e.pageY > ($(window).height()-85) && dragging_element != "")
		{
			to_delete_element = dragging_element;
			if(check == 0) {
				check = 1;
				$(".bin").fadeTo(100, 1);
			}
		}
		else
		{
			to_delete_element = "";
			if(check == 1) {
				check = 0;
				$(".bin").fadeTo(100, 0.3);
			}
		}
	}); 
	
	$(".info").fadeTo(1, 0, function(){ $(this).css("display", "inline"); });
	
	
	$(".add").fadeTo(1, 0, function(){ $(this).css("display", "inline"); $(this).fadeTo(1000, 0.3); });
	$(".add").mouseover(function() { $(this).fadeTo(100, 1); });
	$(".add").mouseout(function() { $(this).fadeTo(200, 0.3); });
	$(".add").click(function() {
		create();
	});
	addTooltip(".add", "Create a new Post-it!", "Try to press CTRL + N or DOUBLE-CLICK somewhere");
	
	
	
	$(".save").fadeTo(1, 0, function(){ $(this).css("display", "inline"); $(this).fadeTo(1000, 0.3); });
	$(".save").mouseover(function() { $(this).fadeTo(100, 1); });
	$(".save").mouseout(function() { $(this).fadeTo(200, 0.3); });
	$(".save").click(function() {
		save();
	});
	addTooltip(".save", "Save your wall!", "Try to press CTRL + S");
	
	
	
	$(".settings").fadeTo(1, 0, function(){ $(this).css("display", "inline"); $(this).fadeTo(1000, 0.3); });
	$(".settings").mouseover(function() { $(this).fadeTo(100, 1); });
	$(".settings").mouseout(function() { $(this).fadeTo(200, 0.3); });
	$(".settings").click(function() {
		showSettings();
	});
	addTooltip(".settings", "Settings", "Click here to open your settings window!");
	
	
	$(".logout").fadeTo(1, 0, function(){ $(this).css("display", "inline"); $(this).fadeTo(1000, 0.3); });
	$(".logout").mouseover(function() { $(this).fadeTo(100, 1); });
	$(".logout").mouseout(function() { $(this).fadeTo(200, 0.3); });
	$(".logout").click(function() {
		logout();
	});
	addTooltip(".logout", "Logout!", "Try to press CTRL + L");
	
	//$(".bin").fadeTo(1, 0, function(){ $(this).css("display", "inline"); $(this).fadeTo(1000, 0.3); });
	//$(".bin").bind("mouseenter", function() {  });
	//$(".bin").bind("mouseleave", function() { $(this).fadeTo(200, 0.3); });
	
	$(".bin").fadeTo(1, 0, function(){ $(this).css("display", "inline"); $(this).fadeTo(1000, 0.3); });
	
	$(".bin").mouseout(function() { $(this).fadeTo(200, 0.3); });
	$(".bin").click(function() { deletePostIt(); });
	
	addTooltip(".bin", "Delete!", "Drag a Post-it on it to delete it");
	
	addTooltip(".change", "Modifications!", "Press SAVE or CTRL + S to save the wall");
	
	$("body").bind("click", function(e) {
		if(!$(e.target).parent().attr('id') || $(e.target).parent().attr('id') != editing)
		{
			deactivateFields();
		}
		if($(e.target).attr('tagName') == "BODY") {
			unselect();
		}
	});
	$("body").bind("dblclick", function(e) {
		if($(e.target).attr('tagName') == "BODY")
		{
			var createx, createy;
			
			if(mousex > ($(window).width()-100))
			{
				createx = $(window).width()-200;
			}
			else
			{
				if(mousex < 100)
				{
					createx = 0;
				}
				else
				{
					createx = mousex-100;
				}
			}

			if(mousey > ($(window).height()-100))
			{
				createy = $(window).height()-200;
			}
			else
			{
				if(mousey < 100)
				{
					createy = 0;
				}
				else
				{
					createy = mousey-100;
				}
			}

			createx = (100*createx)/$(window).width();
			createy = (100*createy)/$(window).height();
			create(getRandomId(), createx+"%", createy+"%", 200, 200, "New Post-it", "", true, true);
			
		}
	});
	
}
 

function bindKeys()
{
	$(document).bind('keydown', 'Ctrl+s',function (evt){save(); return false; });
	$(document).bind('keydown', 'Ctrl+n',function (evt){create(); return false; });
	$(document).bind('keydown', 'Ctrl+t',function (evt){changeTheme(); return false; });
	$(document).bind('keydown', 'Del',function (evt){if(editing==''){ deletePostIt(); return false;} });
	$(document).bind('keydown', 'Ctrl+l',function (evt){logout(); return false; });
	$(document).bind('keydown', 'Ctrl+g',function (evt){setAlString(); return false; });
}
