var isFirstTabShown = false;

$(document).ready(function() {

	$("div.docTab").click(function() {
		var oids = $(this).attr("id").split("_");
		var type = oids[0];
		var oid  = oids[1];

		getDocuments(oid, type);
	});

	$("div.prodTab").click(function() {
		var oids = $(this).attr("id").split("_");
		var oid = oids[0];
		var tm = oids[1];
		var sm = oids[2];

		getProducts(oid, tm, sm);
	});

	$('div#slideShow').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '250px'
	});

	// Close document tabs that have no documents
	var isProductCategory = true;
	$("div.tab", $("#tabContainer")).each(function () {
		id = $(this).attr("id").split("_");

		// if product category
		if(id.length == 3)
			isProductCategory = false;
	});


	$("div.tab", $("#tabContainer")).each(function () {
		id = $(this).attr("id").split("_");
		type = id[0];
		oid = id[1];

		if(id.length == 2)
			getNrOfDocuments(oid, type, isProductCategory);
	});



});

function getProducts (oid, tm, sm) {
	$("div.tabActive").removeClass("tabActive");
	$(this).addClass("tabActive");
	$("div.content div#products").html("");
	$("div.content div#documents").html("");
	$("div.content div#loading").css("display", "block");

	var params = "oid=" + oid + "&tm=" + tm + "&sm=" + sm;
	getData('/actions/getProducts.jsp', params, listProducts, error);
}

function getDocuments(oid,type) {
	$("div.tabActive").removeClass("tabActive");
	$(this).addClass("tabActive");
	$("div.content div#documents").html("");
	$("div.content div#products").html("");
	$("div.content div#loading").css("display", "block");

	var params = "oid=" + oid + "&type=" + type;
	getData('/actions/getDocuments.jsp', params, listDocuments, error);
}

function getNrOfDocuments(oid, type) {
	$.ajax({
			url: "/actions/getNrOfDocuments.jsp",
			type: "POST",
			data: "oid=" + oid + "&type=" + type,
			dataType: "xml",
			success: checkIfTabIsContainingDocuments,
			error: error
		}
	);
}

function checkIfTabIsContainingDocuments(xml) {
	var element = $(xml).find("nrOfDocuments");
	value = parseInt(element.attr("value"));
	type = parseInt(element.attr("type"));
	oid = parseInt(element.attr("oid"));


	if(value == 0) {
			$("#" + type + "_" + oid).css("display", "none");
	} else {

			$("#" + type + "_" + oid).addClass("tabActive");
			if(!isProductCategory) {
				getDocuments(oid,type);
			}
			isFirstTabShown = true;

	}
}


function getData(urlPost, dataPost, successFunc, errorFunc) {
	$.ajax({
		  url: urlPost,
		  type: "POST",
		  data: dataPost,
		  dataType: "html",
		  success: successFunc,
		  error: errorFunc
	   }
	);
}

function listDocuments(html) {
	$("div.content div#loading").css("display", "none");
	$("div.content div#documents").html(html);
}

function listProducts(html) {
	$("div.content div#loading").css("display", "none");
	$("div.content div#products").html(html);
}

function error(html) {
	alert("Något gick fel.");
}
