
loader = {

	c: 	[],
	ci: [],

	attach: function (obj, id, params){
		loader.state = id+"|"+params;
		loader.obj = obj;
		if (loader.ci.indexOf(loader.state) > -1) return;
		var o = obj[0];
		var p = _findPos(o);
		var w = o.offsetWidth;
		var h = o.offsetHeight;
		$('body').prepend($('<div class="loader2"></div>').attr('id', id).css({ left: p[0]+'px', top: p[1]+"px", width: w+"px", height:h+'px', opacity: '0.75'}));
	},

	remove: function (id){
		$("#"+id).remove();
	},

	inCache: function (){
		var i = loader.ci.indexOf(loader.state);
		if (i > -1) {
			loader.obj.html(loader.c[i]);
			loader.obj.trigger('refresh');
			return true;
		} else {
			return false;
		}
	},

	inCacheXML: function (){
		var i = loader.ci.indexOf(loader.state);
		if (i > -1) {
			loader.obj.trigger('refresh', loader.c[i]);
			return true;
		} else {
			return false;
		}
	},

	updateCurrent: function(){
		var i = loader.ci.indexOf(loader.state);
		if (i != -1){
			loader.c[i] = loader.obj.html();
		}
	},

	uncache: function(){

		var i = loader.ci.indexOf(loader.state);
		if (i != -1){
			loader.c.splice(i,1);
			loader.ci.splice(i,1);
		}
	},

	uncacheAll: function(){
		var i = loader.ci.length;
		loader.c.splice(0,i);
		loader.ci.splice(0,i);
	},

	cache: function(id, params, data){

		var i = loader.ci.indexOf(id+"|"+params);
		if (i != -1){
			loader.c[i] = data;
		} else {
			loader.c.push(data);
			loader.ci.push(id+"|"+params);
		}
	}

};

help = {

	instance : 0,

	init : function(){

		$('div .help').each(function(){

			var h = $(this).html();

			$(this).html('?');
			$(this).attr('i', help.instance);

			$('body').append($('<div class="helpBox"></div>').css({ display: 'none' }).attr('id', 'help'+help.instance).append(h));

			$(this).bind('mouseenter', function(){

				var o = this;
				$(document).bind('scroll.help', function(){
					$(o).trigger('mouseleave');
				});

				var pos = _findPos(o);
				pos[1] += $(o).outerHeight()+3;
				$('#help'+$(o).attr('i')).each(function(){
					$(this).css({ position: 'absolute', left: pos[0] + 'px', top: pos[1] + 'px'}).css({ display : 'inline' });
				});
			}).bind('mouseleave', function(){
				$('#help'+$(this).attr('i')).each(function(){
					$(this).css({ display : 'none' });
				});
				$(document).unbind('scroll.help');
			});

			help.instance++;
		});

	}

}

confirmAction = {

	dialog : function(msg, url, data){

		if ($('#dialog').length > 0) return false;
		// dialog html frame
		var c = '<div class="title">We need you to confirm this action.</div>'+
				'<div class="confirmMsg">'+msg+'</div>'+
				'<center class="base">'+
					'<a href="#" ajax>Confirm</a>'+
				'</center>';

		// dialog parameters

		var div = $('<div></div>').dialog();
		$('body').append(div);
		dialog.load(c);
		$("#dialog").find('a').each(function(){

			if ($(this).attr('ajax') != undefined){
				$(this).removeAttr('ajax');
				$(this).bind("click", function(){
					$.post(url, data, function (d){
							confirmAction.ajaxResponse(d);
							dialog.remove();
						}, "xml");
					return false;
				});
			}
		});

		return false;
	},

	ajaxResponse : function (data){

		$(data).find('request').each(function(){

			if ($("#system_msg").children().length > 0){
				$("#system_msg").children().each(function(){
					$(this).remove();
				});
			}

			switch ($(this).find('code').text()){
				case "1": // success
					$("#system_msg").html('<div success>'+confirmAction.alert($(this).find('msg').text())+'</div>');
					break;
				case "2": // failure
					$("#system_msg").html('<div error>'+confirmAction.error($(this).find('msg').text())+'</div>');
					break;
			}

			jQuery.globalEval($(this).find('cmd').text());

		});

	},

	error : function (txt){

		return 	'<div class="ui-widget" style="margin-top:5px;">'+
				'<div class="ui-state-error ui-corner-all" style="padding: 5px 0.7em;">'+
				'<div><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>'+
				txt+
				'</div></div></div>';
	},

	alert : function (txt){

		return 	'<div class="ui-widget" style="margin-bottom:5px;">'+
				'<div class="ui-state-highlight  ui-corner-all" style="padding: 5px 0.7em;">'+
				'<div><span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span>'+
				txt+
				'</div></div></div>';
	}

}


