
var homePage = 'http://www.google.com/';
var frontMost = '';
var titleBarHeight = 50;
var cornerWidth = 24;
var bottomBar = 8;

$(function() {
	$("#applications").click( function() { showHide('appsMenu') } );
	$("#appsMenu").css("display","none");
	$("li").mouseover( function() { $(this).css("background-image","url(images/appsMenu/mouseOver.png)") } );
	$("li").mouseout( function() { $(this).css("background","#979596") } );
	$("#myDocuments").click( function() { openWindow('myDocsWindow', "apps/myDocuments/myDocuments.asp?folder=KenyaOS", 'My Documents', 'myDocuments.png', 500, 400) } );
	$("#addressBook").click( function() { openWindow('addressBookWindow', "apps/addressBook/addressBook.asp", 'Address Book', 'addressBook.png', 500, 400) } );
	$("#webBrowser").click( function() { openWindow('browserWindow', "apps/webBrowser/webBrowser.asp?url="+homePage, 'Web Browser', 'webBrowser.png', 900, 550) } );
	$("#rssReader").click( function() { openWindow('rssReaderWindow', "apps/rssReader/rssReader.asp", 'RSS Reader', 'rssReader.png', 700, 550) } );
	$("#notepad").click( function() { openWindow('notepadWindow', "apps/notepad/notepad.htm", 'Notepad', 'notepad.png', 400, 400) } );
	$("#calendar").click( function() { openWindow('calendarWindow', "apps/calendar/calendar.asp", 'Calendar', 'calendar.png', 268, 456) } );
	
	$("#inputSearch").change( function() { 
		openWindow('browserWindow', "apps/webBrowser/webBrowser.asp?url="+homePage+"search?q="+$(this).val(), 'Web Browser', 'webBrowser.png', 900, 550);
		$(this).val("")
	} );
});

function showHide(id) {
	if ($("#"+id).css("display")=="none") {
		$("#"+id).show();
		setFrontMost(id);
	} else
		$("#"+id).hide();
}

function openWindow(id, url, title, icon, width, height) {
	if ($("#"+id).length==0) {
		widthCorners=width-cornerWidth
		heightBottomBar=height+bottomBar
		heightTitleBar=height-titleBarHeight
		top = "<div class='topLeft handle'></div><div id='"+id+"_titleBar' class='titleBar handle'><div id='"+id+"_title' class='title'><img src='images/appsMenu/"+icon+"' />"+title+"</div><div id='"+id+"_resize' class='resize'></div><div id='"+id+"_close' class='close'></div></div><div class='topRight handle'></div>";
		middle = "<div id='"+id+"_contentArea'><div id='"+id+"_middleLeft' class='middleLeft handle'></div><div id='"+id+"_content' class='content'></div><div id='"+id+"_middleRight' class='middleRight handle'></div>";
		bottom = "<div class='bottomLeft handle'></div><div id='"+id+"_bottomBar' class='bottomBar handle'></div><div class='bottomRight handle'></div></div>";
		$("#desktop").append("<div id='"+id+"' class='window'>"+top+middle+bottom+"</div>");
		$("#"+id+"_close").click( function() { showHide(id); } );
		$("#"+id+"_resize").click( function() { 
			if ($("#"+id+"_contentArea").css("height") != titleBarHeight+"px") {
				$("#"+id+"_contentArea").css("height",titleBarHeight);
				showHide(id+"_contentArea");
			}else{
				$("#"+id+"_contentArea").css("height",(heightBottomBar));
				showHide(id+"_contentArea");
			}
		} );
		$("#"+id+"_titleBar").css("width",widthCorners); 
		$("#"+id+"_bottomBar").css("width",widthCorners); 
		$("#"+id+"_content").css("width",widthCorners);
		//$("#"+id).Draggable({handle: "#"+id+"_titleBar",containment : 'parent'});	
		$("#"+id).Draggable({handle: ".handle"});
		$("#"+id).css({width:width,height:(heightBottomBar)});
		$("#"+id+"_middleRight").css({height:heightTitleBar,top: titleBarHeight});
		$("#"+id+"_content").css({height: heightTitleBar,top: titleBarHeight}); 
		$("#"+id+"_middleLeft").css({height:heightTitleBar,top: titleBarHeight}); 
		$("#"+id).click( function() { setFrontMost(id) } );
		//setFrontMost(id);
		$("#"+id).css("z-index","101");
		$("#appsMenu").hide();
	} else {
		showHide(id);
	}
	loadContent(id+"_content", url);
}

function loadContent(id, url) {
	$("#"+id).load(url);
	$("#"+id).html("<img src='images/loading.gif' alt='Loading...' />");
}

function setFrontMost(id) {
	if (frontMost != id) {
		$("#"+frontMost).css("z-index","99"); 
		$("#"+id).css("z-index","100"); 
		frontMost = id;
	}
}

function deleteRecord(tb, id, divId, url) {
	$.post("apps/shared/delete.asp",
		  { tb: tb, id: id},
		  function(){
    			loadContent(divId, url);
  			}
		);
}


// Disable right click

function nrcIE(){
	if (document.all){return false;}
}
function nrcNS(e){
	if(document.layers||(document.getElementById&&!document.all)){ 
		if (e.which==2||e.which==3){
			return false;
		}
	}
} 
if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=nrcNS;
} else {
	document.onmouseup=nrcNS;document.oncontextmenu=nrcIE;
}
document.oncontextmenu=new Function("return false");