var Assanka = Assanka || {};

Assanka.$(document).ready(function() {	

	// If the user has any widget preferences saved in cookies
	// use them to minimise/unminimise the appropriate widgets
	if (Assanka.$.cookie("assanka_minMaxState")) {
		var widgetstates = Assanka.$.cookie("assanka_minMaxState").split("|");
		for (j in widgetstates) {
			if (typeof(widgetstates[j]) == "string" && widgetstates[j].length) {

				var widgetstateparts = widgetstates[j].split(":");
				var el = Assanka.$("#"+widgetstateparts[0]);

				// The first widget in the list should never be minimised
				if (el.is(":first-child")) {
					Assanka.setMinimiseState(el, "open");
				
				// If the widget is not the first widget in the list, set
				// a new state based on the value in the cookie.
				} else {
					Assanka.setMinimiseState(el, widgetstateparts[1]);
				}
			}
		}
	}

	// In IE 6, remove the header bar and minmise button from the first widget
	// (in other browsers they are hidden by CSS)
	if (document.all && typeof document.body.style.maxHeight == "undefined") {
		Assanka.$("#rail-content li:first h2").css('borderTop', '0');
		Assanka.$("#rail-content li:first .widgetminimisebutton").hide();
	}
});


// Minimise / unminimise a widget and set a cookie to store the new state
Assanka.widgetminmax = {};
Assanka.toggleMinimiseWidget = function(el) {

	// Blur the link
	Assanka.$(el).blur();

	// Find the containing widget of the button (el) that was clicked
	var parentelement = Assanka.$(el).parents("li.widget");

	// Minimse state of the MPU cannot be changed
	var parentID = parentelement.attr("id");
	if (parentID.match(/ft-mpu-advert/)) return false;

	// Determine and set appropriate new minimisation state for the element
	var widgetcontent = Assanka.$(parentelement).find(".widgetcontent");
	var newstate = ((widgetcontent.css("display") == "none")?"open":"closed");
	Assanka.setMinimiseState(parentelement, newstate);

	// Build cookie string for storing new minimise state
	var cookiestring = "";
	for (i in Assanka.widgetminmax) {
		cookiestring += (i+":"+Assanka.widgetminmax[i]+"|");
	}

	// Set Cookie storing new minimise state (expires in 90 days)
	Assanka.$.cookie('assanka_minMaxState', cookiestring, {expires:90, path:'/'});
}

Assanka.setMinimiseState = function(el, newstate) {
	Assanka.widgetminmax[Assanka.$(el).attr("id")] = newstate;
	switch (newstate) {
		case "open":
			Assanka.$(el).removeClass("minimisestateclosed");
			Assanka.$(el).addClass("minimisestateopen");
			break;
		case "closed":
			Assanka.$(el).addClass("minimisestateclosed");
			Assanka.$(el).removeClass("minimisestateopen");
			break;
		default:
			// Do nothing
	}
}

// Display a light box
Assanka.showLightBox = function(content, elAnchor, classname, title, width) {

	// Set default classname
	if (typeof(classname) == "undefined") classname = "";

	// Set lightbox HTML
	var newel = Assanka.$('<div class="assankablogs assankalightbox '+classname+'" style="width:'+width+'px"><div class="assankalightboxinner"><div class=\'assankalightboxheader\'><span style=\'float:left;\'>'+title+'</span><a style=\'float:right;\' href="javascript:void(0)" onclick="Assanka.$(this).parents(\'.assankalightbox\').remove();" class=\'assankaLightBoxHideButton\'>Close</a></div>'+content+'</div></div>').hide();

	// Add lightbox to page
	Assanka.$("body").prepend(newel);

	// Set position of lightbox, so that the bottom of the lightbox lines up with the
	// bottom of the anchor element and is centered horizontally on the page
	// (has to be done after the lightbox is added to the page so that the width() and height() are nonzero)
	newel.css({"position":"absolute", "top":((parseInt(elAnchor.offset().top) - newel.outerHeight()) + elAnchor.outerHeight())+"px", "left":((Assanka.$(window).width() / 2) - (newel.width() / 2))+"px"});

	// Show lightbox
	Assanka.$(newel).show();
}

