function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function addProduct(cat,id,type,link) {
	switch(type) {
		case 'jackets':
			type = 'ylaosa';
			break;
		case 'trousers':
			type = 'alaosa';
			break;
		case 'overalls':
			type = 'haalari';
			break;
		case 'accessories':
			type = 'varuste';
			break;
	}
	var c = readCookie('kokeilukori');
	var keksi;
	if(c != null) {
		var parts = c.split('&');
		if(type == 'ylaosa') {
			var ylaosa = parts[0].split('=');
			var yotuotteet = ylaosa[1].split('-');
			for(var i = 0;i < yotuotteet.length;i++) {
				var catid = yotuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					swapRemove(cat,id,type,link);
					return false;
				}
			}
			keksi = parts[0] + (yotuotteet.length > 0 && yotuotteet[0].length > 0 ? '-' : '') + cat + ':' + id + '&' + parts[1] + '&' + parts[2] + '&' + parts[3];
		} else if(type == 'alaosa') {
			var alaosa = parts[1].split('=');
			var aotuotteet = alaosa[1].split('-');
			for(var i = 0;i < aotuotteet.length;i++) {
				var catid = aotuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					swapRemove(cat,id,type,link);
					return false;
				}
			}
			keksi = parts[0] + '&' + parts[1] + (aotuotteet.length > 0 && aotuotteet[0].length > 0 ? '-' : '') + cat + ':' + id + '&' + parts[2] + '&' + parts[3];
		} else if(type == 'haalari') {
			var varuste = parts[2].split('=');
			var vtuotteet = varuste[1].split('-');
			for(var i = 0;i < vtuotteet.length;i++) {
				var catid = vtuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					swapRemove(cat,id,type,link);
					return false;
				}
			}
			keksi = parts[0] + '&' + parts[1] + '&' + parts[2] + (vtuotteet.length > 0 && vtuotteet[0].length > 0 ? '-' : '') + cat + ':' + id + '&' + parts[3];
		} else if(type == 'varuste') {
			var varuste = parts[3].split('=');
			var vtuotteet = varuste[1].split('-');
			for(var i = 0;i < vtuotteet.length;i++) {
				var catid = vtuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					swapRemove(cat,id,type,link);
					return false;
				}
			}
			keksi = parts[0] + '&' + parts[1] + '&' + parts[2] + '&' + parts[3] + (vtuotteet.length > 0 && vtuotteet[0].length > 0 ? '-' : '') + cat + ':' + id;
		}
	} else {
		if(type == 'ylaosa') {
			keksi = 'ylaosa=' + cat + ':' + id + '&alaosa=&haalari=&varuste=';
		} else if(type == 'alaosa') {
			keksi = 'ylaosa=&alaosa=' + cat + ':' + id + '&haalari=&varuste=';
		} else if(type == 'haalari') {
			keksi = 'ylaosa=&alaosa=&haalari=' + cat + ':' + id + '&varuste=';
		} else if(type == 'varuste') {
			keksi = 'ylaosa=&alaosa=&haalari=&varuste=' + cat + ':' + id;
		}
	}
	
	createCookie('kokeilukori',keksi,1);
	
	swapRemove(cat,id,type,link);
	return false;
}

// ylaosa=[alueenid]:[tuoteid],[alueenid]:[tuoteid]&alaosa=[alueenid]:[tuoteid],[alueenid]:[tuoteid]&haalari=[alueenid]:[tuoteid],[alueenid]:[tuoteid]&varuste=[alueenid]:[tuoteid],[alueenid]:[tuoteid]

