﻿$(document).ready(function() {
	// External links
	$('a[@rel=external]').click(function(event) {
		var newWin = window.open(this.href, 'pop');
		event.preventDefault();
	});
	
	// Translate dropdown
	$('#translate').change(function() {
		window.location = $(this).val();
	});
	
	$("#form-search-right input.textfield").after("<a href=''></a>");
	
	$("#form-search-right a").click(function() {
		this.parentNode.submit();
		return false;
	});
});

function cleanLists() {
	$("ul").each(function() {
		for (var i = 0; i < this.childNodes.length; i++) {
			var node = this.childNodes[i];
			if (node.nodeType == 3 && !(/\S/).test(node.nodeValue))
				this.removeChild(node);
		}
		return this;
	});
}

function swapDefault() {
	$("input[@type=text]").each(function() {
		$(this).focus(function() {
			if(this.defaultValue && this.value == this.defaultValue) this.value = "";
		}).blur(function() {
			if(this.defaultValue && !this.value.length) this.value = this.defaultValue;
		});
	});
}

function searchLink() {
	$("#form-search input.textfield").after("<a href=''></a>");
	$("#form-search a").click(function() {
		this.parentNode.submit();
		return false;
	});
}

function dropNav() {
	// When the child UL is visible, change the class of the parent link.
	$("#nav ul ul").hover(
		//mouseover
		function() {
		$(this).parents("li").children("a").addClass("hover");
		//mouseout
		},function() {
		$(this).parents("li").children("a").removeClass("hover");
	});
	
	//Add the hover class to the child LIs to shift the left attribute.
	$("ul#top-level > li").hover(
		//mouseover
		function() {
		
			var offsetLeft		= parseFloat($(this).css("left"));
			var ulWidth			= parseFloat($(this).parents("ul").width());
			var liWidth			= parseFloat($(this).width());
			
			if (offsetLeft <= (ulWidth / 2) - 50) {
				switch (this.id) {
					case 'nav1' : // News
						$(this).children("ul").css("left", '7px');
						break;
					case 'nav2' : // Reviews
						$(this).children("ul").css("left", '2px');
						break;
					case 'nav3' : // Articles
						$(this).children("ul").css("left", '3px');
						break;
					case 'nav4' : // Backstage
						$(this).children("ul").css("left", '1px');
						break;
					default :
						$(this).children("ul").css("left", 0);
				}
				$(this).children("ul").css("width", (ulWidth - offsetLeft) + "px");
			} else {
				$(this).children("ul").css("left", -offsetLeft + "px");
				$(this).children("ul").css("textAlign", "right");
				$(this).children("ul").css("width", (ulWidth - 5) + "px");
				
				switch (this.id) {
					case 'nav6' : // Help
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 47) + "px 0");
						break;
					case 'nav7' : // Music
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 47) + "px 0");
						break;
					case 'nav8' : // Forums
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 63) + "px 0");
						break;
					case 'nav9' : // Galleries
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 59) + "px 0");
						break;
					case 'nav10' : // Software
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 72) + "px 0");
						break;
					case 'nav11' : // About Us
						$(this).children("ul").css("left", -offsetLeft-77 + "px");
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth + 4) + "px 0");
						break;
					case 'nav12' : // Members
						if ( ! logged_in) {
							$(this).children("ul").css("left", -offsetLeft-27 + "px");
							$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 45) + "px 0");
						} else {
							$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 72) + "px 0");
						}
						break;
					case 'nav13' : // Shop
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 47) + "px 0");
						break;
					default :
						$(this).children("ul").css("backgroundPosition", (offsetLeft + liWidth - 22) + "px 0");
				}
			};
		//mouseout
		},function() {
			$(this).children("ul").css("left", -9999);
	});
}

function toggleVisibility(el) {
	$(el).toggle();
}

// Expand and Collapse our sortable sections
function sectionToggle(name) {
	var title, lnk;
	
	$("div.showhide").addClass("clearfix").prepend('<a class="link-toggle" href=""></a>');
	$("div.showhide ul").each(function() {
		title	= $(this).siblings("h3").text();
		lnk		= $(this).siblings("a.link-toggle");
		if( !$(this).is(":hidden") ) {
			lnk.addClass("open");
			title	= "Collapse " + title;
		} else { title	= "Expand " + title; }
		lnk.attr("title", title);
	});
	
	$("a.link-toggle").click(function(event) {
		event.preventDefault();
		
		title	= $(this).siblings("h3").text();
		if(this.className.match("open")) {
			$(this).removeClass("open");
			title	= "Expand " + title;
		} else { 
			$(this).addClass("open");
			title	= "Collapse " + title;
		}
		
		$(this).attr("title", title);
		$(this).parents("div.showhide").children("ul").slideToggle("fast").queue(function() {
			serialize(name,'sort-sections'); 
			$(this).dequeue();
		});
		return false;
	});
}

function sectionSort(name) {
		$("#sort-sections").Sortable({
		accept: 'showhide',
		handle: 'h3',
		opacity: 0.7,
		tolerance: 'pointer',
		axis: 'vertically',
		onChange: function() { serialize(name,'sort-sections'); }
	});
}

function serialize(name,s)
{
	var serial 		= $.SortSerialize(s);
	var sortOrder	= serial.hash.replace(/sort-sections\[\]\=/g,'').split('&');
	var str			= '';
	var dt			= new Date();
	var exp			= dt.getTime() + (30*24*60*60*1000); // Expire 30 days from now
	
	dt.setTime(exp);
	
	$.each(sortOrder, function(i, el) {
		var state = $('#'+el).children('ul').css('display');
		if ( (state != 'none' && state != 'block') || state == 'none')
			state = '-';
		else
			state = '+';
		sortOrder[i] = el.replace(/-column/g,'') + state;
	});
	
	str = sortOrder.join('|');
		
	// Write cookie
	document.cookie = 'exp_'+name+'='+str+'; expires='+dt.toGMTString()+'; path=/; domain=.www.ilounge.com';
};

// Scripts used in all pages
function globalScripts() {
	cleanLists();
	dropNav();
	swapDefault();
	searchLink();
}

//Scripts used in Homepage only
function homeScripts() {
	sectionSort('sortorder');
	sectionToggle('sortorder');
}

function softwareScripts() {
	sectionSort('softwaresortorder');
	sectionToggle('softwaresortorder');
}