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()
	{	// DESICO Bildswapper
		//Hide all sub pages
		var swappers=document.getElementsBySelector('.bildswapper');
		
		for (i=0;i<swappers.length;i++)
		{	swappics=swappers[i].getElementsByTagName('DIV');
			
			//Filter out divs which arent areas.
			var counter=0;
			swappics2=new Array();
			for (j=0;j<swappics.length;j++)
			{	if(swappics[j].className!="bilddesc")
				{	swappics2[counter]=swappics[j];
					counter++;
				}
			}
			swappics=swappics2;
			
			//Loop through subpics
			for (j=0;j<swappics.length;j++)
			{	swapperclass=swappics[j].className;
				swapperclass=swapperclass.split(' ');
				
				desc=swappics[j].getElementsByTagName('DIV')[0];
				desc.style.display="none";
				
				if (swapperclass[0]=="bild")
				{	thepic=swappics[j].getElementsByTagName('IMG')[0];
					var img=document.createElement("IMG");
					img.setAttribute('src',thepic.getAttribute('src'));
					img.style.cursor="pointer";
					img.className="swapthumb s"+j;
					swappers[i].appendChild(img);
				}
			}
			var newDesc=document.createElement("SPAN");
			newDesc.style.display="block";
			newDesc.className="bilddesc";
			newDesc.innerHTML=swappers[i].getElementsByTagName('DIV')[0].getElementsByTagName('DIV')[0].innerHTML;;
			swappers[i].appendChild(newDesc);
			Behaviour.apply();
		}		
	}
);

var Rules = {
	'.swapthumb' : function(el)
	{	el.onclick = function()
		{	x=this.className;
			x=x.split(' ');x=x[1];
			x=x.split('s');x=x[1];						
			thepics=this.parentNode.getElementsByTagName('DIV');
			var counter=0;
			thepics2=new Array();
			for (i=0;i<thepics.length;i++)
			{	if(thepics[i].className!="bilddesc")
				{	thepics2[counter]=thepics[i];
					counter++;
				}
			}
			thepics=thepics2;
			
			for (i=0;i<thepics.length;i++)
			{	if (i!=x)
				{	thepics[i].style.display="none";
				}
			}
			desc=thepics[x].getElementsByTagName('DIV')[0];
			thepics[x].parentNode.getElementsByTagName('SPAN')[0].innerHTML=desc.innerHTML;
			thepics[x].style.display="block";
		},
		el.onmouseover = function()
		{	this.className=this.className+" bildswaphover";
		},
		el.onmouseout = function()
		{	x=this.className;
			x=x.split(' ');
			this.className=x[0]+" "+x[1];
		}
	},
    '.extern' : function(el)
	{	el.onclick = function()
		{    this.setAttribute('target','_blank');
		}
	},
	'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);

jQuery(document).ready(function()
{	
	jQuery('a[rel="rxs-postage"]').click(function(){
		url = jQuery(this).attr('href');
		var x = window.open(url+'&popup=1','popup','height=300,width=550,scrollbars=1');
		x.focus();
		return false;
	});
	
	jQuery('a[href$=.pdf]').attr('target','_blank');
});