function removeProduct(cat,id,type,link) {
	switch(type) {
		case 'jackets':
			type = 'ylaosa';
			break;
		case 'trousers':
			type = 'alaosa';
			break;
		case 'overalls':
			type = 'haalari';
			break;
		case 'accessories':
			type = 'varuste';
			break;
	}
	var c = readCookie('kokeilukori');
	var keksi;
	if(c != null) {
		var parts = c.split('&');
		if(type == 'ylaosa') {
			var ylaosa = parts[0].split('=');
			var yotuotteet = ylaosa[1].split('-');
			for(var i = 0;i < yotuotteet.length;i++) {
				var catid = yotuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					keksi = 'ylaosa=';
					for(var j = 0;j < yotuotteet.length;j++) {
						if(j != i) {
							keksi += yotuotteet[j];
							if(!((i == yotuotteet.length-1 && j == yotuotteet.length-2) || j == yotuotteet.length-1)) {
								keksi += '-';
							}
						}
					}
					keksi += '&' + parts[1] + '&' + parts[2] + '&' + parts[3];
					createCookie('kokeilukori',keksi,1);
					swapAdd(cat,id,type,link);
					return false;
				}
			}
			
		} else if(type == 'alaosa') {
			var alaosa = parts[1].split('=');
			var aotuotteet = alaosa[1].split('-');
			for(var i = 0;i < aotuotteet.length;i++) {
				var catid = aotuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					keksi = parts[0] + '&alaosa=';
					for(var j = 0;j < aotuotteet.length;j++) {
						if(j != i) {
							keksi += aotuotteet[j];
							if(!((i == aotuotteet.length-1 && j == aotuotteet.length-2) || j == aotuotteet.length-1)) {
								keksi += '-';
							}
						}
					}
					keksi += '&' + parts[2] + '&' + parts[3];
					createCookie('kokeilukori',keksi,1);
					swapAdd(cat,id,type,link);
					return false;
				}
			}
		} else if(type == 'haalari') {
			var varuste = parts[2].split('=');
			var vtuotteet = varuste[1].split('-');
			for(var i = 0;i < vtuotteet.length;i++) {
				var catid = vtuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					keksi = parts[0] + '&' + parts[1] + '&haalari=';
					for(var j = 0;j < vtuotteet.length;j++) {
						if(j != i) {
							keksi += vtuotteet[j];
							if(!((i == vtuotteet.length-1 && j == vtuotteet.length-2) || j == vtuotteet.length-1)) {
								keksi += '-';
							}
						}
					}
					keksi += '&' + parts[3];
					createCookie('kokeilukori',keksi,1);
					swapAdd(cat,id,type,link);
					return false;
				}
			}
		} else if(type == 'varuste') {
			var varuste = parts[3].split('=');
			var vtuotteet = varuste[1].split('-');
			for(var i = 0;i < vtuotteet.length;i++) {
				var catid = vtuotteet[i].split(':');
				var tcat = catid[0];
				var tid = catid[1];
				if(tcat == cat && tid == id) {
					keksi = parts[0] + '&' + parts[1] + '&' + parts[2] + '&varuste=';
					for(var j = 0;j < vtuotteet.length;j++) {
						if(j != i) {
							keksi += vtuotteet[j];
							if(!((i == vtuotteet.length-1 && j == vtuotteet.length-2) || j == vtuotteet.length-1)) {
								keksi += '-';
							}
						}
					}
					createCookie('kokeilukori',keksi,1);
					swapAdd(cat,id,type,link);
					return false;
				}
			}
		}
	}
	
	swapAdd(cat,id,type,link);
	return false;
}

function rotateBackward(type) {
	var c = readCookie('kokeilukori');
	var keksi;
	if(c != null) {
		var parts = c.split('&');
		if(type == 'ylaosa') {
			var ylaosa = parts[0].split('=');
			var yotuotteet = ylaosa[1].split('-');
			var temp = yotuotteet[0];
			for(var i = 0;i < yotuotteet.length-1;i++) {
				yotuotteet[i] = yotuotteet[i+1];
			}
			yotuotteet[yotuotteet.length-1] = temp;
			var tulos = yotuotteet.join('-');
			keksi = 'ylaosa=' + tulos + '&' + parts[1] + '&' + parts[2] + '&' + parts[3];
			createCookie('kokeilukori',keksi,1);
		} else if(type == 'alaosa') {
			var alaosa = parts[1].split('=');
			var aotuotteet = alaosa[1].split('-');
			var temp = aotuotteet[0];
			for(var i = 0;i < aotuotteet.length-1;i++) {
				aotuotteet[i] = aotuotteet[i+1];
			}
			aotuotteet[aotuotteet.length-1] = temp;
			var tulos = aotuotteet.join('-');
			keksi = parts[0] + '&' + 'alaosa=' + tulos + '&' + parts[2] + '&' + parts[3];
			createCookie('kokeilukori',keksi,1);
		} else if(type == 'haalari') {
			var haalari = parts[2].split('=');
			var htuotteet = haalari[1].split('-');
			var temp = htuotteet[0];
			for(var i = 0;i < htuotteet.length-1;i++) {
				htuotteet[i] = htuotteet[i+1];
			}
			htuotteet[htuotteet.length-1] = temp;
			var tulos = htuotteet.join('-');
			keksi = parts[0] + '&' + parts[1] + '&' + 'haalari=' + tulos + '&' + parts[3];
			createCookie('kokeilukori',keksi,1);
		}
	}
	return false;
}

