/**
* requires: prototype, dwr
* UserDownloadBox is a class that represents a box for getting the download link for a content.
* Box contains a Listbox where a FileFormat can be chosen
* 
* the document needs to have an element with id "contentMain" where it can append itself to
* 
* styles for the following elements are asumed:
* 
* #userDownloadBoxBackgroundDiv
* #userDownloadBoxOverlayDiv
*/
var downloadBox = Class.create();
downloadBox.prototype={
	initialize : function(appRoot,contentId)
	{
		this.appRoot = appRoot;
		this.contentId = contentId;
		
		this.backgroundDiv = document.createElement("div");
		this.backgroundDiv.id = "userDownloadBoxBackgroundDiv";
		this.backgroundDiv.style.top = "0px";
		this.backgroundDiv.style.left = "0px";
		this.backgroundDiv.style.height = Math.max(document.body.scrollHeight,(document.documentElement.clientHeight || document.body.clientHeight || 0))+"px";
		this.backgroundDiv.style.width = Math.min(document.body.scrollWidth,(document.documentElement.clientWidth || document.body.clientWidth || 0))+"px";
		this.overlayDiv = document.createElement("div");
		this.overlayDiv.id = "userDownloadBoxOverlayDiv";
		
		this.overlayInnerDiv = document.createElement("div");
		this.overlayInnerDiv.id = "userDownloadBoxOverlayInnerDiv";

		this.headerText = document.createElement("div");
		this.headerText.innerHTML = unescape("Bitte w%E4hle den Dateityp aus%2C den du herunterladen willst%3A");
		
		this.dropDown = document.createElement("select");
		this.dropDown.name = "userDownloadBoxDropDown";
		this.dropDown.className = "userDownloadBoxDropDown";
		this.dropDown.id = "userDownloadBoxDropDown";
		this.dropDown.size = 1;
		
		for(var i = 0; i < 3; i++)
			this.dropDown.options[i] = document.createElement("option");
		this.dropDown.options[0].innerHTML = "windows(avi)";
		this.dropDown.options[1].innerHTML = "Mac(mp4)";
		this.dropDown.options[2].innerHTML = "Handy(3gpp)";
		this.formats = new Array("avi", "mp4", "3gpp");
		
		this.getAddressButtonLink = document.createElement("a");
		this.getAddressButtonLink.href = "javascript:void(0)";
		this.getAddressButton = document.createElement("img");
		this.getAddressButton.src = appRoot+"/images/btn_weiter.gif";
		this.getAddressButton.onclick = function(){
			dwr.engine._execute(ContentManager._path, 'ContentManager', 'getDownloadLink',downloadBoxInstance.contentId,
			downloadBoxInstance.formats[downloadBoxInstance.dropDown.selectedIndex],downloadBoxInstance.getAddressCallback);
	
		};
		this.getAddressButtonLink.appendChild(this.getAddressButton);
		
		this.closeButtonLink = document.createElement("a");
		this.closeButtonLink.href = "javascript:void(0)";
		this.closeButton = document.createElement("img");
		this.closeButton.src=appRoot+"/images/btn_abbrechen.gif";
		this.closeButton.onclick = function(){
			downloadBoxInstance.closeDownloadBox.call(downloadBoxInstance);
				}
		this.closeButtonLink.appendChild(this.closeButton);
		
		this.overlayInnerDiv.appendChild(this.headerText);
		this.overlayInnerDiv.appendChild(this.dropDown);
		this.overlayInnerDiv.appendChild(this.closeButtonLink);
		this.overlayInnerDiv.appendChild(this.getAddressButtonLink);
		
		this.overlayDiv.appendChild(this.overlayInnerDiv);
		
		document.getElementById("contentMain").appendChild(this.backgroundDiv);
		document.getElementById("contentMain").appendChild(this.overlayDiv);
			
	},
	getAddressCallback : function(DataFromServer)
	{
		downloadBoxInstance.getAddress.call(downloadBoxInstance,DataFromServer);
	},
	getAddress : function(link)
	{
		if(link=="mail")
		{
			this.closeDownloadBox();
			alert(unescape("Video wird nun im ausgew%E4hlten Format erstellt. Sobald die Erstellung abgeschlossen ist%2C erh%E4ltst Du eine Email mit dem Download-Link"));
		} else
		if(link=="login")
		{
			this.closeDownloadBox();
			alert("Du musst registriert und eingeloggt sein, um ein video herunterzuladen.");
		} else
		if(link=="")
		{
			this.closeDownloadBox();
			alert("Es ist leider ein Fehler aufgetreten, dieses Video kann im moment nicht heruntergeladen werden.");
		} else
		{
			window.location.href = appRoot+"/UserDownload.do?" +link;
		}
	},
	openDownloadBox : function(appRoot,newContentId)
	{
		if(this.contentId != newContentId)
		{
			this.appRoot = appRoot;
			this.contentId = contentId;
		}
		this.backgroundDiv.style.visibility = "visible";
		this.overlayDiv.style.visibility = "visible";
	},
	closeDownloadBox : function()
	{
		this.backgroundDiv.style.visibility = "hidden";
		this.overlayDiv.style.visibility = "hidden";
	}
};
var downloadBoxInstance = null;
function openDownloadBox(appRoot,contentId){
	if(downloadBoxInstance==null)
		downloadBoxInstance= new downloadBox(appRoot,contentId);
	downloadBoxInstance.openDownloadBox(appRoot,contentId);
};
