$(document).ready(function() {

	tabListeners();
	extendedTabListeners();
		
	// Append  + -  image to the box
	var minMaxImage = '<div class="minMaxImage minMaxImageMinus"></div>';
	$(".listbox h3").prepend(minMaxImage);
	
	//Check if listbox should be minimized
	$(".listbox").each(function() {
		if ($(this).attr('id') != undefined) {
			if ($.cookie('listbox' + $(this).attr('id')) == "closed") {
				$(this).children(".listboxContent").hide();
				$(this).children("h3").children(".minMaxImage").addClass("minMaxImagePlus");
				$(this).children("h3").children(".minMaxImage").removeClass("minMaxImageMinus");
			}
		}		
	})

	//Listener for click event on + - image
	//$(".minMaxImage").click(function() {
	$(".listbox h3").click(function() {
		//var box = $(this).parent().parent();
		var box = $(this).parent();
		var image = $(this).children('.minMaxImage');
		
		var content = $(box).children(".listboxContent");
		if ($(content).is(':visible') && $(image).is(".minMaxImageMinus")) {
			$(content).slideUp(100);
			$(image).addClass("minMaxImagePlus");
			$(image).removeClass("minMaxImageMinus");
			if ($(box).attr('id') != undefined) {
				$.cookie('listbox' + $(box).attr('id'), 'closed', { expires: 999, path: '/'});
			}
		} else if ($(content).is(':hidden') && $(image).is(".minMaxImagePlus")) {
			$(content).slideDown(100);
			$(image).addClass("minMaxImageMinus");
			$(image).removeClass("minMaxImagePlus");
			if ($(box).attr('id') != undefined) {
				$.cookie('listbox' + $(box).attr('id'), null);
			}
		}
	});

});

function extendedTabListeners() {
	$('.clickTab').click(function(e) {
		e.preventDefault();
		
		var tabID = $(this).attr('class');
		tabID = tabID.substr(0, tabID.indexOf('}'));
		tabID = tabID.substr(tabID.indexOf('{')+1, tabID.length);
		
		$('#'+tabID).find('.ajaxlink').click()
	});
}

function tabListeners() {
	// when user clicks on ajax tab
	//alert("tabListeners called");
	
	/*$(".boxTabs").children("li").children("span").each(function() {
		//alert($(this).attr("class"));
	});*/
	
	$(".boxTabs").children("li").children(".ajaxlink").each(function() {
		var txt = $(this).text();
		var className = $(this).attr("class");
		var parent = $(this).parent();
		$(this).remove();
		$(parent).append('<span class="'+className+'">'+txt+'</span>');
	});
	
	
	
	
	$(".boxTabs").children("li").children("span").click(function() {
		//alert("click called");
		//first parse the url and params
		var str = $(this).attr("class");
		var url = str.substr(str.indexOf("{")+1, str.indexOf("?")-str.indexOf("{")-1);
		var params = str.substr(str.indexOf("?")+1, str.indexOf("}")-str.indexOf("?")-1);
		
		//find the box to replace
		var box = $(this).parent().parent().parent();

		//remove the active tab and set new active
		$(box).children("ul").children("li").removeClass("active");
		$(this).parent().addClass("active");

		$(box).css("cursor", "wait");
		
		$.ajax({
			type: 'post',
			url: url,
			data: params,
			cache: false, 
			dataType: "html",
			success: function(data){
			
				// Fade out the content if the box has the 'fadeContent' class
				$(box).children('.fadeContent').children(':visible').fadeOut();
				
				if ($(box).find(".ajaxContent").length == 0) {
				
					var pager = ($(box).is(".pager"));
					
					$(box).children(".boxContent").html(data);
					$(box).children('.fadeContent').children(':hidden').fadeIn();
					
					if (pager) {
						//$(box).children('.boxContent').addClass("pager");
						initPager($(box).children('.boxContent'));
					}
					$(box).children(".boxContent").show();
				} else {
					$(box).find(".ajaxContent").html(data);
					if($(box).is('.fadeContent')) {
						$(box).find(".ajaxContent").children(':hidden').fadeIn();
					}
				}
				
				$(box).css("cursor", "default");
				extendedTabListeners();
				makeButtons();
				makeTooltip();
				resizeTextarea();
				if(window.initRating) {
					initRating();
				}
				if(window.makeRateReview) {
					makeRateReview();
				}
			}
		});	
	});

}
