function $(a) {
	return document.getElementById?document.getElementById(a):undefined;
}
function $$(a) {
	return document.getElementsByTagName?document.getElementsByTagName(a):undefined;
}

var imgs = [];

function load_images() {
	for(var i = 0; i < imgs.length; i++) {
		var img = new Image();
		img.src = imgs[i];
	}
}

var fcks = [];
var FCKeditorAPI;
function fckshow(a, asd) {
	var i, b, c;
	for(i = 1; i < 4; i++) {
		c = i == a;
		b = c ? 'block' : 'none';
		$('ta'+i).style.display = b;
		if(!asd) {
			$('fcktoolbar'+i).style.display = b;
		}
		$('fbtn'+i).className = 'left' + (c ? ' sel' : '');
		if(c) {
			if(FCKeditorAPI !== undefined) {
				FCKeditorAPI.GetInstance('text'+i).Focus();
			}
		}
	}
}
var count = 0;
function FCKeditor_OnComplete( editorInstance ){
	count++;
	if(count == 3) {
		fckshow(1);
	}
}

function fcksubmit() {
	$('f1').submit();
}



var SWFUpload = {
	instances : [],
	count : 0
};

var SWFU = function (settings) {
	var nul = null;
	var x = function (s){return encodeURIComponent(s);};
	
	this.settings = settings;
	this.s = this.settings;
	this.q = [];
	this.n = "SWFUpload_" + SWFUpload.count++;
	this.m = nul;
	SWFUpload.instances[this.n] = this;

	this.i = function () {
		var a = this.s, targetElement, container;
		
		a.file_post_name = 'Filedata';
		a.return_upload_start_handler = this.returnUploadStart;

		if($(this.n) !== nul) {
			return;
		}

		targetElement = $$("body")[0];

		if(targetElement === undefined) {
			return;
		}

		container = document.createElement("div");
		var v = container.style;
		v.width = '1px';
		v.height = '1px';
		v.position = 'absolute';
		v.top = 0;
		v.left = 0;

		targetElement.appendChild(container);
		
		var b = '" /><param name="';
		container.innerHTML = ['<object id="', this.n, '" type="application/x-shockwave-flash" data="', a.flash_url,
				'" width="1" height="1" style="-moz-user-focus:ignore;"><param name="movie" value="', a.flash_url, b, 'bgcolor" value="',
				'#000000', b, 'quality" value="high', b, 'menu" value="false', b, 'allowScriptAccess" value="always', b,
				'flashvars" value="',
				["movieName="+x(this.n),
					"uploadURL="+x(a.upload_url),
					"filePostName="+x(a.file_post_name),
					"fileTypes="+x(a.file_types),
					"fileTypesDescription="+x(a.file_types_description),
					"fileSizeLimit="+x(a.file_size_limit),
					"fileUploadLimit="+x(a.file_upload_limit),
					"fileQueueLimit="+x(a.file_queue_limit)].join("&amp;")
				,'" /></object>'].join("");
	};

	this.w = function () {
		return this.m === nul ? (this.m = $(this.n)) : this.m;
	};
	
	this.c = function (functionName, withTimeout, argumentArray) {
		withTimeout = !!withTimeout || false;
		argumentArray = argumentArray || [];
		
		var slf = this;
		var callFunction = function() {
			var movieElement = slf.w();
			if (typeof(movieElement[functionName]) === "function") {
				switch(argumentArray.length) {
					case 0: return movieElement[functionName]();
					case 1: return movieElement[functionName](argumentArray[0]);
					case 2: return movieElement[functionName](argumentArray[0], argumentArray[1]);
					case 3: return movieElement[functionName](argumentArray[0], argumentArray[1], argumentArray[2]);
				}
			}
		};
		
		if (withTimeout) {
			setTimeout(callFunction, 0);
		} else {
			return callFunction();
		}
	};

	this.selectFile = function () {
		this.c("SelectFile");
	};
	
	this.selectFiles = function () {
		this.c("SelectFiles");
	};

	this.startUpload = function (fileID) {
		this.c("StartUpload", false, [fileID]);
	};

	this.cancelUpload = function (fileID) {
		this.c("CancelUpload", false, [fileID]);
	};

	this.stopUpload = function () {
		this.c("StopUpload");
	};

	this.e = function (handlerName, argumentArray) {		
		if (argumentArray === undefined) {
			argumentArray = [];
		} else if (!(argumentArray instanceof Array)) {
			argumentArray = [argumentArray];
		}
		
		var self = this;
		if (typeof(this.s[handlerName]) === "function") {
			this.q.push(function () {
				this.s[handlerName].apply(this, argumentArray);
			});
			setTimeout(function () {
				self.t();
			}, 0);
		}
	};

	this.t = function () {
		var  f = this.q.shift();
		f.apply(this);
	};

	this.flashReady = function () {
		var movieElement = this.w();
		if (typeof(movieElement.StartUpload) === "function") {
			this.e("swfupload_loaded_handler");
		}
	};

	this.fileQueued = function (file) {
		this.e("file_queued_handler", [this, file]);
	};

	this.uploadStart = function (file) {
		this.e("return_upload_start_handler", [this, file]);
	};

	this.returnUploadStart = function (file) {
		this.c("ReturnUploadStart", false, [true]);
	};

	this.uploadProgress = function (file, bytesComplete, bytesTotal) {
		this.e("upload_progress_handler", [this, file, bytesComplete, bytesTotal]);
	};

	this.uploadComplete = function (file) {
		this.e("upload_complete_handler", [this, file]);
	};

	this.uploadSuccess = function (file, data) {
		this.e("upload_success_handler", [this, file, data]);
	};

	this.i();
};



