﻿function openDownloadablesPopup(szInstruct, imageUrl, downloadImageUrl, aWidth, aHeight) {
	
	var instructions = 
	"<div id='instrDiv' style=\"width: " + (aWidth - 10) + "px; padding: 5px; border: 1px solid black; font-family: verdana; font-size: 9px; color: #ffffff; background-color: #2c8b92\">" +
		"Instruktioner för nerladdning på PC:<br />" +
		"1. Högerklicka på bilden och välj ”Använd som skrivbordsunderlägg” eller ”Använd som skrivbordsbakgrund”.<br />" +
		"<br />Instruktioner för nerladdning på MAC OS:<br />" +
		"1. Kontrollklicka på ”Spara bild”.<br />" +
		"2. Spara bilden till ditt skrivbord.<br />" +
		"3. Gå till äpplemenyn > Systeminställningar > Personligt och välj ”Skrivbord och skärmsläckare”.<br />" +
		"4. sedan den mapp där du har sparat bilden i och leta rätt på bilden.<br />" +
		"<br />Instruktioner för nerladdning på MAC OSX:<br />" +
		"1. Kontrollklicka på bilden och välj ”Använd som skrivbordsbakgrund”.<br />" +
		"2. Klicka på ”Ange skrivbordsbakgrund” på bilden som kommer upp.<br />" +
		"3. Klicka sedan på ”Öppna skrivbordsinställningarna” och klicka på bilden du valt.<br />" +
	"</div>";	
		
	if(szInstruct != undefined ) {
		szInstruct = 
		"<div id='instrDiv' style=\"width: "+(aWidth - 10)+"px; padding: 5px; border: 1px solid black; font-family: verdana; font-size: 9px; color: #ffffff; background-color: #2c8b92\">" +
			szInstruct + 
		"</div>";
		
		instructions = szInstruct;
	}		
	
	var strTemplate = 
	"<html>" +
		"<head>" +
			"<title>Disney XD</title>" +
		"</head>" +
		"<center><body style=\"margin: 0px; padding: 0px\"> "  + 
			"<table>" +
				"<tr>" +
					"<td>" + instructions + "</td>" +
				"</tr>" +
				"<tr align='center'>" +
					"<td ><a href=" + downloadImageUrl + "><img src=" + imageUrl + " border=0></a></td>" +
				"</tr>" +
			"</table>" +
		"</body></center>" +
	"</html>"
	
	
	// by plamen
	var minWidth = 300;
	var minHeight = 400;
	
	var maxWidth = 1150;
	var maxHeight = 8500;
	
	var width  = 800;
	var height = 600;
	
	if(aWidth != undefined && aHeight != undefined) {
		width = aWidth;
		height = aHeight;		
	}
	
	function correctImageSize(){
		if(width < minWidth) {
			width = minWidth + 50;
		} else if(width > maxWidth) {
			width = maxWidth
		}
		
		if(height < minHeight) {
			height = minHeight;
		} else if(height > maxHeight) {
			height = maxHeight;
		}
	}
	
		
	
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+(width+30)+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=yes';
	params += ', scrollbars=yes';
	params += ', status=no';
	params += ', toolbar=no';
	var newWin = window.open("about:blank", "windowName", params);
	newWin.document.write(strTemplate)
	if (window.focus) {newWin.focus()}
	
	var interval;
	function iLoad(imageUrl) {
		var oImg = new Image();
		oImg.src = imageUrl;
		if (oImg.complete) {
			//width = oImg.width;
			//height = oImg.height;
						
			var instrDiv = newWin.document.getElementById("instrDiv");
			var instructionsWidth;
			
			if(oImg.width < minWidth){ 
				instructionsWidth = minWidth;
			} else {
				instructionsWidth = oImg.width;
			}
			instrDiv.style.width = (instructionsWidth)+ "px"
			
			width = oImg.width + 50;
			height = instrDiv.offsetHeight + oImg.height + 100;
			
			correctImageSize();
			newWin.resizeTo(width, height);	
			
			window.clearInterval(interval);			
		}
		else {
			interval = window.setInterval("iLoad('imageUrl')", 1000);
		}
	}
	iLoad(imageUrl);
}
	
	
	
	
	
	// T R A C E   H I S T O R Y
			//window.setTimeout("iLoad('imageUrl')", 1000);
			//alert("width: " + width + " | " + "height: " + height);
			
			
	// C O D E   H I S T O R Y
	
	/* // from galin (>> doesn't work
	// create an image object
	objImage = new Image();	     

	// set what happens once the image has loaded 
	objImage.onLoad=imagesLoaded;
	
	// preload the image file
	objImage.src = imageUrl;
	
	// function invoked on image load
	function imagesLoaded()	{    
		alert(image.width);
	   // document.location.href = downloadImageUrl;
		width = 1000;
		height = objImage.height;
		resizeTo(width, height);
		//resizeBigImages()
	}*/
	
	
	
	
	
	/*function iLoad(imageUrl) {
		var oImg = new Image();
		oImg.src = imageUrl;
		if (oImg.complete) {
			window.alert(oImg.src + ' ' + oImg.width + ' x ' + oImg.height);
			width = oImg.width;
			height = oImg.height;
		}
		else {
			window.setTimeout('iLoad(imgsrc)', 1000);
		}
	}
	//iLoad(imageUrl);
	*/
	
	
	
	
	/*
	var image = new Image()
	image.onLoad = imagesLoaded();
	image.src = imageUrl;	
	
	function imagesLoaded(){
		width = image.width;
		height = image.height;
		resizeBigImage();
	}
	*/
	