function rotateForward(type) {
	var c = readCookie('kokeilukori');
	var keksi;
	if(c != null) {
		var parts = c.split('&');
		if(type == 'ylaosa') {
			var ylaosa = parts[0].split('=');
			var yotuotteet = ylaosa[1].split('-');
			var temp = yotuotteet[yotuotteet.length-1];
			for(var i = yotuotteet.length-1;i > 0;i--) {
				yotuotteet[i] = yotuotteet[i-1];
			}
			yotuotteet[0] = temp;
			var tulos = yotuotteet.join('-');
			keksi = 'ylaosa=' + tulos + '&' + parts[1] + '&' + parts[2] + '&' + parts[3];
			createCookie('kokeilukori',keksi,1);
		} else if(type == 'alaosa') {
			var alaosa = parts[1].split('=');
			var aotuotteet = alaosa[1].split('-');
			var temp = aotuotteet[aotuotteet.length-1];
			for(var i = aotuotteet.length-1;i > 0;i--) {
				aotuotteet[i] = aotuotteet[i-1];
			}
			aotuotteet[0] = temp;
			var tulos = aotuotteet.join('-');
			keksi = parts[0] + '&' + 'alaosa=' + tulos + '&' + parts[2] + '&' + parts[3];
			createCookie('kokeilukori',keksi,1);
		} else if(type == 'haalari') {
			var haalari = parts[2].split('=');
			var htuotteet = haalari[1].split('-');
			var temp = htuotteet[htuotteet.length-1];
			for(var i = htuotteet.length-1;i > 0;i--) {
				htuotteet[i] = htuotteet[i-1];
			}
			htuotteet[0] = temp;
			var tulos = htuotteet.join('-');
			keksi = parts[0] + '&' + parts[1] + '&' + 'haalari=' + tulos + '&' + parts[3];
			createCookie('kokeilukori',keksi,1);
		}
	}
	return false;
}

function initKkMaara() {
	if($('kkmaara')) {
		var c = readCookie('kokeilukori');
		if(c != null) {
			var prods = c.split(':');
			var maara = prods.length-1;
			$('kkmaara').update(maara);
		}
	}
	if($$('div.kktuotteet').length == 0) {
		var c = readCookie('kokeilukori');
		if(c != null) {
			var parts = c.split('&');
			var products = new Array();
			for(var i = 0;i < parts.length;i++) {
				var temp = parts[i].split('=');
				var p = temp[1].split('-');
				for(var j = 0;j < p.length;j++) {
					temp = p[j].split(':');
					products[products.length] = temp.join(',');
				}
			}
			var nappulat = $$('.tuote .oikea div');
			for(var i = 0;i < nappulat.length;i++) {
				var oc = nappulat[i].readAttribute('onclick');
				var temp = oc.split(',');
				var cat = temp[0].split('(');
				cat = cat[1];
				var prod = cat + ',' + temp[1];
				if(products.indexOf(prod) != -1) {
					var type = temp[2];
					type = type.substring(1,type.length-1);
					nappulat[i].cat = cat;
					nappulat[i].catid = temp[1];
					nappulat[i].ptype = type;
					nappulat[i].onclick = function() {
						removeProduct(this.cat,this.catid,this.ptype,this);
					}
					nappulat[i].update(napit['poistakorista']);
					Effect.Fade('takitjaliivit', {duration: 0.1, to: 0.999}); // iefix
				}
			}
		}
	}
}

var removable;

