
/* Load product on mouseover */
var ProductLoader = {
	
	productId: null,
	timer: null,
	timeout: 500,

	load: function() {
		var productId = this.productId;
		this.stop();
		document.location.href = "#productId=" + productId;
	},
	
	start: function(productId) {
		if(productId != this.productId) {
			this.stop();
			if(readCookie("productId") != productId) {
				this.productId = productId;
				this.timer = setTimeout("ProductLoader.load()", this.timeout);
			}
		}
	},
	
	stop: function(productId) {
		if(productId == this.productId) {
			this.productId = null;
			if(this.timer) {
				clearTimeout(this.timer);
			}
		}
	}
}

/* Update UI */
function closeCategory() {
	jQuery("#category").html("").animate({
		"border-width": 0,
		"left": 891,
		"width": 0
	});
}

function openCategory(contents) {
	jQuery("#category").html(contents);
	jQuery("#category").animate({
		"border-width": 1,
		"left": 167,
		"width": 722
	});
}

function updateCode() {
	if(jQuery("input[name=productCode]").length * jQuery("input[name=guessByCode]").length) {
		jQuery("input[name=guessByCode]").val(jQuery("input[name=productCode]").val());
	}
}

function updateTitle() {
	if(jQuery("input[name=productName]").length) {
		document.title = "Kerstpakkettenweb - " + decodeEntities(jQuery("input[name=productName]").val());
	} else if(jQuery("input[name=groupName]").length * jQuery("input[name=categoryName]").length) {
		document.title = "Kerstpakkettenweb - " + decodeEntities(jQuery("input[name=groupName]").val()) + ", " + decodeEntities(jQuery("input[name=categoryName]").val());
	} else if(jQuery("input[name=groupName]").length) {
		document.title = "Kerstpakkettenweb - " + decodeEntities(jQuery("input[name=groupName]").val());
	} else {
		document.title = "Kerstpakkettenweb";
	}
}

/* AJAX navigation */
function go(hash) {
	var page = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
	if(page == "") {
		page = ".";
	}
	document.location.href = page + jQuery.query.REMOVE("categoryId").REMOVE("groupId").REMOVE("productId") + hash;
	return false;
}

function goHome() {
	
	// slide panels
	jQuery("#categoryList").hide("slow");
	closeCategory();
	
	// load
	jQuery.get("start.php", {}, function(response) {
		jQuery("#start").html(response);
		updateTitle();
	});
	
	return false;
}

function guessProduct(productCode) {
	if(productCode) {
	
		// prefix zeros
		if(productCode.length < 2) {
			productCode = "0" + productCode;
		}
		
		// get corresponding product id
		if(jQuery("input[name=categoryId]").length) {
			jQuery.get("guessProduct.php", {productCode: productCode, categoryId: jQuery("input[name=categoryId]").val()}, guessedProduct);
		} else if(jQuery("input[name=groupId]").length) {
			jQuery.get("guessProduct.php", {productCode: productCode, groupId: jQuery("input[name=groupId]").val()}, guessedProduct);
		} else {
			jQuery.get("guessProduct.php", {productCode: productCode}, guessedProduct);
		}
	}
}

function guessedProduct(response) {
	if(response.match(/^-{0,1}\d*\.{0,1}\d+$/)) {
		go("#productId=" + response);
	} else {
		alert(response);
	}
}

function hashchange(hash) {
	if(hash.substring(0, 11) == "categoryId=") {
		loadCategory(hash.substring(11));
	} else if(hash.substring(0, 8) == "groupId=") {
		loadGroup(hash.substring(8));
	} else if(hash.substring(0, 10) == "productId=") {
		loadProduct(hash.substring(10));
	} else {
		goHome();
	}
}

function loadCategory(categoryId, productId) {
	
	// slide panels
	jQuery("#categoryList").hide("slow");
	
	if(productId) {
		
		// load by product id
		jQuery.get("category.php", {productId: productId}, function(response) {
			//jQuery("#category").html(response);
			openCategory(response);
			updateCode();
			updateTitle();
		});
		
	} else {
	
		// load by category id
		jQuery.get("category.php", {categoryId: categoryId}, function(response) {
			//jQuery("#category").html(response);
			openCategory(response);
			updateCode();
			updateTitle();
		});
	}
	
	return false;
}

function loadGroup(groupId) {
	
	// slide panels
	closeCategory();
	
	// load category list
	jQuery("#categoryList").show("slow");
	jQuery.get("categoryList.php", {groupId: groupId}, function(response) {
		jQuery("#categoryList").html(response);
		updateTitle();
	});
	
	jQuery.get("group.php", {groupId: groupId}, function(response) {
		jQuery("#start").html(response);
		updateTitle();
	});
	
	return false;
}

function loadInfoRequest() {
	jQuery.get("requestDialog.php", {type: "info"}, function(response) { jQuery("#requestDialog").html(response) }) ;
}

function loadProduct(productId) {
	
	// slide panels
	jQuery("#categoryList").hide("slow");
	openCategory();
	jQuery("#dealerLogo").animate({ left: "176px" });

	// load
	if(jQuery("#product").length) {
		jQuery.get("product.php", {productId: productId}, function(response) {
			jQuery("#product").html(response);
			updateCode();
			updateTitle();
			
			// if category has changed, reload category
			if(readCookie("categoryId") != jQuery("input[name=categoryId]").val()) {
				loadCategory(null, jQuery("input[name=productId]").val());
			}
		});
	} else {
		loadCategory(null, productId);
	}
	
	return false;
}

function loadTenderRequest() {
	jQuery.get("requestDialog.php", {type: "tender"}, function(response) { jQuery("#requestDialog").html(response) }) ;
}

/* Alter request */
function addInfoRequest(productId) {
	jQuery.get("requestAdd.php", {type: "info", productId: productId}, function(response) {
		if(response == "OK") {
			jQuery("#requestInfo").click();
		} else {
			alert(response);
		}
	});
}

function addTenderRequest(productId) {
	jQuery.get("requestAdd.php", {type: "tender", productId: productId}, function(response) {
		if(response == "OK") {
			jQuery("#requestTender").click();
		} else {
			alert("Fout opgetreden: " + response);
		}
	});
}

function deleteInfoRequest(productId, silent) {
	jQuery.post("requestDelete.php", {type: "info", productId: productId}, function(response) {
		if(response == "OK") {
			if(!silent) {
				loadInfoRequest();
			}
		} else {
			alert(response);
		}
	});
}

function deleteTenderRequest(productId, silent) {
	jQuery.post("requestDelete.php", {type: "tender", productId: productId}, function(response) {
		if(response == "OK") {
			if(!silent) {
				loadTenderRequest();
			}
		} else {
			alert(response);
		}
	});
}

/* Utility */
function decodeEntities(text) {
	return text.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/* Initialize hashchange handler */
jQuery(function() {

	// bind hashchange handler
	jQuery(window).bind("hashchange", function(e) {
		hashchange(jQuery.param.fragment());
	});

	// initial hashchange, if url includes hash
	var url = window.location.href;
	if(url.search(/[#]/) != -1) {
		jQuery(window).trigger("hashchange");
	}
});

jQuery(function() {
	jQuery("#requestInfo").fancybox({
		"margin" : 10,
		"onStart" : loadInfoRequest
	});
	jQuery("#requestTender").fancybox({
		"margin" : 10,
		"onStart" : loadTenderRequest
	});
});