/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008

	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};

	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &

	// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);

		var value = (pair.length==2)
		? decodeURIComponent(pair[1])
		: name;

		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}


function _containerBGs($offset) {
	
	if (($offset)) {
		hexBorders = $offset;
	}

	$('.container').each(function() {
		$(this).css('background', "#c0c0c0 url('http://teamklipz.demandx.com/gradient.svg?b=767676|e2e2e2|767676&f=000000&w="+($(this).width()+8)+"&h="+($(this).height()+8)+"&bo="+hexBorders+"')");
	});
	$('#topMarquee,#midNav,#midDescription,#botNav,.navSubItems').each(function() {
		$(this).css('background', "#c0c0c0 url('http://teamklipz.demandx.com/gradient.svg?b=767676|e2e2e2|767676&f=e0e0e0&w="+($(this).width()+8)+"&h="+($(this).height()+8)+"&bo="+hexBorders+"&fo="+hexBorders+"')");
	});
	$('#midShare').each(function() {
		$(this).css('background', "#c0c0c0 url('http://teamklipz.demandx.com/gradient.svg?b=767676|e2e2e2|767676&f=000000&p=2&w="+($(this).width()+8)+"&h="+($(this).height()+8)+"&bo="+hexBorders+"')");
	});
}

var tmrAdv;
var tmrSrch;
var hexBorders;

function advSearchHide() {
	$('#headSearch input').blur();
	$('#advSearch').fadeOut('slow');
}

function advSearchDo() {
	if ($('#advNone').length) {
		$('#advNone').remove();
	}
	$('#headSearch li').hide();
	$('#headSearch li[search*='+$('#headSearch input[name=q]').val().toLowerCase().replace(/ /g, '][search*=')+']').show();
	
	if (!$('#headSearch li:visible').length) {
		$('#headSearch ul').append('<li id="advNone">No results to display.</li>');
	}
}

$(document).ready(function() {
	_containerBGs();

	$('#midNav ul:first li:first-child a').parents('li').hover(function() {
		return false;
		
		$(this).find('.navSubItems:first').css('margin-left', $(this).width()+'px').show(10, function() {
			$(this).css('background-image', "url('http://teamklipz.demandx.com/gradient.svg?b=767676|e2e2e2|767676&f=e0e0e0&w="+($(this).width()+8)+"&h="+($(this).height()+8)+"&bo="+hexBorders+"&fo="+hexBorders+"')");
		});
	}, function() {
		$(this).children('.navSubItems').fadeOut('fast');
	});

	$('#midNav ul:first a').click(function() {
		if ($(this).attr('type')) {
			//$('.navSubItems').fadeOut('fast');
			//$('#headSearch input[name=q]').val($(this).text().replace(' »', '')).focus();
			$('#headSearch input[name=q]').focus();
			$('#advSearch ul').html('<li id="advNone">Retreiving results...</li>');
			$.get('_ajax.php?channel='+$(this).attr('channel')+
				($(this).attr('type')?'&type='+$(this).attr('type'):'')+
				($(this).attr('tag')?'&tag='+$(this).attr('tag'):'')+
				($(this).attr('title')?'&title='+$(this).attr('title'):''), function(data) {

					$('#advSearch ul').html('');
					if (data.results) {
						for (i in data.results) {
							$('#advSearch ul').append('<li search="'+data.results[i].title.toLowerCase()+'"><a href="'+data.results[i].link+'">'+data.results[i].title+'</a></li>');
						}
					}

					$('#headSearch input[name=q]').focus();
				}, 'json');

			return false;
		}
		if ($(this).attr('href') == '#') {
			return false;
		}
	});

	$('#headSearch input[name=q]').focus(function () {
		$('#advSearch').slideDown('fast');
		advSearchDo();
	}).keyup(function() {
		clearTimeout(tmrSrch);
		tmrSrch = setTimeout('advSearchDo()', 500);
	});

	$('#headSearch').mouseout(function() {
		clearTimeout(tmrAdv);
		tmrAdv = setTimeout('advSearchHide()', 100);
	});
	$('#headSearch,#advSearch').mouseover(function() {
		clearTimeout(tmrAdv);
	});

	$('#headSearch form').submit(function() {
		$('#advNone').remove();
		$('#headSearch li').hide();
		//$('#headSearch ul').append('<li id="advNone">This feature is currently under development.</li>');

		if (!$('#advSearch').is(':visible')) {
			$('#advSearch').slideDown('fast');
		}
		return false;
	});

	$('#headSearch li a').click(function() {
		$('#headSearch form').submit();
		return false;
	});
});
