/**
 * Copyright (c) 2008 Michael Manning (http://actingthemaggot.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 **/
jQuery.parseQuery = function(qs,options) {
	var q = (typeof qs === 'string'?qs:window.location.search), o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}}, options = (typeof qs === 'object' && typeof options === 'undefined')?qs:options, o = jQuery.extend({}, o, options), params = {};
	jQuery.each(q.match(/^\??(.*)$/)[1].split('&'),function(i,p){
		p = p.split('=');
		p[1] = o.f(p[1]);
		params[p[0]] = params[p[0]]?((params[p[0]] instanceof Array)?(params[p[0]].push(p[1]),params[p[0]]):[params[p[0]],p[1]]):p[1];
	});
	return params;
}

$(document).ready(function(){

	var prodTabs = $('#tabs').tabs();
	var prodTabsLength = prodTabs.tabs( 'length' );

	var qString = $.parseQuery();
	var selectTab = parseInt(qString.viewtab);
	var selectCat = qString.viewcat;

	// get a list of all of the cat(egory)-wrap(pers), we'll need it in a minute
	var catWraps = $('div.cat-wrapper').size();

	// make sure the selected tab index exists
	if( selectTab <= prodTabsLength-1 ) {
		// see if there might be something reasonable as the selected category
		if( selectCat.length > 2 ) {
			// check the select category id to see if it exists
			if( parseInt($("*").index($(selectCat))) > 0 ) {
				// it does not
				// alert("bad category: " + selectCat);
				$('div.cat-wrapper').css('display','block');
			} else {
				// it does
				// alert("good category: " + selectCat);
				// hide all of the cat-wraps
				$('div.cat-wrapper').css('display','none');
				// display the select cat-wrap
				$('div#' + selectCat).css('display','block');
			}
		}
		// that tab index exists, switch to it
		prodTabs.tabs('select', selectTab );
	}

	// some debug info
	$('#poof').html("<pre>" +
			"num tabs:   " + prodTabsLength + "\n" +
			"select tab: " + selectTab + "\n" +
			"caegories:  " + catWraps + "\n" +
			"select cat: " + selectCat + "\n" +
			"</pre>"
			);
}); // end document.ready

function showCategory(id) {
	if( id == "all" ) {
		$('div.cat-wrapper').css('display','block');
	} else {
		$('div.cat-wrapper').css('display','none');
		$('div#' + id).css('display','block');
	}
} // end showCategory

