var BLURBS = "includes/blurbs.php";



$(document).ready(function()
{	
	/* Replace text on mail login on focus*/
	$.fn.search = function()
	{
		return this.focus(function()
		{
			if (this.value == this.defaultValue) 
			{
				this.value = "";
			}
		}).blur(function()
		{
			if (!this.value.length) 
			{
				this.value = this.defaultValue;
			}
		});
	};	
	$('.login_txt').search();
	$('#username').focus();
	
	
	/* set height of right pane so it matches left pane */
	evenColumns();
	window.onresize = function(){
		evenColumns();
	};
	
	
	/* load pages in modal */
	$('#modal').jqm({
		ajax: '@href',
		target: '.target',
		onShow: function(h){
			$(h.t).addClass("active");
			
			h.w.fadeIn(400);
			//used to fix the no-cursor bug on firefox
			if (!$.browser.msie) {
				/*$("#modal").css({
					position: "fixed",
					overflow: "expression('absolute')"
				});*/
			}
			DockMenu.disable();
		},
		onHide: function(h){

			h.w.fadeOut(400, function(){
				if (h.o) 
					h.o.remove();
				$(h.t).removeClass("active");
				
				DockMenu.enable();
			});
			
		}
	});
	
	$('#modal').jqmAddTrigger('a.modaltrigger');

});

$(window).load(function(){
		DockMenu.init("#dock", "#main_content", {
					overlap: true,
					distance: 120,
					baseline: -1,
					zIndex: 20,
					magnification: 1.27
				});
	
});

var DockMenu = function(){
	
	var in_progress = false;
	var current_action;
	var obj, cont, opts;
	
	
	return {
		showLoading: function()
		{
			$("#loading").show();
		},
		hideLoading: function()
		{
			$("#loading").hide();
		},
		enable: function(){
			obj.magnifierEnable();
		}, 
		disable: function(){
			obj.magnifierDisable();
		},
		init: function(o, c, op){
			var flag = false;
			var imgs = [];
			obj = $(o);
			cont = $(c);
			opts = op;
			$(window).resize(DockMenu.reset);
			
			$('a', obj).each(function(){
				var _action = this.href.substr(this.href.lastIndexOf('/') + 1).split('.')[0];
				var img = $('img', this).attr({
					action: _action,
					width: this.width
				});
				imgs.push(img);
			});
			
			if (imgs.length > 0) {
				obj.empty();
				
				$(imgs).each(function(){
					this.appendTo(obj);
					this.click(function(){
						var _id = $(this).attr("action");
						if (!in_progress && current_action != _id) {
							current_action = _id;
							in_progress = true;
							DockMenu.showLoading();
							DockMenu.disable();
							
							 $(this).fadeIn(100).animate({top:"-=20px"},400).animate({top:"+=20px"},200).animate({top:"-=20px"},125)
							.animate({top:"+=20px"},100, DockMenu.enable);
						
							
							var f = function(){
								$.get(BLURBS, {
									id: _id
								}, function(data){
									cont.slideDown("slow", DockMenu.hideLoading	);
									cont.html(data);
									in_progress = false;
									//attach modal events
									$('#modal').jqmAddTrigger('a.modaltrigger');
								});
							}
							
							cont.slideUp("slow", f);
						}
					});
				});
				obj.magnifier(opts);
				
			}
			
		}
	};
}();

function evenColumns()
{
	if ($('div.yui-gf div.first')[0]) 
	{
		var h = $('div.yui-gf div.first')[0].offsetHeight;
		$('#main_content').height(h - 30);
	}
}
