//debug.setLevel(0);

$(document).ready(function(){
// Prevent AJAX rushes ==================================
	window.requests = []
	$.ajaxSetup({
	  beforeSend: function(xhr){
		var new_request_list = [xhr]
		$.each(window.requests, function(k,v){
		  if(v.readyState != 4) new_request_list.push(v)
		})
		window.requests = new_request_list
	  }
	})

// Read the user's jQuery animation prefrence ============
	var value = readCookie("DisableFX");
	if(value == null){value = false;}
	
	if($.browser.msie){
		$.fx.off = false;
	}
	
	$("#_chkDisableFX").attr("checked", value);
	$.fx.off = value;

	$("#_chkDisableFX").click(function(){
		var value = $(this).is(":checked");
		
		$.fx.off = value;
		if(value){
			createCookie("DisableFX", value, 30);
		}else{
			eraseCookie("DisableFX");
		}
	});
// ------------------------------------
	Pulse("LOAD",{"url": document.URL});
});

// -------------------------------------
function Pulse(action, data){
		data = $.extend(true,data,{
			"ref": escape(document.referrer),
			"uri": escape(document.URL)
		});
		//debug.log("Pulse data");
		//debug.log(data);
	
	$.post("/include/AJAX/pulse.php",{
		"action"	: action,
		"data"		: data
	},function(){
		debug.log("beat.");
	});
}
// -------------------------------------
jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};
// ------------------------------------
// Cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// ************************************
// Loading overlay
jQuery.fn.LoadMsg = function(remove){
	if(remove === undefined){var remove = false;}
	var $this = $(this);
	var $Lwin = $this.children(".loading");

	if(remove){
		if($Lwin.length > 0){return this;}
		$this.append("<div class='loading'>&nbsp;</div>");
		var $loadWin = $this.children(".loading");
		$loadWin
			.unbind()
			.fadeTo("fast",0.4);
	}else{
		$Lwin.fadeOut("fast",function(){$Lwin.remove();});
	}
	return this;
};

// ************************************
// Lightbox

function Lightbox(lbContent){
	var $bod = $("body");
	
	var s = false;
	
	if(lbContent != ""){s = true;}
	if(lbContent === undefined){s = false;}
	if(lbContent === false){s = false;}

	if(s){
		$bod
		//	.height(wh)
			.css('overflow','hidden')
			.append("<div id='__lbo' class='overlay'></div>")
			.append("<div class='lbLightbox'>"+lbContent+"</div>");
			
		var $overlay = $("#__lbo.overlay");
		
		$overlay
		
		var $lb = $(".lbLightbox");
		var $win = $(window);
		var st = $win.scrollTop();

		$bod.height($win.height());
		
		$lb.children(".lbClose").click(function(){
			Lightbox(false);
		});
		
		$overlay
			.addClass("lbOverlay")
			.css({'top':st})
			.fadeTo("slow", 0.8, function(){
				$lb
					.css({
						'top': ($win.height()/2 - $lb.height()/2) + st,
						'left': $win.width()/2 - $lb.width()/2
					})
					.fadeIn("slow");		
			});
			
	}else{
		var $overlay = $("#__lbo.overlay");
		var $lb = $(".lbLightbox");
		
		$lb.fadeOut("slow", function(){
			$(this).html("&nbsp;");
			$overlay.fadeOut("slow", function(){
				$overlay.remove();
				$lb.remove();
			});
		});
		
		$bod.css('overflow','auto');
	}
}
