// This is the global js script, register subscripts here

$(document).ready(function()
{
   	// comments.js
	if (self.checkRememberCookie)
		checkRememberCookie();
			
	// hide alert
	$('#alert').click(function(){$(this).slideUp()}).css('cursor','pointer');
	
	initHilite();
		
	// edit.js
	if (self.initEdit)
		initEdit();
	
	// form.js
	if (self.initForm)
		initForm();
	
	// custom, site specific functions
	if (self.initCustom)
		initCustom();
		

});

$(window).unload( function () {
	// comments.js
	if (self.setRememberCookie)
	setRememberCookie(); 
});

/* COOKIES */
var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (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=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();


function openClose(which)
{
	if (which.className == 'open')
		which.className = 'close';
	else
		which.className = 'open';
}

function initHilite()
{
	// Local search
	if(Cookies.fuel_search)
	{
		var terms = Cookies.fuel_search;
		Cookies.erase('fuel_search');
		
		hilite(terms);
		
		// If user followes results link, hilite terms there too
		$('a.search_result_link').click(function(){Cookies.create('fuel_search',terms,60);})
	}
	
	// Search engines
	if(document.referrer && (matches = document.referrer.match(/[qp]=([^&]*)/)))
	{
		hilite(matches[1]);
	}
	
	function hilite(str)
	{
		if(jQuery().highlight){
			$.each(str.split("+"), function(idx, val) { $('#main *').highlight(decodeURIComponent(val)); })
		}
	}
}
