var previous = 'tabWallpaper'; 

$(document).ready(function(){

	$(".tabDownloads").click(function () {
		var x = $(this).attr("name");
		//alert(x);
		$(".downloadSection").hide();
		$("#"+x).show();
	});
	
	//initial tab loaded is Wallpapers
	
	$(".tabDownloads").click(function(){
		if($(this).attr("src").indexOf("_on") == -1) {
			//replace previous tab with off state
			var oldSrc = $(".tabDownloads[id="+ previous +"]").attr("src").replace("_on.jpg","_off.jpg");
			$(".tabDownloads[id="+ previous +"]").attr("src",oldSrc); 
			
			//replace current tab with on state
			var newSrc = $(this).attr("src").replace("_off.jpg","_on.jpg");
			$(this).attr("src",newSrc); 
			
			//store the current tab as previous for next click
			previous = $(this).attr("id");
		}
	});
	
	// deeplinking
	var params = getParams();
	if(params.hash)
	{
		deeplinkTab(params.hash);
		if(params.hash != "wallpaper")
		{
			$("#wallpaper_more_btn").hide();
		}
	}
	
});


function deeplinkTab(tabToShow)
{
	$(".tabDownloads").each(function() {
		// set all tabs to off
		var oldSrc = $(this).attr("src").replace("_on.jpg","_off.jpg");
		$(this).attr("src",oldSrc); 	
		
		// tab to show
		if($(this).attr("name").toLowerCase().indexOf(tabToShow) != -1)
		{
			//replace current tab with on state
			var newSrc = $(this).attr("src").replace("_off.jpg","_on.jpg");
			$(this).attr("src",newSrc); 
			
			//store the current tab as previous for next click
			previous = $(this).attr("id");
		}
	});

	// show correct download section/div
	$(".downloadSection").each(function() {
		if($(this).attr("id").toLowerCase().indexOf(tabToShow) != -1)
		{
			$(this).show();
		}
		else
		{
			$(this).hide();
		}	
	});
}


function getParams(url)
{	
	var result = {};
	var queryString;
 
	if(url)
	{
		result.hash = url.substr(url.indexOf("#") + 1);
		queryString = url.substr(url.indexOf("?") + 1);	
	}
	else
	{
		result.hash = document.location.hash.substr(1);
		queryString = document.location.search.substr(1);
	}
 
	var params = queryString.split("&");			
	var curParam;
 
	for(var i in params)
	{
		curParam = params[i].split("=");
		result[unescape(curParam[0]).replace(" ", "")] = unescape(curParam[1]);
	}
 
	return result;		
}