/*
 * jQuery doTimeout: Like setTimeout, but better! - v1.0 - 3/3/2010
 * http://benalman.com/projects/jquery-dotimeout-plugin/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */

var hostname = 'http://www.devforge.net/';

var uri = hostname + '/';

(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);

var head = document.getElementsByTagName("head")[0];

if (typeof jQuery == 'undefined') {
	var script = document.createElement('script');
	script.id = 'jQuery';
	script.type = 'text/javascript';
	script.src = uri + 'include/jquery/jquery-1.5.1.min.js';
	head.appendChild(script);

	var script = document.createElement('script');
	script.id = 'jQuery';
	script.type = 'text/javascript';
	script.src = uri + 'include/jquery/jquery-ui-1.8.11.custom.min.js';
	head.appendChild(script);

	var script = document.createElement('script');
	script.id = 'jQuery';
	script.type = 'text/javascript';
	script.src = uri + 'include/jquery/jquery.timers-1.2.js';
	head.appendChild(script);
}


$(document).ready(function() {

	var head = document.getElementsByTagName("head")[0];

	resizeviewport();
	$(window).resize(function() {
		resizeviewport();
	});

	$.getJSON(uri + 'applications/list', function(data) {
		var items = [];
		var count = 1;

		$.each(data, function(key, val) {
			switch(val.type) {
			case 'embed':
				$('#' + val.target).append('<li class="widget" id="widget-' + val.name + '" style="position: relative; text-align: left; background-color: inherit;"></li>');
				$('#widget-' + val.name).html('<div style="padding="10px"><img src="images/ajax-loader-large.gif" height="20px" width="20px" border="0"></div>');

				loadcontent(val);

				$('#widget-' + val.name).css({
					'text-align' : 'left'
				});

				break;
			case 'script':

				var script = document.createElement('script');
				script.id = 'script-' + val.name;
				script.type = 'text/javascript';
				script.src = val.url;
				head.appendChild(script);
				break;
			case 'widget':
				break;
			default:
				$('#' + val.target + ' > ul').append('<li id="widget-' + val.name + '"><a href="#">' + val.title + '</a></li>');
				$('#widget-' + val.name).css({
					'list-style' : 'none',
					'background-color' : 'inherit',
					'cursor' : 'pointer',
					'height' : '30px',
					'line-height' : '30px',
					'border-right' : '1px solid inherit',
					'padding-left' : '10px'
				});

				$('#widget-' + val.name + ' a').css({
					'text-decoration' : 'none',
					'color' : 'inherit'
				});

				$('#widget-' + val.name).mouseover(function(){
					$(this).css({
						'background-color' : 'maroon',
						'border-right' : '1px solid green',
						'color' : 'white'
					});
				}).mouseout(function(){
					$(this).css({
						'background-color' : 'inherit',
						'border-right' : '1px solid inherit',
						'color' : 'inherit'
					});
				});
			}
			count = count + 1;
		});
	});

	resizeviewport();
});

function loadcontent(val) { // source target title
// val.url, 'widget-' + val.name, val.title
	var target = 'widget-' + val.name;

	var headerclass = 'widget-header-fixed';
	if (val.draggable == 1) {
		headerclass = 'widget-header';
	}

	$.ajax({
		url: val.url,
		success: function(data){
			$('#' + target).html('');
			if (val.title && val.title != '') {
				$('#' + target).append('<div class="' + headerclass + '" id="' + target + '-header" style="line-height: 28px; background-color: #C0C0C0; margin-top: 10px;">' + val.title + '</div>');
			}
			else
			{
				if (val.draggable == 1) {
					var pos = $('#' + target).offset();
					$('#' + target).append('<div class="' + headerclass + '" id="' + target + '-header"></div>');
					$('#' + target + '-header').css({
						'position' : 'absolute',
						'display' : 'none',
						'top' : '0px',
						'left' : '0px',
						'width' : '100%',
						'background-color' : 'black',
						'min-height' : '30px',
						'line-height' : '30px',
						'z-index' : '999999',
						'opacity' : '0.6'
					});

					$('#' + target).mouseenter(function(){
						var widget = $(this);
						var id = $(this).attr('id');
						var pos = $(this).offset();
						$('#' + id + '-header').css({
							'display' : 'block'
						});
					}).mouseleave(function(){
						var id = $(this).attr('id');
						setTimeout(function(){
							$('#' + id + '-header').css({
								'display' : 'none'
							})
						}, 200 );
					});
				}
			}
			$('#' + target).append(data);
		}
	});
}

function resizeviewport() {
	$('.column').sortable({
		connectWith: '.column',
		handle: '.widget-header',
		placeholder: 'droppable-hover',
		helper: 'clone',
		opacity: '0.6',
		stop: function(event, ui) {
			var menu = ui.item.parent().attr('id');
			var count = 1;
			var data = '';
			$('#' + menu).find('>li').each(function(){
				var name = $(this).attr('id').replace(/widget-/, '');
				data = data + '{"order":"' + count + '","name":"' + name + '","target":"' + menu + '"},';
				count = count + 1;
			});

			if (count > 1) {
				data = '[' + data.replace(/,$/, '') + ']';
				$.ajax({
					type: 'POST',
					url: uri + 'applications/update/',
					dataType: 'html',
					data: 'data=' + data,
					success: function(sdata) {
//							alert(sdata);
					}
				});
			}
		}
	});
	$('.column').disableSelection();
}

