function openwindow(url, width, height)
{
	var newwindow=window.open(url,'fullSize','height='+(parseInt(height)+20)+',width='+(parseInt(width)+20));
	if (window.focus) {newwindow.focus()}
}

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

var i18n = new Object();

i18n.strings = {
	addToCart: {
		fi: 'lis&auml;&auml; ostoskoriin <img src="img/site/orangebullet.gif" />',
		en: 'add to cart <img src="img/site/orangebullet.gif" />'
	},
	removeFromCart: {
		fi: 'poista ostoskorista <img src="img/site/graybullet.gif" />',
		en: 'remove from cart <img src="img/site/graybullet.gif" />'
	},
	page: {
		fi: 'sivu',
		en: 'page'
	}
}

i18n.getString = function(name) {
	var locale;
	if(typeof(this.locale) == "undefined") {
		locale = "fi"; //default!
	} else {
		locale = this.locale
	}
	if(i18n.strings[name] == null)
		return null;
	if(i18n.strings[name][locale] == null)
		return i18n.strings[name]["fi"]; //default
	
	return i18n.strings[name][locale];
}

i18n.setLocale = function(locale) {
	this.locale = locale;
}

$.fn.selectedIndex = function(a) {
	return this.each(function(){
		this.selectedIndex = a;
	});
}

function getHash(name) {
	var hash = window.location.hash.toString();
	if(hash.length == 0)
		return null;
	var re = new RegExp(name + "=([^;]*);");
	var result = hash.match(re);
	
	if(result == null)
		return null;
	return result[1];
}

function setHash(name, value) {
	var hash = window.location.hash.toString();
	if(hash.length == 0) {
		hash = "#";
	}
	var curVal = getHash(name);
	if(curVal != null) {
		var re = new RegExp("(" + name + "=)" + curVal);
		hash = hash.replace(re, "$1" + value);
	} else {
		hash = hash + name + "=" + value + ";";
	}
	window.location.hash = hash;
}

Category = {
	init: function(catId, url, sort, listview, page, itemsPerPage, itemsPerListPage, itemsAmount) {
		if(sort == null)
			Category.sort = "name";
		else
			Category.sort = sort;
		if(listview == null)
			Category.list = false;
		else
			Category.list = listview;

		Category.itemsAmount = itemsAmount;
		Category.itemsPerPage = itemsPerPage;
		Category.itemsPerListPage = itemsPerListPage;
		
		Category.page = page;
		Category.index = 0;
		Category.id = catId;
		Category.url = url;
		
		//attach event handlers
		$("#orderAlpha").click(Category.orderAlphaHandler);
		$("#orderNumeric").click(Category.orderNumericHandler);
		$("#orderDate").click(Category.orderDateHandler);
		
		$("#showList").click(Category.showListHandler);
		$("#showSeparate").click(Category.showSeparateHandler);
		
		$("#items").find("a.itemlink").click(Category.loadItemHandler);
		
		$("#alphanavigation").find("a").click(Category.findLetterHandler);
		
		Category.initPageBrowse();
	},
	getPages: function() {
		if(Category.list) {
			var rest = ((Category.itemsAmount % Category.itemsPerListPage) != 0)? 1 : 0;
			return ((Category.itemsAmount - (Category.itemsAmount % Category.itemsPerListPage)) / Category.itemsPerListPage) + rest;
		} else {
			var rest = ((Category.itemsAmount % Category.itemsPerPage) != 0)? 1 : 0;
			return ((Category.itemsAmount - (Category.itemsAmount % Category.itemsPerPage)) / Category.itemsPerPage) + rest;
		}
	},
	makeActive: function(id) {
		$("#" + id).find("a.active").removeClass("active");
		$(this).addClass("active");
	},
	orderAlphaHandler: function() {
		if(Category.sort != "name") {
			Category.makeActive.call(this, "order");
			Category.sort = "name";
			Category.setPage(1);
			$("#alphanavigation").show();
		}
		return false;
	},
	orderNumericHandler: function() {
		if(Category.sort != "number") {
			Category.makeActive.call(this, "order");
			Category.sort = "number";
			$("#alphanavigation").hide();
			Category.setPage(1);
		}
		return false;
	},
	orderDateHandler: function() {
		if(Category.sort != "date") {
			Category.makeActive.call(this, "order");
			Category.sort = "date";
			$("#alphanavigation").hide();
			Category.setPage(1);
		}
		return false;
	},
	showListHandler: function() {
		if(!Category.list) {
			Category.makeActive.call(this, "view");
			Category.page = ((Category.index - (Category.index % Category.itemsPerListPage)) / Category.itemsPerListPage) + 1;
			Category.list = true;
			Category.initDropdown();
			Category.load();
		}
		return false;
	},
	showSeparateHandler: function() {
		if(Category.list) {
			Category.makeActive.call(this, "view");
			Category.page = ((Category.index - (Category.index % Category.itemsPerPage)) / Category.itemsPerPage) + 1;
			Category.list = false;
			Category.initDropdown();
			Category.load();
		}
		return false;
	},
	goToLetterHandler: function() {
		
	},
	loadItemHandler: function() {
		Item.itemsPerPage = (Category.list)? Category.itemsPerListPage : Category.itemsPerPage;
		Item.index = parseInt(this.id.substr(3));
		Category.hide();
		Item.load(this.href);
		return false;
	},
	findLetterHandler: function() {
		try {
		var index;
		var letter = this.href.toString().match(/letter=([a-z])/)[1];
		browseDwr.findIndexByLetter(letter, Category.id, {
			async: false,
			callback: function(data) { index = data; }
		});
		
		var ipp = (Category.list)? Category.itemsPerListPage : Category.itemsPerPage;
		var page = ((index - (index % ipp)) / ipp) + 1;
		Category.setPage(page);
		} catch (e) { alert(e); }
		return false;
	},
	initPageBrowse: function() {
		$("select.pagedropdown").selectedIndex(Category.page - 1).show().change(Category.dropDownHandler);
		this.next = $("a.nextpage").click(Category.nextPageHandler);
		this.prev = $("a.prevpage").click(Category.prevPageHandler);
	},
	initDropdown: function() {
		$("select.pagedropdown").each(function() {
			var pages = Category.getPages();
			var current = this.childNodes.length;
			if(current == pages) {
				return;
			}
			
			if(pages < current) {
				var diff = current - pages;
				for(var i = 0; i < diff; i++) {
					this.removeChild(this.lastChild);
				}
			} else {
				for(var i = current + 1; i <= pages; i++) {
					var element = document.createElement("option");
					element.value = i;
					element.innerHTML = i;
					$(this).append(element);
				}
			}
		});
	},
	dropDownHandler: function() {
		Category.setPage(parseInt(this.value));
		return false;
	},
	nextPageHandler: function() {
		var scroll = false;
		if(this.className.indexOf("scroll") > -1)
			scroll = true;
		Category.setPage(Category.page + 1, scroll);
		return false;
	},
	prevPageHandler: function() {
		var scroll = false;
		if(this.className.indexOf("scroll") > -1)
			scroll = true;
		Category.setPage(Category.page - 1, scroll);
		return false;
	},
	setPage: function(page, scroll) {
		if((page < 1) || (page > Category.getPages()))
			return;
		Category.index = ((Category.list)? Category.itemsPerListPage : Category.itemsPerPage) * (page - 1);
		Category.page = page;
		Category.load(scroll);		
	},
	load: function(scroll) {
		var params = {
			catId: Category.id,
			sort: Category.sort,
			page: Category.page,
			list: Category.list,
			ajax: "true"
		};
		
		$("#indicator").append('<img src="img/site/indicator.gif" />'); //loading indicator
		$("#items").load(Category.url, params, function(response) {
			if(Category.page == 1)
				$("a.prevpage:visible").hide();
			else
				$("a.prevpage:hidden").show();
			if(Category.page == Category.getPages())
				$("a.nextpage:visible").hide();
			else
				$("a.nextpage:hidden").show();
				
			$("select.pagedropdown").selectedIndex(Category.page - 1);
			Cart.initCartLinks();
			
			$(this).find("a.itemlink").click(Category.loadItemHandler);
			
			$("#indicator").find("img").remove(); //remove loading indicator
			
			if(scroll)
				$("#navigation").ScrollTo();
		});
	},
	hide: function() {
		$("#alternativeModes").hide();
		$("#navigation").hide();
		$("#items").hide();
		$("#bottomnavigation").hide();
		$("div.pageselect").hide();
		$("#alphanavigation").hide();
	},
	show: function() {
		$("#alternativeModes").show();
		$("#navigation").show();
		$("#items").show();
		$("#bottomnavigation").show();
		$("div.pageselect").show();
		if(Category.sort == "name") {
			$("#alphanavigation").show();
		}
	}
}