function removeProductInKkori(cat,id,type,link) {
	if(removable) {
		return false;
	}
	removeProduct(cat,id,type,link);
	if(type == 'ylaosa') {
		var ylaosat = $$('#ylaosastore div.tuote');
		if(ylaosat.length > 0) {
			removable = $($('ylaosacontainer').firstDescendant());
			Effect.Fade(removable, {duration: 0.2, to: 0.01, afterFinish: function() { prevYlaosa(); }});
			if(ylaosat.length == 1) {
				Effect.Fade($$('#ylaosa div.vasennappi')[0], {duration: 1, to: 0.01});
				Effect.Fade($$('#ylaosa div.oikeanappi')[0], {duration: 1, to: 0.01});
			}
		} else {
			new Effect.Morph($$('#ylaosa div.vasennappi')[0], {style:'height: 0px; margin-top: 0px;', duration: 0.5});
			new Effect.Morph($$('#ylaosa div.oikeanappi')[0], {style:'height: 0px; margin-top: 0px;', duration: 0.5});
			if(!$('alaosa')) {
				new Effect.Morph('ylaalaosa', {style: 'margin: 0px;', duration: 0.5});				
				new Effect.BlindUp($($('ylaalaosa').previous('h3')), {duration: 0.5});
			}
			new Effect.Morph('ylaosacontainer', {style:'height: 0px', duration: 0.5, afterFinish: function() { 
				$('ylaosa').remove();
				if(!$('alaosa')) {
					$($('ylaalaosa').previous('h3')).remove();
					$('ylaalaosa').remove();
				} 
			}});
			if($('alaosacontainer')) {
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.1}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.2}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.3}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.4}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.5}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.6}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.7}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.8}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 0.9}); // iefix
				Effect.Fade('alaosacontainer', {duration: 0.1, to: 0.999, delay: 1}); // iefix
			}
		}
	} else if(type == 'alaosa') {
		var alaosat = $$('#alaosastore div.tuote');
		if(alaosat.length > 0) {
			removable = $($('alaosacontainer').firstDescendant());
			Effect.Fade(removable, {duration: 0.2, to: 0.01, afterFinish: function() { prevAlaosa(); }});
			if(alaosat.length == 1) {
				Effect.Fade($$('#alaosa div.vasennappi')[0], {duration: 1, to: 0.01});
				Effect.Fade($$('#alaosa div.oikeanappi')[0], {duration: 1, to: 0.01});
			}
		} else {
			new Effect.Morph($$('#alaosa div.vasennappi')[0], {style:'height: 0px; margin-top: 0px;', duration: 0.5});
			new Effect.Morph($$('#alaosa div.oikeanappi')[0], {style:'height: 0px; margin-top: 0px;', duration: 0.5});
			if(!$('ylaosa')) {
				new Effect.Morph('ylaalaosa', {style: 'margin: 0px;', duration: 0.5});				
				new Effect.BlindUp($($('ylaalaosa').previous('h3')), {duration: 0.5});
			}
			new Effect.Morph('alaosacontainer', {style:'height: 0px', duration: 0.5, afterFinish: function() { 
				$('alaosa').remove();
				if(!$('ylaosa')) {
					$($('ylaalaosa').previous('h3')).remove();
					$('ylaalaosa').remove();
				}
			}});
		}
	} else if(type == 'haalari') {
		var kokovartalot = $$('#kokovartalostore div.tuote');
		if(kokovartalot.length > 0) {
			removable = $($('kokovartalocontainer').firstDescendant());
			Effect.Fade(removable, {duration: 0.2, to: 0.01, afterFinish: function() { prevKokovartalo(); }});
			if(kokovartalot.length == 1) {
				Effect.Fade($$('#kokovartalo div.vasennappi')[0], {duration: 1, to: 0.01});
				Effect.Fade($$('#kokovartalo div.oikeanappi')[0], {duration: 1, to: 0.01});
			}
		} else {
			new Effect.Morph($$('#kokovartalo div.vasennappi')[0], {style:'height: 0px; margin-top: 0px;', duration: 0.5});
			new Effect.Morph($$('#kokovartalo div.oikeanappi')[0], {style:'height: 0px; margin-top: 0px;', duration: 0.5});
			new Effect.Morph('kokovartaloasu', {style: 'margin: 0px;', duration: 0.5});				
			new Effect.BlindUp($($('kokovartaloasu').previous('h3')), {duration: 0.5});
			new Effect.Morph('kokovartalocontainer', {style:'height: 0px', duration: 0.5, afterFinish: function() { 
				$($('kokovartaloasu').previous('h3')).remove();
				$('kokovartaloasu').remove();
			}});
		}
	} else if(type == 'varuste') {
		var e = link;
		while(e = e.parentNode) {
			if(e.className == 'tuote') {
				removable = $(e);
				Effect.BlindUp(removable, {duration: 0.5, afterFinish: function() { removable.remove(); removable = null; }});
				break;
			}
		}
		var varusteet = $$('#lisavarustecontainer div.tuote');
		if(varusteet.length == 1) {
			new Effect.Morph('lisavarusteet', {style: 'margin: 0px;', duration: 0.5});				
			new Effect.BlindUp($($('lisavarusteet').previous('h3')), {duration: 0.5});
			new Effect.Morph('lisavarustecontainer', {style:'height: 0px', duration: 0.5, afterFinish: function() { 
				$($('lisavarusteet').previous('h3')).remove();
				$('lisavarusteet').remove();
			}});
		}
	}
}

var swapButton;
var notediv=new Array();

function swapRemove(cat,id,type,link) {
	link.onclick = function() {
		removeProduct(cat,id,type,link);
	}
	var pos = Position.cumulativeOffset($(link).firstDescendant());
	var nn = notediv.length;
	notediv[nn] = document.createElement('div');
	notediv[nn] = $(notediv[nn]);
	notediv[nn].className = 'note';
	notediv[nn].setStyle({
		top: (pos[1]+19)+'px',
		left: pos[0]+'px'
	});
	notediv[nn].style.display = 'none';
	notediv[nn].update(tekstit['notediv_lisatty']);
	var maara = parseInt($('kkmaara').innerHTML);
	maara += 1;
	$('kkmaara').update(maara);
	document.body.appendChild(notediv[nn]);
	Effect.Appear(notediv[nn], {duration: 0.5});
	Effect.Fade(notediv[nn], {duration: 0.50, delay: 3.5, afterFinish: function(note) { note.element.remove(); }});
	Effect.Fade($(link), {duration: 0.20, to: 0.01, afterFinish: function(link) {
		Element.update(link.element,napit['poistakorista']);
		Effect.Appear(link.element, {duration: 0.20});
	}});
}

