/***************************/
//@Author: Donald Yessick
//@website: www.dyessick.com
//@email: dyessick@gmail.com
/***************************/


//OS elements
var main = $("#main");
var taskbar = $("#taskbar");
var clock = $("#clock");
var trash = $("#trash");
var icons = $(".icon");
var std = $(".icon:not(.course, .doc, .page, .folder, .webfolder, .link, .pdf)");
var courses = $(".course");
var docs = $(".doc");
var pages = $(".page");
var folders = $(".folder, .webfolder");
var links = $(".link:not(.course, .doc, .page, .folder, .webfolder, .pdf)");
var pdfs = $(".pdf");
var img = 'images/'
var classIcon = img+'course.png';
var docIcon = img+'doc.png';
var linkIcon = img+'www.png';
var pageIcon = img+'page.png';
var folderIcon = img+'folder.png';
var webfolderIcon = img+'webfolder.png';
var pdfIcon = img+'pdf.png';


//Mouse status
var mouseDiffY = 0;
var mouseDiffX = 0;
var mouseActiveIcon = 0;
var mouseActiveCloneIcon = 0;

//update clock function
function updateClock(){
	var now = new Date();
	var hour = now.getHours();
	if(hour < 10) hour = "0" + hour;
    var mins = now.getMinutes();
	if(mins < 10) mins = "0" + mins;
    var secs = now.getSeconds();
	if(secs < 10) secs = "0" + secs;
	//print the current time in the clock division
	clock.html(hour + " : " + mins + " : " + secs);
	//recursive call
    setTimeout("updateClock()", 1000);
}
$(document).ready(function(){
	//cancel context menu
	$(document).bind("contextmenu",function(e){
		return false;
	});
	
	//show icons
	icons.each(
		function ( i ){
			var src = img+$(this).attr('id')+'.png';
			if ($(this).hasClass('custom')){
				$(this).css('backgroundImage','url(' + src +')');
			}
			else if ($(this).hasClass('course')){
				$(this).css('backgroundImage','url(' + classIcon +')');
			}
			else if ($(this).hasClass('doc')){
				$(this).css('backgroundImage','url(' + docIcon +')');
			}
			else if ($(this).hasClass('page')){
				$(this).css('backgroundImage','url(' + pageIcon +')');
			}
			else if ($(this).hasClass('pdf')){
				$(this).css('backgroundImage','url(' + pdfIcon +')');
			}
			else if ($(this).hasClass('folder')){
				$(this).css('backgroundImage','url(' + folderIcon +')');
			}
			else if ($(this).hasClass('webfolder')){
				$(this).css('backgroundImage','url(' + webfolderIcon +')');
			} 
			else if ($(this).hasClass('link')){
				$(this).css('backgroundImage','url(' + linkIcon +')');
			}
			else {
				$(this).css('backgroundImage','url(' + src +')');
			}
		});

	std.each(
		function ( i ){
			$(this).css("left", 32).css("top", 32+i*80);
		});
	courses.each(
		function ( i ){
			$(this).css("left", 132).css("top", 32+i*80);

		});
	docs.each(
		function ( i ){
			$(this).css("left", 200).css("top", 32+i*80);

		});
	pages.each(
		function ( i ){
			$(this).css("left", 270).css("top", 32+i*80);

		});
	folders.each(
		function ( i ){
			$(this).css("left", 340).css("top", 32+i*80);

		});
	links.each(
		function ( i ){
			$(this).css("left", 410).css("top", 32+i*80);

		});

	pdfs.each(
		function ( i ){
			$(this).css("left", 480).css("top", 32+i*80);

		});

	trash.css({'top':(main.height()) - (96 + taskbar.height()), 'left':main.width() - 96 });

	icons.fadeIn(1500);
	taskbar.slideDown();
	
	//show current time
	updateClock();
	
	//mouse click
	icons.mousedown(function(e){
		//only accepts left click; all navs uses 0 but IE uses 1 lol...
		if(e.button <= 1){
			//calculate differences when user clicks the icon
			mouseDiffY = e.pageY - this.offsetTop;
			mouseDiffX = e.pageX - this.offsetLeft;
			if(mouseActiveIcon !=0){
				mouseActiveIcon.removeClass("active");
			}
			mouseActiveIcon = $(this);
			mouseActiveCloneIcon = mouseActiveIcon.clone(false).insertBefore(mouseActiveIcon);
		}
	});
	
	//moving mouse
	$(document).mousemove(function(e){
		if(mouseActiveIcon){
			//update position
			mouseActiveIcon.css({"top":e.pageY - mouseDiffY, "left":e.pageX - mouseDiffX, "opacity":0.35});
			var restaY = e.pageY - $(this).css("top");
			var restaX = e.pageX - $(this).css("left");
		}
	});
	
	//release mouse click
	$(document).mouseup(function(){
		if(mouseActiveIcon != 0){
			mouseActiveIcon.css({"opacity":1.0});
			mouseActiveIcon = 0;
			mouseActiveCloneIcon.remove();
			mouseActiveCloneIcon = 0;
		}
	});
	
	//mouse double click
	icons.dblclick(function(){
		var folder= $(this).attr('id');
		var docloc= $(this).attr('id')+'.doc';
		var href= $(this).attr('id')+'.html';
		var pdf= $(this).attr('id')+'.pdf';

		if ($(this).hasClass('link'))
			document.location.href = $(this).attr('title');
		else
		if ($(this).hasClass('doc'))
			document.location.href = docloc;
		else
		if ($(this).hasClass('page'))
			document.location.href = href;
		else
		if ($(this).hasClass('pdf'))
			document.location.href = pdf;
		else
		if ( ($(this).hasClass('course'))
		|| ($(this).hasClass('folder'))
		|| ($(this).hasClass('webfolder'))
		)
			document.location.href = folder;
		else if ($(this).attr('title'))
			document.location.href = $(this).attr('title');
		else
			alert(this.id);
	});

	
	//custom context menu on right click
	main.mousedown(function(e){
		if(e.button == 2){
			alert("context menu");
		}
	});

});
