function ShowTab(sender, element)
{
	hide_crop_api();
	
	if (sender !== null)
	{
		ResetTabsState();
		$(sender).addClass("selected");
	}

	var tabs = [
		"#tab1",
		"#tab2",
		"#tab3",
		"#tab4",
		"#tab5"
	];

	$.each(tabs, function(i){
		if ($(tabs[i]).is(":visible"))
			$(tabs[i]).hide();
	});

	$(element).fadeIn(300);
	SetHeight(element);

	if ($.toggle_right_tabs == true)
	{
		var right_tab = $(element + "-right");

		if (right_tab !== null)
			right_tab.show();
	}
}

function ResetTabsState(sender)
{
	if (sender == undefined)
	{
		$("li.tab1 a").removeClass("selected");
		$("li.tab2 a").removeClass("selected");
		$("li.tab3 a").removeClass("selected");
		$("li.tab4 a").removeClass("selected");
		$("li.tab5 a").removeClass("selected");

		if ($.toggle_right_tabs == true)
		{
			var tabs = [
				"#tab1-right",
				"#tab2-right",
				"#tab3-right",
				"#tab4-right",
				"#tab5-right"
			];

			$.each(tabs, function(i){
				if ($(tabs[i]) !== null)
					$(tabs[i]).hide();
			});
		}
	}
	else
	{
		$.ul = $(sender).parent().parent();

		$("#" + $.ul.get(0).id + " > li > a").each(function(){
			if ("#" + this.id !== sender)
				$("#" + this.id).removeClass("selected");
		});
	}
}


function SetHeight(tab)
{
	// - container (acelas height cu tab1, deoarece tab1 este afisat default)
	//		- slot-main
	//			- tabX (height fix)
	//		- slot right (height fix, deoarece fara mosteneste height 100% de la clasa)

	var a = parseInt($("div.container > div.slot-main > " + tab).height());
	var b = parseInt($("div.container > div.slot-right").height());
	var x = ( a >= b ? a : b );

	$("div.container").css("height", x.toString() + "px");
}

function CheckAndSwitchTo()
{
	var tab_rx		= /\?tab=/i;
	var index_rx	= /index.html/;
	var where		= document.location.href;

	if (where.match(tab_rx))
	{
		var items		= where.split(tab_rx);
		var tab_name	= "#tab" + items[1];
		var tab_btn		= "#tab" + items[1] + "-btn";

		if (where.match(index_rx))
			DisplayContent("#batchphoto-tab" + items[1] + "-btn", "#batchphoto-tab" + items[1], "#batchphoto");
		else
		{
			try
			{
				ResetTabsState();
				ShowTab(tab_btn, tab_name);
			}
			catch (ex) { }
		}
	}
}

$(document).ready(function(){
	if (crop["api"] != null)
	{
		$("#tab1-btn, #tab2-btn, #tab3-btn").click(function(){
			crop["api"].setOptions({hide: true});
			crop["api"].update();
		});
	}

	$("#filters-list-header").click(function(){
		hide_crop_api();
	});

	$("#filters-btn").click(function(){
		hide_crop_api();

		if ($("#filters-list").is(":hidden"))
		{
			$("#filters").css("height", "180px");
			$("#filters-list").fadeIn(100, function(){
				update_filter_holders();
			});
		}
		else
		{
			$("#filters").css("height", "60px");
			$("#filters-list").fadeOut(100, function(){
				update_filter_holders();
			});
		}
	});
});