function swapAdd(cat,id,type,link) {
	if($$('div.kktuotteet').length == 0) {
		link.onclick = function() {
			addProduct(cat,id,type,link);
		}
		var pos = Position.cumulativeOffset($(link).firstDescendant());
		var nn = notediv.length;
		notediv[nn] = document.createElement('div');
		notediv[nn] = $(notediv[nn]);
		notediv[nn].setStyle({
			top: (pos[1]+19)+'px',
			left: pos[0]+'px'
		});
		notediv[nn].style.display = 'none';
		notediv[nn].className = 'note';
		notediv[nn].update(tekstit['notediv_poistettu']);
		document.body.appendChild(notediv[nn]);
		Effect.Appear(notediv[nn], {duration: 0.5});
		Effect.Fade(notediv[nn], {duration: 0.50, delay: 2, afterFinish: function(note) { note.element.remove(); }});
		Effect.Fade($(link), {duration: 0.20, to: 0.01, afterFinish: function(link) {
			Element.update(link.element,napit['lisaakoriin']);
			Effect.Appear(link.element, {duration: 0.20});
		}});
	}
	var maara = parseInt($('kkmaara').innerHTML);
	maara -= 1;
	$('kkmaara').update(maara);
}

function getVar(name) {
	var get_string = document.location.search;         
	var return_value = '';
	do {
		var name_index = get_string.indexOf(name + '=');

		if(name_index != -1) {
			get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
			var end_of_value = get_string.indexOf('&');
			if(end_of_value != -1) {
				var value = get_string.substr(0, end_of_value);                
			} else {
				var value = get_string;
			}
			if(return_value == '' || value == '') {
				return_value += value;
			} else {
				return_value += ', ' + value;
			}
		}
	} while(name_index != -1);

	return(return_value);        
}

var current;

function prevYlaosa() {
	var prev = $('ylaosastore').immediateDescendants().last();
	if(!prev || current) {
		return false;
	}
	current = $('ylaosacontainer').firstDescendant();
	prev.setStyle({zIndex: '1', opacity: '0'});
	current.setStyle({zIndex: '2'});
	Effect.MoveBy( prev, 0, 570 , {duration: 0.0,  transition: Effect.Transitions.sinoidal});
	$('ylaosacontainer').insertBefore(prev, current);
	Effect.MoveBy( current, 0, -570 , {duration: 0.3,  transition: Effect.Transitions.sinoidal, afterFinish: function() { $('ylaosastore').insertBefore(current, $('ylaosastore').firstDescendant()); }});
	new Effect.Morph(prev, {duration: 0.1, style: 'opacity: 1;'});
	Effect.MoveBy( prev, 0, -570 , {duration: 0.3, delay: 0.1, transition: Effect.Transitions.sinoidal});
	if(removable) {
		Effect.MoveBy( current, 0, 570 , {duration: 0.0, delay: 0.4, transition: Effect.Transitions.sinoidal, afterFinish: function() { removable.remove(); removable = null; current = null; }});
	} else {
		Effect.MoveBy( current, 0, 570 , {duration: 0.0, delay: 0.35, transition: Effect.Transitions.sinoidal, afterFinish: function() { current = null; }});
		rotateForward('ylaosa');
	}
	return false;
}

function nextYlaosa() {
	var next = $('ylaosastore').firstDescendant();
	if(!next || current) {
		return false;
	}
	current = $('ylaosacontainer').firstDescendant();
	next.setStyle({zIndex: '1', opacity: '0'});
	current.setStyle({zIndex: '2'});
	Effect.MoveBy( next, 0, -570 , {duration: 0.0,  transition: Effect.Transitions.sinoidal});
	$('ylaosacontainer').insertBefore(next, current);
	Effect.MoveBy( current, 0, 570 , {duration: 0.3,  transition: Effect.Transitions.sinoidal, afterFinish: function() { $('ylaosastore').appendChild(current); }});
	new Effect.Morph(next, {duration: 0.1, style: 'opacity: 1;'});
	Effect.MoveBy( next, 0, 570 , {duration: 0.3, delay: 0.1, transition: Effect.Transitions.sinoidal});
	Effect.MoveBy( current, 0, -570 , {duration: 0.0, delay: 0.35, transition: Effect.Transitions.sinoidal, afterFinish: function() { current = null; }});
	rotateBackward('ylaosa');
	return false;
}