var uploadingFiles = [];
function addFile(swfu, file) {
	var group = swfu.settings.data_group;
	file.data_group = group;
	file.group = swfu.settings.group_name;

	var holder = document.createElement('div');
	holder.className = 'pic';
	
	var progress = document.createElement('div');
	progress.className = 'progress';
	progress.style.width = '0%';

	holder.appendChild(progress);

	file.holder = holder;
	file.progress = progress;
	uploadingFiles[file.id] = file;

	if(group.className == 'onlyone') {
		group.removeChild(group.lastChild);
	}
	group.appendChild(holder);
}
function fileQueued(swfu, file) {
	var fname = $('file_name');
	if(fname) {
		fname.innerHTML = file.name;
	}
	addFile(swfu, file);
	swfu.startUpload();
}
function uploadSuccess(swfu, file, data) {
	var filename = swfu.settings.data_name;
	var img = new Image();
	var uFile = uploadingFiles[file.id];
	if(filename == '') {
		filename = uFile.name;
	} else {
		filename += '.jpg';
	}
	img.src = 'data/gallery/'+(uFile.group)+'/thumb/'+filename+'?'+(new Date()).getTime();
	uFile.holder.removeChild(uFile.progress);

	var aimg = document.createElement('a');
	aimg.href = 'data/gallery/'+(uFile.group)+'/'+filename+'?'+(new Date()).getTime();
	aimg.onclick = function() {
		showLightbox(aimg);
		return false;
	};
	aimg.appendChild(img);

	var img1 = document.createElement('div');
	img1.className = 'img';
	img1.appendChild(aimg);
	uFile.holder.appendChild(img1);

/*
	var a = document.createElement('a');
	a.innerHTML = 'Delete';
	a.href = '?id=web&sub=gallery&grp='+(uFile.group)+'&delete='+filename;
	a.onclick = function() {
		return confirm('Delete image?');
	}

	uFile.holder.appendChild(a);
*/
}
function uploadComplete(swfu, file) {
	swfu.startUpload();
}
function uploadProgress(swfu, file, complete, total) {
	if(total > 0) {
		var p = Math.round(100 * complete / total);
		var uFile = uploadingFiles[file.id];
		uFile.progress.style.width = p+'%';
	}
}

var swaphist = [];
function swapshow(a, b) {
	if(swaphist.length == 2) {
		var x = swaphist[0], y = swaphist[1];
		swaphist = [];
		swapshow(y, x);
	}
	$(a).style.display = 'none';
	$(b).style.display = 'block';
	swaphist = [a, b];
}

function init_swfupload(name, group, data) {
	if(data == undefined) {
		data = '';
	}
	var url = 'upload.php?name='+name+'&data='+data;
	var sessid = $('session_id');
	if(sessid) {
		url += '&session_id=' + sessid.innerHTML;
	}

	return new SWFU({
		// Backend settings
		upload_url : url,

		// Flash file settings
		file_size_limit : "5024",
		file_types : "*.jpg;*.png;*.gif",
		file_types_description : "Image files",
		file_upload_limit : (data == '' ? "0" : "1"),
		file_queue_limit : "0",

		// Flash Settings
		flash_url : "swfupload/swfupload_f8.swf",

		file_queued_handler : fileQueued,	
		upload_progress_handler : uploadProgress,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,

		data_group : $(group),
		group_name : name,
		data_name : data
	});
}

window.onload = function() {
	if(!window.opera){try{document.execCommand('BackgroundImageCache',false,true);}catch(ex1){}}
	load_images();
	window.onload = function() {};
};
