/**
* requires: prototype, dwr
* publicateBox is a class that represents a box for calling an action 
* to publicate the content into a group chosen by a listbox.
* with the method "openpublicateBox" you can create a box(or open it if already avaible)
* the Param is the url that will be called once the send-button is clicked.
* 
* the document needs to have an element with id "contentMain" where it can append itself to
* 
* styles for the following elements are asumed:
* 
* #publicateBoxBackgroundDiv
* #publicateBoxOverlayDiv
*/
var publicateBox = Class.create();
publicateBox.prototype={
	initialize : function(appRoot,publicateActionUrl,exportActionUrl,contentId,uid)
	{
		this.publicateActionUrl = publicateActionUrl;
		this.exportActionUrl = exportActionUrl;
		this.contentId = contentId;
		this.uid = uid;
		this.backgroundDiv = document.createElement("div");
		this.backgroundDiv.id = "publicateBoxBackgroundDiv";
		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 = "publicateBoxOverlayDiv";
		
		this.overlayInnerDiv = document.createElement("div");
		this.overlayInnerDiv.id = "publicateBoxOverlayInnerDiv";

		this.headerText = document.createElement("div");
		this.headerText.innerHTML = unescape("Du kannst das Video hier ver%F6ffentlichen%3A");
		
		this.globalPublicate = document.createElement("div");
		this.globalPublicateCheckbox = document.createElement("input");
		this.globalPublicateCheckbox.type = 'checkbox';
		this.globalPublicateCheckbox.name = 'public';
		this.globalPublicateCheckbox.defaultChecked = false;
		this.globalPublicateCheckbox.value = 'public';
		this.globalPublicateCheckbox.onclick = function(){
			publicateBoxInstance.publicateGlobal.call(publicateBoxInstance);
		}
		this.globalPublicateText = document.createTextNode(unescape("Dieses video ist allgemein verf%FCgbar"));
		this.globalPublicate.appendChild(this.globalPublicateCheckbox);
		this.globalPublicate.appendChild(this.globalPublicateText);
		
		this.waitingText = document.createElement("div");
		this.waitingText.innerHTML = "Bitte warten, die Gruppen werden gesucht...";
		
		this.dropDown = document.createElement("select");
		this.dropDown.name = "publicateBoxDropDown1";
		this.dropDown.className = "publicateBoxDropDown";
		this.dropDown.id = "publicateBoxDropDown1";
		this.dropDown.size = 1;
		
		this.sendButtonLink = document.createElement("a");
		this.sendButtonLink.href = "javascript:void(0)";
		this.sendButton = document.createElement("img");
		this.sendButton.src = appRoot+"/images/btn_veroefffentlichen.gif";
		this.sendButton.onclick = function(){
			publicateBoxInstance.sendRequest.call(publicateBoxInstance);
		};
		this.sendButtonLink.appendChild(this.sendButton);
		
		this.exportDropDown = document.createElement("select");
		this.exportDropDown.name = "publicateBoxDropDown2";
		this.exportDropDown.className = "publicateBoxDropDown";
		this.exportDropDown.id = "publicateBoxDropDown2";
		this.exportDropDown.size = 1;
		
		this.exportButtonLink = document.createElement("a");
		this.exportButtonLink.href = "javascript:void(0)";
		this.exportButton = document.createElement("img");
		this.exportButton.src=appRoot+"/images/btn_exportieren.gif";
		this.exportButton.onclick = function(){
			publicateBoxInstance.checkAndSend.call(publicateBoxInstance);
		};
		this.exportButtonLink.appendChild(this.exportButton);
		
		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(){
			publicateBoxInstance.closePublicateBox.call(publicateBoxInstance);
		}
		this.closeButtonLink.appendChild(this.closeButton);
		
		this.overlayInnerDiv.appendChild(this.headerText);
		this.overlayInnerDiv.appendChild(this.globalPublicate);
		this.overlayInnerDiv.appendChild(this.waitingText);
		this.overlayInnerDiv.appendChild(this.dropDown);
		this.overlayInnerDiv.appendChild(this.sendButtonLink);
		this.overlayInnerDiv.appendChild(this.exportDropDown);
		this.overlayInnerDiv.appendChild(this.exportButtonLink);
		this.overlayInnerDiv.appendChild(this.closeButtonLink);
		
		this.enterDatas = document.createElement("div");
		this.enterDatasHeaderText = document.createElement("div");
		this.enterDatasHeaderText.innerHTML = unescape("Du hast f%FCr diese Platform noch keine Zugangsdaten eingetragen oder sie sind nicht korrekt. Das kannst du hier %E4ndern%3A");
		
		this.enterDatasName = document.createElement("div");
		this.enterDatasPlatformNameText = document.createElement("div");
		this.enterDatasPlatformNameText.innerHTML = "Name:";
		this.enterDatasPlatformNameField = document.createElement("input");
		this.enterDatasPlatformNameField.type = "text";
		this.enterDatasPlatformNameField.name = "name";
		this.enterDatasName.appendChild(this.enterDatasPlatformNameText);
		this.enterDatasName.appendChild(this.enterDatasPlatformNameField);
		
		this.enterDatasPassword = document.createElement("div");
		this.enterDatasPlatformPasswordText = document.createElement("div");
		this.enterDatasPlatformPasswordText.innerHTML = "Passwort:";
		this.enterDatasPlatformPasswordField = document.createElement("input");
		this.enterDatasPlatformPasswordField.type = "password";
		this.enterDatasPlatformPasswordField.name = "password";
		this.enterDatasPassword.appendChild(this.enterDatasPlatformPasswordText);
		this.enterDatasPassword.appendChild(this.enterDatasPlatformPasswordField);		
		
		this.cancelEnterDatasLink = document.createElement("a");
		this.cancelEnterDatasLink.href = "javascript:void(0)";
		this.cancelEnterDatas = document.createElement("img");
		this.cancelEnterDatas.src=appRoot+"/images/btn_abbrechen.gif";
		this.cancelEnterDatas.onclick = function(){
			publicateBoxInstance.hideEnterDatas.call(publicateBoxInstance);
		};
		this.cancelEnterDatasLink.appendChild(this.cancelEnterDatas);
		
		this.submitEnterDatasLink = document.createElement("a");
		this.submitEnterDatasLink.href = "javascript:void(0)";
		this.submitEnterDatas = document.createElement("img");
		this.submitEnterDatas.src=appRoot+"/images/btn_speichern.gif";
		this.submitEnterDatas.onclick = function(){
			publicateBoxInstance.sendEnterDatas.call(publicateBoxInstance);
		};
		this.submitEnterDatasLink.appendChild(this.submitEnterDatas);
		
		this.enterDatas.appendChild(this.enterDatasHeaderText);
		this.enterDatas.appendChild(this.enterDatasName);
		this.enterDatas.appendChild(this.enterDatasPassword);
		this.enterDatas.appendChild(this.cancelEnterDatasLink);
		this.enterDatas.appendChild(this.submitEnterDatasLink);
		
		this.overlayDiv.appendChild(this.overlayInnerDiv);
		
		document.getElementById("contentMain").appendChild(this.backgroundDiv);
		document.getElementById("contentMain").appendChild(this.overlayDiv);
		this.initStates();
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'makeGlobalAvailable',this.contentId,true,this.firstGlobalPublicateCallback);
		
	},
	checkAndSend : function()
	{
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'canExport',
		this.platformData[this.exportDropDown.options.selectedIndex],this.checkExportableCallback);
	},
	checkExportableCallback : function(DataFromServer)
	{
		publicateBoxInstance.checkExportable.call(publicateBoxInstance,DataFromServer);
	},
	checkExportable : function(loginValuesAreSet)
	{
		if(loginValuesAreSet==true)
			this.sendExportRequest();
		else
			this.overlayInnerDiv.appendChild(this.enterDatas);
	},
	hideEnterDatas : function()
	{
		this.overlayInnerDiv.removeChild(this.enterDatas);
	},
	sendEnterDatas : function()
	{
		this.overlayInnerDiv.removeChild(this.enterDatas);
		this.exportButton.disabled = true;
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'setUserPlatformLogin',
		this.platformData[this.exportDropDown.options.selectedIndex],
		this.enterDatasPlatformNameField.value,this.exportUnlockCallback);
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'setUserPlatformPassword',
		this.platformData[this.exportDropDown.options.selectedIndex],
		this.enterDatasPlatformPasswordField.value,this.exportUnlockCallback);
	},
	exportUnlockCallback : function(DataFromServer)
	{
		publicateBoxInstance.exportUnlock.call(publicateBoxInstance);
	},
	exportUnlock : function()
	{
		this.exportButton.disabled = false;
	},
	openPublicateBox : function(appRoot,newPublicateActionUrl,newExportActionUrl,newContentId,newUid)
	{
		if(this.contentId != newContentId)
		{
			this.publicateActionUrl = newPublicateActionUrl;
			this.exportActionUrl = newExportActionUrl;
			this.contentId = newContentId;
			this.uid = newUid;
			this.exportDropDown.options.length = 0;
			this.dropDown.options.length=0;
			this.initStates();
			dwr.engine._execute(ContentManager._path, 'ContentManager', 'makeGlobalAvailable',this.contentId,true,this.firstGlobalPublicateCallback);
		}
		this.backgroundDiv.style.visibility = "visible";
		this.overlayDiv.style.visibility = "visible";
	},
	closePublicateBox : function()
	{
		this.backgroundDiv.style.visibility = "hidden";
		this.overlayDiv.style.visibility = "hidden";
	},
	exportButtonCallbackHandler : function(DataFromServer)
	{
		if(DataFromServer == null)
		{
			var exportedPlatform = this.platformData[this.exportDropDown.options.selectedIndex];
			this.platformData.splice(this.exportDropDown.options.selectedIndex,1);
			this.exportDropDown.remove(this.exportDropDown.options.selectedIndex);
			alert(unescape("Dein Video wird gerade zu "+exportedPlatform+" hochgeladen. Du bekommst sobald es eingestellt wurde eine Email mit der Best%E4tigung."));
		}
		else
			alert(DataFromServer);
	},
	sendButtonCallback : function()
	{
		alert(unescape("wurde hinzugef%FCgt"));
	},
	exportButtonCallback : function(DataFromServer)
	{
		publicateBoxInstance.exportButtonCallbackHandler.call(publicateBoxInstance,DataFromServer);
	},
	sendRequest : function()
	{
		new Ajax.Request(this.publicateActionUrl +"&id="+this.categoryData[this.dropDown.options.selectedIndex].id, 
			{onComplete: this.sendButtonCallback 
			, method: 'post'});
			this.categoryData.splice(this.dropDown.options.selectedIndex,1);
			this.dropDown.remove(this.dropDown.options.selectedIndex);
			this.closePublicateBox.call(publicateBoxInstance);
	},
	sendExportRequest : function()
	{
			dwr.engine._execute(ContentManager._path, 'ContentManager', 'export',
				this.contentId,this.platformData[this.exportDropDown.options.selectedIndex],this.exportButtonCallback);
			this.closePublicateBox.call(publicateBoxInstance);
	},
	categoriesReady : function(DataFromServer)
	{
		this.waitingText.style.visibility = "hidden";
		this.dropDown.disabled = false;
		this.sendButton.disabled = false;
		this.categoryData = DataFromServer;
		for(var i = 0; i < DataFromServer.length;i++)
		{
			this.dropDown.options[i] = document.createElement("option");
			//if(DataFromServer[i].isMember==true)
				this.dropDown.options[i].innerHTML = DataFromServer[i].name;
			//else
			//	this.dropDown.options[i].innerHTML = DataFromServer[i].name +"(Gruppe automatisch beitreten)";
		}
	},
	platformsReady : function(DataFromServer)
	{
		this.platformData = DataFromServer;
		for(var i = 0; i < DataFromServer.length; i++)
		{
			this.exportDropDown.options[i] = document.createElement("option");
			this.exportDropDown.options[i].innerHTML = DataFromServer[i];
		}
	},
	categoriesReadyCallback : function(DataFromServer)
	{
		publicateBoxInstance.categoriesReady.call(publicateBoxInstance,DataFromServer);
	},
	platformsReadyCallback : function(DataFromServer)
	{
		publicateBoxInstance.platformsReady.call(publicateBoxInstance,DataFromServer);
	},
	publicateGlobal : function()
	{
		this.globalPublicateCheckbox.disabled = true;
		var publicate = this.globalPublicateCheckbox.checked;
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'makeGlobalAvailable',this.contentId,publicate,this.makeGlobalAvailableCallback);
	},
	makeGlobalAvailableCallback : function(DataFromServer)
	{
		publicateBoxInstance.makeGlobalAvailbale.call(publicateBoxInstance);
	},
	makeGlobalAvailbale : function()
	{
		this.globalPublicateCheckbox.disabled = false;
		this.switchEnabledState(this.globalPublicateCheckbox.checked);
	},
	switchEnabledState : function(enabled)
	{
		this.dropDown.disabled = !enabled;
		this.sendButton.disabled = !enabled;
		this.exportDropDown.disabled = !enabled;
		this.exportButton.disabled = !enabled;
	},
	initStates : function()
	{
		this.globalPublicateCheckbox.disabled = true;
		this.switchEnabledState(false);
		this.globalPublicateCheckbox.checked = true;
	},
	firstGlobalPublicate : function(DataFromServer)
	{
		this.globalPublicateCheckbox.disabled = false;
		this.switchEnabledState(this.globalPublicateCheckbox.checked);
		this.dropDown.disabled = true;
		this.sendButton.disabled = true;
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'getAllContentCats', this.contentId,this.uid, this.categoriesReadyCallback);
		dwr.engine._execute(ContentManager._path, 'ContentManager', 'getExportablePlatforms',this.platformsReadyCallback);
	},
	firstGlobalPublicateCallback : function(DataFromServer)
	{
		publicateBoxInstance.firstGlobalPublicate.call(publicateBoxInstance,DataFromServer);
	}
};
var publicateBoxInstance = null;
function openPublicateBox(appRoot,publicateActionUrl,exportActionUrl,contentId,uid){
	if(publicateBoxInstance==null)
		publicateBoxInstance= new publicateBox(appRoot,publicateActionUrl,exportActionUrl,contentId,uid);
	publicateBoxInstance.openPublicateBox(appRoot,publicateActionUrl,exportActionUrl,contentId,uid);
};