function prevAlaosa() {
	var prev = $('alaosastore').immediateDescendants().last();	
	if(!prev || current) {
		return false;
	}
	current = $('alaosacontainer').firstDescendant();
	prev.setStyle({zIndex: '1', opacity: '0'});
	current.setStyle({zIndex: '2'});
	Effect.MoveBy( prev, 0, 570 , {duration: 0.0,  transition: Effect.Transitions.sinoidal});
	$('alaosacontainer').insertBefore(prev, current);
	Effect.MoveBy( current, 0, -570 , {duration: 0.3,  transition: Effect.Transitions.sinoidal, afterFinish: function() { $('alaosastore').insertBefore(current, $('alaosastore').firstDescendant()); }});
	new Effect.Morph(prev, {duration: 0.1, style: 'opacity: 1;'});
	Effect.MoveBy( prev, 0, -570 , {duration: 0.3, delay: 0.1, transition: Effect.Transitions.sinoidal});
	if(removable) {
		Effect.MoveBy( current, 0, 570 , {duration: 0.0, delay: 0.4, transition: Effect.Transitions.sinoidal, afterFinish: function() { removable.remove(); removable = null; current = null; }});
	} else {
		Effect.MoveBy( current, 0, 570 , {duration: 0.0, delay: 0.35, transition: Effect.Transitions.sinoidal, afterFinish: function() { current = null; }});
		rotateForward('alaosa');
	}
	return false;
}

function nextAlaosa() {
	var next = $('alaosastore').firstDescendant();
	if(!next || current) {
		return false;
	}
	current = $('alaosacontainer').firstDescendant();
	next.setStyle({zIndex: '1', opacity: '0'});
	current.setStyle({zIndex: '2'});
	Effect.MoveBy( next, 0, -570 , {duration: 0.0,  transition: Effect.Transitions.sinoidal});
	$('alaosacontainer').insertBefore(next, current);
	Effect.MoveBy( current, 0, 570 , {duration: 0.3,  transition: Effect.Transitions.sinoidal, afterFinish: function() { $('alaosastore').appendChild(current); }});
	new Effect.Morph(next, {duration: 0.1, style: 'opacity: 1;'});
	Effect.MoveBy( next, 0, 570 , {duration: 0.3, delay: 0.1, transition: Effect.Transitions.sinoidal});
	Effect.MoveBy( current, 0, -570 , {duration: 0.0, delay: 0.35, transition: Effect.Transitions.sinoidal, afterFinish: function() { current = null; }});
	rotateBackward('alaosa');
	return false;
}

function prevKokovartalo() {
	var prev = $('kokovartalostore').immediateDescendants().last();	
	if(!prev || current) {
		return false;
	}
	current = $('kokovartalocontainer').firstDescendant();
	prev.setStyle({zIndex: '1', opacity: '0'});
	current.setStyle({zIndex: '2'});
	Effect.MoveBy( prev, 0, 570 , {duration: 0.0,  transition: Effect.Transitions.sinoidal});
	$('kokovartalocontainer').insertBefore(prev, current);
	Effect.MoveBy( current, 0, -570 , {duration: 0.3,  transition: Effect.Transitions.sinoidal, afterFinish: function() { $('kokovartalostore').insertBefore(current, $('kokovartalostore').firstDescendant()); }});
	new Effect.Morph(prev, {duration: 0.1, style: 'opacity: 1;'});
	Effect.MoveBy( prev, 0, -570 , {duration: 0.3, delay: 0.1, transition: Effect.Transitions.sinoidal});
	if(removable) {
		Effect.MoveBy( current, 0, 570 , {duration: 0.0, delay: 0.4, transition: Effect.Transitions.sinoidal, afterFinish: function() { removable.remove(); removable = null; current = null; }});
	} else {
		Effect.MoveBy( current, 0, 570 , {duration: 0.0, delay: 0.35, transition: Effect.Transitions.sinoidal, afterFinish: function() { current = null; }});
		rotateForward('haalari');
	}
	return false;
}

function nextKokovartalo() {
	var next = $('kokovartalostore').firstDescendant();
	if(!next || current) {
		return false;
	}
	current = $('kokovartalocontainer').firstDescendant();
	next.setStyle({zIndex: '1', opacity: '0'});
	current.setStyle({zIndex: '2'});
	Effect.MoveBy( next, 0, -570 , {duration: 0.0,  transition: Effect.Transitions.sinoidal});
	$('kokovartalocontainer').insertBefore(next, current);
	Effect.MoveBy( current, 0, 570 , {duration: 0.3,  transition: Effect.Transitions.sinoidal, afterFinish: function() { $('kokovartalostore').appendChild(current); }});
	new Effect.Morph(next, {duration: 0.1, style: 'opacity: 1;'});
	Effect.MoveBy( next, 0, 570 , {duration: 0.3, delay: 0.1, transition: Effect.Transitions.sinoidal});
	Effect.MoveBy( current, 0, -570 , {duration: 0.0, delay: 0.35, transition: Effect.Transitions.sinoidal, afterFinish: function() { current = null; }});
	rotateBackward('haalari');
	return false;
}

