// HTML/JS/AJAX CLIENT SIDE in jQuery framework is made by Nikola Stamatovic as a part of web desktop enviroment called SILK WDE for project http://ivarize.com

$(function() {

	var locked = false;
	var gfx = 2; //0-low,1-medium,2-high;
	var maxStack = 50;
	var icons="img/icons/"

	var iconar = new Array();
	iconar[0] = new Icon(0, 0, icons+"google.png", "Google",
			"http://www.google.com/profiles/110891834354805146613");
	iconar[1] = new Icon(0, 0, icons+"facebook.png", "Facebook",
			"http://www.facebook.com/theStamat");
	iconar[2] = new Icon(0, 0, icons+"yahoo.png", "Yahoo",
			"http://profiles.yahoo.com/u/2HNOEDLT2GXH4ONMUKZTPUDETY");
	iconar[3] = new Icon(0, 0, icons+"deviantart.png", "Deviant Art",
			"http://thestamat.deviantart.com/");
	iconar[4] = new Icon(0, 0, icons+"wordpress.png", "Wordpress",
			"http://stamat.info/blog");
	iconar[5] = new Icon(0, 0, icons+"lastfm.png", "LastFm",
			"http://www.lastfm.se/user/theStamat");
	iconar[6] = new Icon(0, 0, icons+"twitter.png", "Twitter",
			"http://twitter.com/theStamat");
	iconar[7] = new Icon(0, 0, icons+"flickr.png", "Flickr",
			"http://www.flickr.com/photos/34101718@N08/");
	iconar[8] = new Icon(0, 0, icons+"linkedin.png", "LinkedIn",
			"http://www.linkedin.com/in/stamat");
	iconar[9] = new Icon(0, 0, icons+"myspace.png", "MySpace",
			"http://www.myspace.com/stamat64");
	iconar[10] = new Icon(0, 0, icons+"blogger.png", "Blogger",
			"http://n-stamat.blogspot.com");
	iconar[11] = new Icon(0, 0, icons+"youtube.png", "YouTube",
			"http://www.youtube.com/user/Stamatron");
	iconar[12] = new Icon(0, 0, icons+"stumbleupon.png", "StumbleUpon",
			"http://www.stumbleupon.com/stumbler/Stamat/");
	iconar[13] = new Icon(0, 0, icons+"delicious.png", "Delicious",
			"http://delicious.com/theStamat");
	iconar[14] = new Icon(0, 0, icons+"feed.png", "Blog Feed",
			"http://stamat.info/blog/?feed=rss2");
	iconar[15] = new Icon(0, 0, icons+"lastfm.png", "My Band",
			"http://www.last.fm/music/Zadnja");
	iconGridAlign(iconar);

	drgEnable();

	$("#lock").bind("click", function(e) {
		if (locked == false) {
			$(this).css("background", "url(img/lock.png)");
			$("#hoverinfo p").text("Unlock desktop");
			locked = true;
			$(".icon").draggable('destroy');
			$(".swidget").draggable('destroy');
			$(".cwidget").draggable('destroy');
		} else {
			$(this).css("background", "url(img/unlock.png)");
			$("#hoverinfo p").text("Lock desktop");
			locked = false;
			drgEnable();
		}
	});
			
	$('#music').dialog({
		autoOpen: false,
		resizable: false,
		width: 275,
		height:466
	});
	$('#music_link').click(function(){
		$('#music').dialog('open');
		return false;
	});
	
	$('#about').dialog({
		autoOpen: false,
		width: 585,
		height:476
	});
	$('#about_link').click(function(){
		$('#about').dialog('open');
		return false;
	});
	
	$('#aboutme').dialog({
		autoOpen: false,
		width: 585,
		height:476
	});
	$('#aboutme_link').click(function(){
		$('#aboutme').dialog('open');
		return false;
	});
	
	$('#debug').dialog({
		autoOpen: false,
		resizable: false,
		width: 400
	});
	$('#debug_link').click(function(){
		$('#debug').dialog('open');
		return false;
	});
	
	$('#contact').dialog({
		autoOpen: false,
		width: 320,
		height:400
	});
	$('#contact_link').click(function(){
		$('#contact').dialog('open');
		return false;
	});
	
	$('#guestbook').dialog({
		autoOpen: false,
		width: 400,
		height:500
	});
	$('#guestbook_link').click(function(){
		$('#guestbook').dialog('open');
		return false;
	});

	$(".fadeanim").hover(function() {
		$(this).animate( {
			"opacity" : "0.60"
		}, "slow");
	}, function() {
		$(this).animate( {
			"opacity" : "1"
		}, "slow");
	});
	
	$(".navlink img").hover(function() {
		$(this).animate( {marginTop : "0px",width:"32px",height:"32px"}, 200);
		
	}, function() {
		$(this).animate( {marginTop : "2px", width:"26px",height:"26px"}, 200);
	});

	$(".cwidget").hover(function() {
		if (locked == false) {
			$(this).css("z-index", maxStack);
			$(".whandle", this).css("visibility", "visible");
		}
	}, function() {
		if (locked == false) {
			$(".whandle", this).css("visibility", "hidden");
		}
	});

	function drgEnable() {
		$('.swidget').draggable( {
			stack : {
				group : '.widget',
				min : maxStack
			},
			iframeFix : true,
			containment : '#content',
			cursor: 'crosshair',
			opacity : 0.45,
			scroll : false
		});

		$('.cwidget').draggable( {
			stack : {
				group : '.widget',
				min : maxStack
			},
			iframeFix : true,
			containment : '#content',
			handle : '.whandle',
			cursor: 'crosshair',
			opacity : 0.45,
			scroll : false
		});

		$('.icon').draggable( {
			stack : {
				group : '.icon',
				min : 50
			},
			containment : '#content',
			cursor: 'pointer',
			opacity : 0.45,
			scroll : false
		});
		
		if(ifIE()==true){
			$('.icon').draggable('option', 'opacity', 0);
		}
	}

	function iconGridAlign(iconArray) {
		
		  var rWidth = 0;
		  var rHeight = 0;
		  
		  if( typeof( window.innerWidth ) == 'number' ) {
		    rWidth = window.innerWidth;
		    rHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    rWidth = document.documentElement.clientWidth;
		    rHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    rWidth = document.body.clientWidth;
		    rHeight = document.body.clientHeight;
		  }

		var iconw = 64;
		var iconh = 80;
		var winw = rWidth;
		var winh = rHeight-60;
		var hspace = 20;
		var wspace = 20;
		var tmod = 0;
		var lmod = 0;
		var col = 0;
		var row = 0;
		for ( var i = 0; i < iconArray.length; i++) {
			var id = "i" + i;
			row += 1;
			var hcalc = row * hspace + (row - 1) * iconw - tmod;
			var title = iconArray[i].title;
			if (hcalc > winh - iconw) {
				row = 1;
				col += 1;
			}
			var ido = document.getElementById(id);
			if (title.length > 10) {
				title = iconTitleParser(title);
			//	alert(title);
			//	$("p",'#i5').css({
			//		"color":"#ff0000",
			//		"width":"100px",
			//		"margin-left":"-18px"
			//	});					
			}
			iconAppend(id, (col + 1) * wspace + col * iconw - lmod, row
					* hspace + (row - 1) * iconw - tmod, iconArray[i].iconimg,
					title, iconArray[i].link);
			//iconHoverInit(ido,title);
		}
	}

	function iconTitleParser(iconTitle) {
		var title = iconTitle;
		var t = "";
		var separator = "<br/>";
		var count = 0;
		for (i = 0; i < title.length; i++) {
			var c = title.charAt(i);
			if (c == ' ') {
				count = 0;
			}
			count += 1;
			t += c;
			if (count == 11) {
				count = 0;
				t += separator;
			}

		}
		return t;
	}

	function iconInit(iconArray) {
		for ( var i = 0; i < iconArray.length; i++) {
			var id = "i" + i;
			iconArray[i].title
			iconAppend(id, iconArray[i].x, iconArray[i].y,
					iconArray[i].iconimg, iconArray[i].title, iconArray[i].link);
		}
	}

	function linkAssign(obj, url) {
		$(obj).bind("dblclick", function() {
			window.open(url);
		});
	}

	function iconAppend(id, x, y, iconimg, title, link) {
		$("#content")
				.append(
						"<div id=\""
								+ id
								+ "\" class=\"icon\" style=\"position:absolute; left:"
								+ x
								+ "px; top:"
								+ y
								+ "px;\"><div class=\"iconimg\" style=\"background:url("
								+ iconimg
								+ ") no-repeat center center;\"></div><p>"
								+ title + "</p></div>");
		linkAssign("#" + id, link);
	}

	function Icon(x, y, iconimg, title, link) {
		this.x = x;
		this.y = y;
		this.iconimg = iconimg;
		this.title = title;
		this.link = link;
	}
	
	function ifIE(){
		if(document.all){
			return true;
		}else{
			return false;
		}
	}

});