$.fn.dialog = function(options){
	dialog.settings = jQuery.extend({
		pWidth : 500,
		pTop : 125,
		slide : 10
	}, options);

	dialog.open();
};

dialog = {

	open : function(data){

		// get some dimensions
		//var wH = $(window).height();
		//var wW = $(window).width();
		var dH = $(document).height();
		var dW = $(window).width();
		var wT = $(window).scrollTop();

		// attach overlay
		if ($('#overlay').length == 0){
			var overlay = $("<div></div>").attr('id', "overlay").css({ position: 'absolute', top : '0px', height : dH+'px', width : dW+'px' }).addClass('diaOverlay');
			$('body').append(overlay);
			$(window).resize(function(){
				dialog.resize();
			});
		}

		//attach dialog and animations
		var dM = dW/2;
		var mOffset = dialog.settings.pWidth/2;

		var c = $('<div id="close"></div>').addClass('closeReg').css({ position : 'relative', top : 15+'px', width : '20px' }).click(function(){
			dialog.remove();
		}).mouseenter(function(){ $(this).addClass('closeHover');})
		  .mouseleave(function(){ $(this).removeClass('closeHover'); });
			var d = $('<div></div>').addClass('dBox').addClass('diaLoader').css({ 'margin-right': (34)+'px' });
			var p = $('<div id="dialog"></div>').append(d).css({ top : (dialog.settings.pTop)+'px', left : (dM-mOffset-dialog.settings.slide)+'px', width : dialog.settings.pWidth+'px' });

		// IE doesn't like position:fixed... weaker work around
		if ($.browser == "msie" && $.browser.version < 7){
			p.css({ position : 'absolute' });
			$(window).scroll(function(){ dialog.scroll(); });
		} else { p.css({ position : 'fixed' }); }

		$('body').append(p);

		$('#dialog').css({ opacity:0 }).animate({
			opacity : 1,
			left : '+=' + dialog.settings.slide+'px'
		}, 300, 'linear', function(){ $('#dialog').prepend(c); });

	},

	load : function (data){
		var d = $('#dialog');
		d.find('.dBox').css({ background : '#fff' }).html(data);
		return d;
	},

	remove : function (){
		$('#close').remove();
		$('#dialog', top.document).animate({
			opacity : 0, left : '+=' + dialog.settings.slide+'px'},
			200, "linear", function(){
				$('#overlay').fadeOut(200, function(){
					$(this).remove();
				})
				$(this).remove();
		});
	},

	resize : function(){
		if ($('#overlay').length != 0){
			var dW = $(window).width();
			$('#overlay').css({ width : dW+'px' });
		}
	},

	scroll : function(){
		if ($('#dialog').length != 0){
			var wT = $(window).scrollTop();
			$('#dialog').css({ top : (wT+dialog.settings.pTop)+'px' });
		}
	}
};

/* Find an object's position on the screen. */
_findPos = function(obj) {
    while (obj && (obj.type == 'hidden' || obj.nodeType != 1)) {
        obj = obj.nextSibling;
    }

    var position = $(obj).offset();
    return [position.left, position.top];
};

$(document).ready(function () {
	help.init();
});

// IE is shit and does not support indexOf... lame
// thanks soledad penades
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}