Item = {
	load: function(url) {
		urlParts = url.split("?");
		url = urlParts[0] + "?ajax=true&" + urlParts[1];
		$("#contentItem").remove();
		
		var container = document.createElement("div");
		container.id = "contentItem";
		$("#content").append(container);
		$(container).load(url, null, function(response) {
			Cart.initCartLinks(this);
			$("#nextlink").find("a").click(Item.nextHandler);
			$("#prevlink").find("a").click(Item.prevHandler);
			$("#closeItem").click(Item.closeHandler);
		});
	},
	nextHandler: function() {
		Item.index++;
		if(Item.index >= Item.itemsPerPage) {
			Category.page++;
			Category.index += Item.itemsPerPage;
			Item.index = 0;
		}
		Item.load(this.href);
		return false;
	},
	prevHandler: function() {
		Item.index--;
		if(Item.index < 0) {
			Category.page--;
			Category.index -= Item.itemsPerPage;
			Item.index = Item.itemsPerPage - 1;
		}
		Item.load(this.href);
		return false;
	},
	closeHandler: function() {
		Category.load();
		$("#contentItem").remove();
		Category.show();
		return false;
	}
}

var Cart = {
	TO: 'tocart',
	FROM: 'fromcart',
	
	initCartLinks: function(container) {
		if(container == null) container = $('#content');
		$(container).find("a.cartlink").filter("." + Cart.TO).click(Cart.setIn);
		$(container).find("a.cartlink").filter("." + Cart.FROM).click(Cart.setOut);
	},
	setIn: function() {
		var link = this;
		var item = this.parentNode;
		var callback = function() {
			$(link).unclick().click(Cart.setOut).html(i18n.getString('removeFromCart'));
			if(item.tagName.toLowerCase() != "td")
				$(item).append('<div style="display: none;" class="incart"></div>').find("div.incart").fadeIn(400);
		}
		Cart.loadCart.call(this, "add", callback);
		return false;
	},
	setOut: function() {
		var link = this;
		var item = this.parentNode;
		var callback = function() {
			$(item).find("a.autohide").each(function() { $(item).fadeOut(400, function() { $(this).remove(); }); });
			$(link).unclick().click(Cart.setIn).html(i18n.getString('addToCart'));
			$(item).find("div.incart").fadeOut(400, function() { $(this).remove(); });
		}
		Cart.loadCart.call(this, "remove", callback);
		return false;
	},
	loadCart: function(action, callback) {
		var id = this.id.replace(/cartlink(\d+)/,"$1");
		$("#cart").load("cart.do", { action: action, ajax: "true", id: id }, function(data) {
			callback();
		});
	}
}