function showPopup(file,width,height,maxW,url)
{	
	file=file.src;	
	file=file.split("w__");
	file=file[1];
	
	newwidth=width;
	newheight=height;
	
	ratio=maxW/newwidth;
		
	if (newwidth>maxW)
	{	newwidth=maxW;
		newheight=height*ratio;
	}


	var x=window.open('', 'bigPrev', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+(newwidth)+',height='+(newheight));
	
	x.document.write("<html><head><title>Bild</title>");
	x.document.write('<style type="text/css">');
	x.document.write("html,body{margin:0;padding:0;overflow:hidden;}img{cursor:pointer;}");
	x.document.write("</style></head><body>");

	x.document.write('<img src="'+url+'index.php?rex_resize='+newwidth+'w__'+file+'" alt="" onclick="window.close();">');
	x.document.write("</body></html>");
	x.document.close();
	x.focus();
}	


function switchPic(width,height,file,displaywidth,popupwidth,url)
{	img=document.getElementById('prodimage').getElementsByTagName('IMG')[0];
	
	img.src='index.php?rex_resize='+displaywidth+'w__'+file;
	img.onclick=function() {	showPopup(img,width,height,popupwidth,url+"/");	};
}



Behaviour.addLoadEvent 
(	function()
	{			
	}
);

var Rules = {
    '.extern' : function(el)
	{	el.onclick = function()
		{    this.setAttribute('target','_blank');
		}
	},
	'#basketform' : function(el)
	{	el.onsubmit = function()
		{   var variants=document.getElementsBySelector('div.variations div');
			if (!variants)
			{	/* No variants, add to basket */
				return true;
			}
			else
			{	/* Variants are there, check if they are checked.. */
				var checked=new Array();
				var labels=new Array();
				
				for (i=0;i<variants.length;i++)
				{	labels[i]=variants[i].getElementsByTagName('LABEL')[0];
					
					radios=variants[i].getElementsByTagName('INPUT');
					
					checked[i]=0;
					for (j=0;j<radios.length;j++)
					{	if (radios[j].checked==true)
						{	checked[i]=1;
						}
					}
				}
				/* Check if any of the sections don't have a selection */
				variantsNotSelected=1;
				for (i=0;i<checked.length;i++)
				{	if (checked[i]==0)
					{	variantsNotSelected=0;
						missing=labels[i].innerHTML;
					}
				}
				if (variantsNotSelected==0)
				{	alert('Bitte w'+unescape('%E4')+'hlen Sie "'+missing+'" aus.');
				}
				else
				{	return true;
				}
			}		
			
			return false;
		}
	},
	'div.variations input' : function(el)
	{	el.onclick = function()
		{  	varbox=el.parentNode.parentNode.parentNode; 
			productID=el.parentNode.parentNode.parentNode.id;
			
			advAJAX.post({
			url: window.location.href,
			parameters : {
			"ajax" : "variants",
			"level" : el.parentNode.parentNode.className, 
			"option" : el.parentNode.parentNode.id,
			"value" : this.value,
			"product" : productID
			},
			onSuccess : function(obj)
				{ 	// Display the new variants as quickly as possible
					varbox.innerHTML=obj.responseText;
					Behaviour.apply();
					
					
					
					// Recalculate price					
					advAJAX.post({
					url: window.location.href,
					parameters : {
					"ajax" : "recalc",
					"product" : productID
					},
					onSuccess : function(obj)
						{ 	if (document.getElementById('calcPrice'))
							{	document.getElementById('calcPrice').innerHTML=obj.responseText+" ";
							}
						}
					});
					
					
					
				}
			});
			

		}
	}
};

Behaviour.register(Rules);

