// 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

$(document).ready(function() {

	var locked = false;
	var gfx = 2; //0-low,1-medium,2-high;
	var maxStack = 50;
	var icons = new Array();

	var icontheme = "";
	var panelskin = "";
	var panelicons = "";
	var background = "";
	var backlite = "";


	$.ajax({
	    url: "default_settings.xml",
	    dataType: "xml",
	    success: function(data){
		$(data).find("settings").each(function() {
			icontheme = $(this).find("icontheme").text();
			panelskin = $(this).find("panelskin").text();
			panelicons = $(this).find("panelicons").text();
			background = $(this).find("background").text();
			backlite = $(this).find("backlite").text();
			
		});
		$('#backtile').css("background", "url("+background+") repeat fixed");
		$('#backlite').css("background", "url("+backlite+") fixed repeat-x top");
		setupIcons();
	    }
	 });


	$('ul.menu').slideToggle('medium');
	$("ul.menu li:even").addClass("alt");
	$('#menu_link').click(function () {

		$('ul.menu').slideToggle('medium');

	});

	$("#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();
		}
	});

	$(window).bind('resize', function() { 
		$('.icon').remove();	
		iconGridAlign(icons);
		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 setupIcons(){
		$.ajax({
		    url: "icon.xml",
		    dataType: "xml",
		    success: function(data){
			var i = 0;
			var iconar = new Array();
			$(data).find("icon").each(function() {
				iconar[i]= new Icon($(this).find("x").text(), $(this).find("y").text(), icontheme + $(this).find("image").text(), $(this).find("title").text(),
				$(this).find("link").text());
				i++;
			});
			iconGridAlign(iconar);
			drgEnable();
			icons = iconar;
		    }
		 });
	}

	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-70;
		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);					
			}
			iconAppend(id, (col + 1) * wspace + col * iconw - lmod, row
					* hspace + (row - 1) * iconw - tmod, iconArray[i].iconimg,
					title, iconArray[i].link);
		}
	}

	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;
		}
	}

});