function extendSearch() {
	if($('hakuviesti') && getVar('ext') != 1) {
		new Effect.Fade('hakuviesti', {duration: 0.5});
	}
	Effect.SlideDown('laajennettuhaku', {duration:0.5});
	Effect.Fade('hakuswitch', {duration: 0.25, to: 0.01, afterFinish: function() {
		$('hakuswitch').href = '?ext=0';
		$('hakuswitch').onclick = hideSearch;
		$('hakuswitch').update(tekstit['normaalihaku']);
		Effect.Appear('hakuswitch', {duration: 0.25, from: 0.01, to: 1.0});
	}});
	Effect.Fade('paahakunappi', {duration: 0.5});
	return false;
}

function extendSearchFast() {
	if($('hakuviesti') && getVar('ext') != 1) {
		new Effect.Fade('hakuviesti', {duration: 0});
	}
	Effect.SlideDown('laajennettuhaku', {duration:0});
	Effect.Fade('hakuswitch', {duration: 0, to: 0.01});
	Effect.Fade('paahakunappi', {duration: 0});
	$('hakuswitch').href = '?ext=0';
	$('hakuswitch').onclick = hideSearch;
	$('hakuswitch').update(tekstit['normaalihaku']);
	Effect.Appear('hakuswitch', {duration: 0});
	return false;
}

function hideSearch() {
	Effect.SlideUp('laajennettuhaku', {duration:0.5});
	Effect.Fade('hakuswitch', {duration: 0.25, to: 0.01, afterFinish: function() {
		$('hakuswitch').href = '?ext=1';
		$('hakuswitch').onclick = extendSearch;
		$('hakuswitch').update(tekstit['laajennettuhaku']);
		Effect.Appear('hakuswitch', {duration: 0.25, from: 0.01, to: 1.0});
	}});
	Effect.Appear('paahakunappi', {duration: 0.5});
	return false;
}

function selectAll(ele) {
	$(ele).getElementsBySelector('input.checkbox').each(function(s) {
		s.checked = 1;
	});
}

function deselectAll(ele) {
	$(ele).getElementsBySelector('input.checkbox').each(function(s) {
		s.checked = 0;
	});	
}

function check(ele) {
	var loytyyko = $$('#laajennettuhaku input.button');
	if(loytyyko.length > 0) {
		if($('laajennettuhaku').getStyle('display') != 'none') {
			return true;
		}
	}
	if($(ele).value.length == 0) {
		new Effect.Highlight(ele, {duration: 1.0, startcolor: '#fed100'});
		return false;
	}
	return true;
}

function fadeAndExtendSearch(ele) {
	Effect.Fade(ele, {duration: 0.5});
	return extendSearch();
}

