/** \file
\section license
The MIT License

Copyright (c) 2010 Yuriy Okhonin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
\brief implements vhfiles editor behaviour on browser side.
@param callback callback function (example new VhFiles(function(html){alert(html);})) callback function gets html as a parameter and shound insert it into content editor. Example for tinymce: \n <div onclick=\" \n new VhFiles(function(htmlToInsert){ \n  $('textarea').tinymce.execCommand('mceInsertContent',false,htmlToInsert); \n }); \n \">button\</div>
*/
function VhFiles(callback) {
	this.Init = function() {
		this.container = document.createElement("DIV");
		this.container.className = "vhfiles";
		document.body.appendChild(this.container);
		this.window = document.createElement("TABLE");
		this.window.className = "vhfiles_window";
		this.container.appendChild(this.window);
		this.Menu = new VhFilesMenu(this);
		this.Content = new VhFilesContent(this);
		}
	this.exitClicked = function() {
		document.body.removeChild(this.container);
		}
	this.uploadClicked = function() {
		alert('upload');
		}
	this.Init();
	this.onInsertToHtml = function(id, jsonValue){ 
		callback(id.replace(/\[/g,"<").replace(/]/g,">"), jsonValue);
		this.exitClicked();
		};
	}

function VhFilesMenu(parentElement) {
	this.exitClicked = function() { this.p.exitClicked();}
	this.uploadClicked = function() { this.p.uploadClicked();}
	this.Append = function(element) {
		this.container.appendChild(element.container);
		}
	this.p = parentElement;
	this.upcontainer = document.createElement("TR");
	this.container = document.createElement("TD");
	this.container.className = "vhfiles_menu";
	this.upcontainer.appendChild(document.createElement("TD"));
	this.upcontainer.appendChild(this.container);
	this.p.window.appendChild(this.upcontainer);
	this.Append(new VhFilesExitButton(this));
	//this.Append(new VhFilesUploadButton(this));
	}

function VhFilesExitButton(parentElement) {
	this.p = parentElement;
	this.container = document.createElement("SPAN");
	this.container.className = "vhfiles_menu_button";
	this.container.innerHTML = "&#160;&#215;&#160;";
	this.container.p = this;
	this.container.onclick = function() {
		this.p.exitClicked();
		}
	this.exitClicked = function() { this.p.exitClicked();}
	}

function VhFilesUploadButton(parentElement) {
	this.p = parentElement;
	this.container = document.createElement("SPAN");
	this.container.className = "vhfiles_menu_button";
	this.container.innerHTML = "Upload";
	this.container.p = this;
	this.container.onclick = function() {
		this.p.uploadClicked();
		}
	this.exitClicked = function() { this.p.uploadClicked();}
	}

function VhFilesContent(parentElement) {
	this.p = parentElement;
	this.container = document.createElement("TR");
	this.container.className = "vhfiles_content";
	this.p.window.appendChild(this.container);
	this.left = document.createElement("TD");
	this.left.className = "vhfiles_content_left";
	this.rightTD = document.createElement("TD");
	this.rightTD.className = "vhfiles_content_right_td";
	this.right = document.createElement("DIV");
	this.right.className = "vhfiles_content_right";
	this.right.style.height = window.innerHeight*0.7+"px";
	window.onresize = function() {
		document.getElementById("vhfiles_content_right").style.height = window.innerHeight*0.7+"px";
		}
	this.right.id = "vhfiles_content_right";
	this.right.p = this;
	this.rightTD.appendChild(this.right);
	this.container.appendChild(this.left);
	this.container.appendChild(this.rightTD);
	this.Upload = new VhFilesUpload(this);
	this.Cloud = new VhFilesCloud(this);
	this.left.appendChild(this.Upload.container);
	this.left.appendChild(this.Cloud.container);
	//alert(window.screen.availHeight*0.8+"px");
	this.onUploaded = function() {
		//alert(this.Upload.vhform);
		this.Upload.vhform.reset();
		new VhAjax(this.Cloud.container, "GET", "vhfiles.php?e=cloud");
		new VhAjax(this.right,"GET","vhfiles.php?e=list");
	//this.rightTD.style.height = window.screenHeight*0.8+"px";
		}
	this.onUploaded();
	//this.rightTD.style.height = window.screenHeight*0.8+"px";
	this.onRemove = function(id) {
		if(confirm("Do you really want to remove this file?")) {
			new VhAjax(this.right,"GET","vhfiles.php?e=list&remove="+id);
			}
		}
	this.onInsertToHtml = function(id, jsonValue) { this.p.onInsertToHtml(id, jsonValue);}
	}

function VhFilesCloud(parentElement) {
	this.p = parentElement;
	this.id = "VhFilesCloud";
	this.container = document.createElement("DIV");
	this.container.className = "vhfiles_cloud";
	this.container.id = "vhfiles_cloud";
	this.container.p = this;
	this.onTagClick = function(tagName) {
		new VhAjax(this.p.right,"GET","vhfiles.php?e=list&tag="+tagName)
		}
	}

function VhFilesUpload(parentElement) {
	this.p = parentElement;
	this.container = document.createElement("DIV");
	this.container.className = "vhfiles_upload";
	this.container.p = this;
	this.container.id="VhFilesUpload";
	this.container.innerHTML = "\
		<form action='vhfiles.php' method='post' target='vhfiles_upload_iframe' enctype='multipart/form-data' onsubmit='this.parentNode.p.onFormSubmit(this)'>\
		<table><tbody>\
			<tr><td colspan='2'>\
			<input type='file' name='file' size='5' style='width:150px;'/>\
			</td></tr>\
			<tr>\
				<td colspan='2'>Tags:</td>\
			</tr>\
			<tr>\
			<td><input type='text' name='tags'/></td>\
			<td><input type='submit' value='&#8658;' name='VhFilesUpload'/></td>\
			</tr>\
		</tbody></table>\
		</form>\
		<iframe id='vhfiles_upload_iframe' name='vhfiles_upload_iframe' style='height:0px; width:0px; display:none;'></iframe>\
		";
	vhforms = this.container.getElementsByTagName("FORM");
	this.vhform = vhforms[0];
	this.onFormSubmit = function(vhform) {
		}
	this.onUploaded = function() {
		this.p.onUploaded();
		}
	}
