function setSortOrder(){
	var orderBy = $F('orderBy');
	var orderDirection = $F('orderDirection');
	new Ajax.Request('/fnc/setSortOrder/', {
		method: 'post',
		parameters: {orderBy: orderBy, orderDirection: orderDirection},
		onSuccess: function(data){
			location.reload();
		},
		onFailure: function(){

		}
	});
}

function updateProductComparison(){
	var newDate = new Date;
	new Ajax.Request('/fnc/compareGetContentTable/?'+newDate.getTime(), {
			onCreate: function(){

			},
			onSuccess: function(data){
				var rTxt = data.responseText;
				if(rTxt != ''){
					$('productComparisonContent').innerHTML = rTxt;
				}
				
				$('btnCompare').observe('click', function(){
					var scHeight = screen.height;
					var scWidth = screen.width;
					var newWidth = (scWidth-250);
					var newHeight = scHeight;
					var posLeft = (scWidth - newWidth) / 2;
					var posTop = (scHeight - newHeight) / 2;
					window.open('/produktjamforelse/', 'comparisonWindow', 'width='+ newWidth +', height='+ newHeight +', left='+ posLeft +', top='+ posTop +', toolbar=0, location=false, menubar=0, status=0, scrollbars=1, resizable=1');
				});
				
				$('productComparisonContent').descendants().each(function(s){
		
					//-- Hitta alla Papperskorgar och sätt action på dem
					if(s.id.startsWith('trashCompareItem')){
						s.observe('click', function(){
							var itmId = s.id.split('trashCompareItem')[1];
							var newDate = new Date;
							new Ajax.Request('/fnc/compareAddItem/?'+newDate.getTime(), {
								method: 'post',
								parameters: { productId: itmId },
								onCreate: function(){
								},
								onSuccess: function(data){
									addRemoveCompareCheck(itmId, 0);
									updateProductComparison.delay(0.2);
								},
								onComplete: function(){
								},
								onFailure: function(data){
									alert('Något gick fel. Prova igen.\n'+data.responseText);
								}
							});
						});
					};
				});

			},
			onFailure: function(data){
			}		
	});
}

function addRemoveCompareCheck(itmId, addRem){
	if( $('compareCheck'+itmId) ){
		if(addRem == 1){
			$('compareCheck'+itmId).removeClassName('btnOrngChk0');
			$('compareCheck'+itmId).addClassName('btnOrngChk1');
		}else if(addRem == 0){
			$('compareCheck'+itmId).removeClassName('btnOrngChk1');
			$('compareCheck'+itmId).addClassName('btnOrngChk0');
		}
	}
}

function compare(itmId){
	var newDate = new Date;
	//if(!isAnimating){
		var statusText;
		new Ajax.Request('/fnc/compareAddItem/?'+newDate.getTime(), {
			method: 'post',
			parameters: {productId: itmId},
			onCreate: function(){

			},
			onSuccess: function(data){
				var rTxt = data.responseText;
				if(rTxt == '1'){					
					statusText = 'Artiklen lades till i jämförelselistan.';
					flashStatusMessage(statusText, itmId, 'ok');

					var newDate = new Date;
					var uid = newDate.getTime();
					
					$('wrapper').insert(new Element("div", { id: "item"+uid }))
					$('item'+uid).hide();
					//$('item'+uid).innerHTML = $('productImage'+itmId).innerHTML
					$('item'+uid).setStyle({
						backgroundImage: 'url(/fnc/getProductPicture/?product_id='+itmId+'&max_width=180&max_height=120)',
						backgroundRepeat: 'no-repeat',
						width:'180px',
						height:'120px',
						backgroundColor:'#fff',
						position:'absolute',
						border:'1px dashed #969696',
						left:$('productImage'+itmId).cumulativeOffset().left+40+'px',
						top:$('productImage'+itmId).cumulativeOffset().top-6+'px'
					});
			
					setAnimationStatus(1.5);
					$('item'+uid).appear({duration:0.5});
		
					new Effect.Move('item'+uid, { x: $('productComparisonWrapper').cumulativeOffset().left+5, y: $('productComparisonWrapper').cumulativeOffset().top+5, mode: 'absolute', duration: 1, queue:{position: 'end', scope: 's'+uid, limit: 4} });
					new Effect.Fade('item'+uid, { duration: 0.2, queue:{position: 'end', scope: 's'+uid, limit: 4} });
				}else if(rTxt == '0'){
					//-- Ta bort bock
				}
				addRemoveCompareCheck(itmId, rTxt);
				updateProductComparison.delay(0.2);
			},
			onFailure: function(data){
				alert(data.responseText);
				flashStatusMessage('Ett fel int&auml;ffade.<br />Produkten &auml;r inte tillagd!', itmId, 'error');
			}
		});
	}
	
productPageTabActive = 'Info';
function productPageTabs(id){

	$('productPage'+productPageTabActive).hide();
	$('productPage'+id).show();
	
	$('productPageTab'+productPageTabActive).childElements().each(function(s){

		s.removeClassName('productPageTabActiveLeft');
		s.removeClassName('productPageTabActiveCenter');
		s.removeClassName('productPageTabActiveRight');

		if(s.hasClassName('productPageTabLeft')){
			s.addClassName('productPageTabInactiveLeft');
		}
		if(s.hasClassName('productPageTabCenter')){
			s.addClassName('productPageTabInactiveCenter');
		}
		if(s.hasClassName('productPageTabRight')){
			s.addClassName('productPageTabInactiveRight');
		}
	});
	
	$('productPageTab'+id).childElements().each(function(s){

		s.removeClassName('productPageTabInactiveLeft');
		s.removeClassName('productPageTabInactiveCenter');
		s.removeClassName('productPageTabInactiveRight');

		if(s.hasClassName('productPageTabLeft')){
			s.addClassName('productPageTabActiveLeft');
		}
		if(s.hasClassName('productPageTabCenter')){
			s.addClassName('productPageTabActiveCenter');
		}
		if(s.hasClassName('productPageTabRight')){
			s.addClassName('productPageTabActiveRight');
		}
	});
	
	productPageTabActive = id;
}

function openLightView(url, title, caption, topclose, w, h, fs){
	var option;
	if(fs == '') fs = false;

	if(w == '' && h == '' && fs == false){
		Lightview.show({				   
		  href: url,
		  title: title,
		  topclose: topclose,
		  caption: caption,
			options: {
				autosize: true,
				topclose: topclose
			}
		});
	}else if(fs == true){
		Lightview.show({				   
		  href: url,
		  title: title,
		  caption: caption,
			options: {
				fullscreen: true,
				topclose: topclose
			}
		});
	}else if(w != '' && h != ''){
		Lightview.show({				   
		  href: url,
		  title: title,
		  caption: caption,
			options: {
				width: w,
				height: h,
				topclose: topclose
			}
		});
	}else{

	}


}

function openXmasCalendar(url, w, h){
	var option;
	Lightview.show({				   
	  href: url,
		options: {
			width: w,
			height: h,
			topclose: true
		}
	});
}