function initKori() {
	var qs = document.location.search.substring(9);
	
	if(qs.length > 0) {
		if(getVar('lang')) {
			qs = qs.substring(8);
		}
		createCookie('kokeilukori',qs,1);
		window.location = url['kokeilukori'];
	}
	
	if(!$('ylaosacontainer')) {
		return false;
	}
		
	var ylaosakoot = $$('#ylaosa div.tuote img');
	var maxh = 0;
	for(var i = 0;i < ylaosakoot.length;i++) {
		var h = $(ylaosakoot[i]).getHeight();
		if(h > maxh) maxh = h;
	}
	var ylaosat = $$('#ylaosa div.tuote');
	for(var i = 0;i < ylaosat.length;i++) {
		ylaosat[i].setStyle({
			height: (maxh+40)+'px'
		});
	}
	$('ylaosacontainer').setStyle({
		height: (maxh+40)+'px'
	});
	$$('#ylaosa div.vasennappi')[0].setStyle({
		marginTop: Math.round((maxh+40)/2-20)+'px'
	});
	$$('#ylaosa div.oikeanappi')[0].setStyle({
		marginTop: Math.round((maxh+40)/2-20)+'px'
	});
	if(ylaosat.length == 0) {
		new Effect.Morph('ylaosa', {duration: 0, style:'height: 0px', afterFinish: function() { $('ylaosa').remove(); }});
	}
	
	if(ylaosat.length < 2) {
		Effect.Fade($$('#ylaosa div.vasennappi')[0], {duration: 0, to: 0.01});
		Effect.Fade($$('#ylaosa div.oikeanappi')[0], {duration: 0, to: 0.01});
	}
	
	var alaosakoot = $$('#alaosa div.tuote img');
	maxh = 0;
	for(var i = 0;i < alaosakoot.length;i++) {
		var h = $(alaosakoot[i]).getHeight();
		if(h > maxh) maxh = h;
	}
	var alaosat = $$('#alaosa div.tuote');
	for(var i = 0;i < alaosat.length;i++) {
		alaosat[i].setStyle({
			height: (maxh+40)+'px'
		});
	}
	$('alaosacontainer').setStyle({
		height: (maxh+40)+'px'
	});
	$$('#alaosa div.vasennappi')[0].setStyle({
		marginTop: Math.round((maxh+40)/2-20)+'px'
	});
	$$('#alaosa div.oikeanappi')[0].setStyle({
		marginTop: Math.round((maxh+40)/2-20)+'px'
	});
	if(alaosat.length == 0) {
		new Effect.Morph('alaosa', {duration: 0, style:'height: 0px', afterFinish: function() { $('alaosa').remove(); }});
	}
	
	if(alaosat.length < 2) {
		Effect.Fade($$('#alaosa div.vasennappi')[0], {duration: 0, to: 0.01});
		Effect.Fade($$('#alaosa div.oikeanappi')[0], {duration: 0, to: 0.01});
	}
	
	if(ylaosat.length == 0 && alaosat.length == 0) {
		$($('ylaalaosa').previous('h3')).remove();
		$('ylaalaosa').remove();
	}
	
	var kokovartalokoot = $$('#kokovartalo div.tuote img');
	maxh = 0;
	for(var i = 0;i < kokovartalokoot.length;i++) {
		var h = $(kokovartalokoot[i]).getHeight();
		if(h > maxh) maxh = h;
	}
	var kokovartalot = $$('#kokovartalo div.tuote');
	for(var i = 0;i < kokovartalot.length;i++) {
		kokovartalot[i].setStyle({
			height: (maxh+40)+'px'
		});
	}
	$('kokovartalocontainer').setStyle({
		height: (maxh+40)+'px'
	});
	$$('#kokovartalo div.vasennappi')[0].setStyle({
		marginTop: Math.round((maxh+40)/2-20)+'px'
	});
	$$('#kokovartalo div.oikeanappi')[0].setStyle({
		marginTop: Math.round((maxh+40)/2-20)+'px'
	});
	if(kokovartalot.length == 0) {
		new Effect.Morph('kokovartaloasu', {duration: 0, style:'height: 0px', afterFinish: function() { $($('kokovartaloasu').previous('h3')).remove(); $('kokovartaloasu').remove(); }});
	}
	
	if(kokovartalot.length < 2) {
		Effect.Fade($$('#kokovartalo div.vasennappi')[0], {duration: 0, to: 0.01});
		Effect.Fade($$('#kokovartalo div.oikeanappi')[0], {duration: 0, to: 0.01});
	}
	
	var varusteet = $$('#lisavaruste div.tuote');
	if(varusteet.length == 0) {
		new Effect.Morph('lisavarusteet', {duration: 0, style:'height: 0px', afterFinish: function() { $($('lisavarusteet').previous('h3')).remove(); $('lisavarusteet').remove(); }});
	}
	
	initKkMaara();
}

function sendKkori() {
	var kknappi = $('kkoriemail');
	var pos = Position.cumulativeOffset($(kknappi.firstDescendant()));
	var formdiv = document.createElement('div');
	formdiv = $(formdiv);
	formdiv.id = 'emaildiv'
	formdiv.update('<div class="close" onclick="closeSend()">'+tekstit['sulje']+'</div><form id="emailform" action="http://www.dimex.fi/apps/dimex2/dressingroomMailSender.jsp" onsubmit="return submitKkoriSend()" method="post">'+
	'<label for="kk_email">'+tekstit['email']+'</label>'+
	'<input type="text" id="kk_email" name="kk_email" />'+
	'<input class="button" type="submit" value="" />'+
'</form>');
	formdiv.setStyle({
		top: (pos[1]+34)+'px',
		left: (pos[0]-43)+'px',
		display: 'none'
	});
	document.body.appendChild(formdiv);
	Effect.Appear(formdiv, {duration: 0.5, afterFinish: function() { $('kk_email').focus(); }});
}

function closeSend() {
	Effect.Fade($('emaildiv'), {duration: 0.5, afterFinish: function() { $('emaildiv').remove(); }});
}

function submitKkoriSend() {
	var email = $F('kk_email');
	$('emaildiv').update('<img src="/resources/userfiles/Image/img/loader.gif" alt="'+tekstit['lahetetaan']+'" />');
	new Ajax.Request('http://www.dimex.fi/apps/dimex2/dressingroomMailSender.jsp', {
		method: 'post',
		parameters: 'kk_email='+email,
		onSuccess: function(transport) {
			$('emaildiv').update(tekstit['korilahetetty']);
			Effect.Fade($('emaildiv'), {delay: 2, afterFinish: function() { $('emaildiv').remove(); }});
		}
	});	
	return false;
}

