if(typeof com == "undefined") var com = {};

var com = {};

// Svet
com.Svet = {};
com.Svet = function() { 
	this.launcher = new com.Svet.Launcher();
	this.utils = new com.Svet.Utils();
	this.address = new com.Svet.Address();
	window.Svet = this;
	window.onload = function() {
		this.Svet.run();
	}
}
com.Svet.prototype = {
	run:function() {
		this.launcher.run();
	},
	get:function(str, e) {
		if(!e) {
			e = document.body;
		}
		if(str.indexOf(":") >= 0) {
			var strArr = str.split(":");
			var numOfStr = strArr.length;
			var res = S.get(strArr[0]);
			if(res) {
				for(var i=1; i<numOfStr; i++) {
					if(res) {
						res = res.get(strArr[i]);
					}
				}
			}
			return res;
		} else {
			var res = new com.Svet.Item(this.getById(str), "object");
			if(res.e) {
				return res;
			}
			res = new com.Svet.Item(this.getByName(str, e), "object");
			if(res.e) {
				return res;
			}
			res = new com.Svet.Item(this.getByClass(str, e, new Array()), "array");		
			if(res.e || res.e.length > 0) {			
				return res;
			}
			res = new com.Svet.Item(this.getByTag(str, e, new Array()), "array");		
			if(res.e || res.e.length > 0) {
				return res;
			}
			//alert("Svet.get: missing object '" + str + "'");
			return false;
		}
	},
	getById:function(id) {
		var e = document.getElementById(id);
		if(e) {
			return e;
		} else {
			return false;
		}
	},
	getByName:function(name, e) {
		var result = false;
		if(e.childNodes) {
			var numOfChilds = e.childNodes.length;
			for(var i=0; i<numOfChilds; i++) {
				if(e.childNodes[i].getAttribute && e.childNodes[i].getAttribute("name") == name) {
					return e.childNodes[i];
				}
				if(e.childNodes[i].hasChildNodes && e.childNodes[i].hasChildNodes()) {
					result = this.getByName(name, e.childNodes[i]);
					if(result) {
						return result;
					}
				}
			}
		}
		return result;
	},
	getByClass:function(className, e, arr) {
		if(e.childNodes) {
			var numOfChilds = e.childNodes.length;
			for(var i=0; i<numOfChilds; i++) {
				if(e.childNodes[i].className && e.childNodes[i].className == className) {
					arr.push(e.childNodes[i]);
				}
				if(e.childNodes[i].hasChildNodes && e.childNodes[i].hasChildNodes()) {
					var arr2 = this.getByClass(className, e.childNodes[i], arr);
					arr.concat(arr2);
				}
			}
		}
		return arr;
	},
	getByTag:function(tagName, e, arr) {
		if(e.childNodes) {
			var numOfChilds = e.childNodes.length;
			for(var i=0; i<numOfChilds; i++) {
				if(e.childNodes[i].nodeName && (e.childNodes[i].nodeName == tagName || e.childNodes[i].nodeName == tagName.toUpperCase())) {
					arr.push(e.childNodes[i]);
				}
				if(e.childNodes[i].hasChildNodes && e.childNodes[i].hasChildNodes()) {
					arr = this.getByTag(tagName, e.childNodes[i], arr);					
				}
			}
		}
		return arr;
	},
	createForm:function(action, method) {
		return new com.Svet.Form(action, method, this);
	}
}
// Item
com.Svet.Item = {};
com.Svet.Item = function(e, type) {	
	this.e = e;
	this.type = type;	
	if(this.type == "array" && this.e.length == 0) {
		this.type = "object";
		this.e = false;
	}
	if(this.type == "array" && this.e.length == 1) {
		this.type = "object";
		this.e = e[0];
	}
	
};
com.Svet.Item.prototype = {
	setStyle:function(style, value) {
		alert("style=" + this.e.style["background"]);
		switch(this.type) {
			case "object":
				this.e.style[style] = value;
			break;
			case "array":
				var numOfe = this.e.length;
				for(var i=0; i<numOfe; i++) {
					this.e[i].style[style] = value;
				}
			break;
		}
	},
	getStyle:function(style) {
		switch(this.type) {
			case "object":
				return this.e.style[style];
			break;
			case "array":
				var numOfe = this.e.length;
				var res = "";
				for(var i=0; i<numOfe; i++) {
					res += this.e[i].style[style] + ", ";
				}
				return res;
			break;
		}
	},
	get:function(name) {
		switch(this.type) {
			case "object":
				return S.get(name, this.e);
			break;
			case "array":
				var numOfE = this.e.length;
				var resArray = new Array();
				for(var i=0; i<numOfE; i++) {
					var res = S.get(name, this.e[i]);
					if(res) {
						switch(res.type) {
							case "object":
								resArray.push(res.e);
							break;
							case "array":
								resArray = resArray.concat(res.e);
							break;
						}
					}
				}
				return new com.Svet.Item(resArray, "array");
			break;
		}
	},
	onclick:function(func) {
		switch(this.type) {
			case "object":
				this.e.onclick = func;
			break;
			case "array":
				var numOfE = this.e.length;
				for(var i=0; i<numOfE; i++) {
					this.e[i].onclick = func;
				}
			break;
		}
	},
	onfocus:function(func) {
		switch(this.type) {
			case "object":
				this.e.onfocus = func;
			break;
			case "array":
				var numOfE = this.e.length;
				for(var i=0; i<numOfE; i++) {
					this.e[i].onfocus = func;
				}
			break;
		}
	},
	onblur:function(func) {
		switch(this.type) {
			case "object":
				this.e.onblur = func;
			break;
			case "array":
				var numOfE = this.e.length;
				for(var i=0; i<numOfE; i++) {
					this.e[i].onblur = func;
				}
			break;
		}
	},
	onchange:function(func) {
		switch(this.type) {
			case "object":
				this.e.onchange = func;
			break;
			case "array":
				var numOfE = this.e.length;
				for(var i=0; i<numOfE; i++) {
					this.e[i].onchange = func;
				}
			break;
		}
	},
	nodeName:function() {
		switch(this.type) {
			case "object":
				return this.e.nodeName;
			break;
			case "array":
				var numOfE = this.e.length;
				var res = "";
				for(var i=0; i<numOfE; i++) {
					res += this.e[i].nodeName + ", ";
				}
				return res;
			break;
		}
	},
	filterByAttr:function(attr, match) {
		var regex = new RegExp(match);
		var res = new Array();		
		switch(this.type) {
			case "object":			
				var attrValue = this.e.getAttribute(attr);
				if(attrValue != null) {
					var regexArr = regex.exec(attrValue);
					if(regexArr != null) {
						res.push(this.e);
					}
				}	
			break;
			case "array":
				var numOfE = this.e.length;				
				for(var i=0; i<numOfE; i++) {
					var attrValue = this.e[i].getAttribute(attr);
					if(attrValue != null) {						
						var regexArr = regex.exec(attrValue);
						if(regexArr != null) {
							res.push(this.e[i]);
						}
					}					
				}
			break;
		}
		if(res.length > 0) {
			return new com.Svet.Item(res, "array");
		} else {
			return null;
		}
	},
	setAttribute:function(name, value) {
		switch(this.type) {
			case "object":
				this.e.setAttribute(name, value);
			break;
			case "array":
				var numOfE = this.e.length;
				for(var i=0; i<numOfE; i++) {
					this.e[i].setAttribute(name, value);
				}
			break;
		}
	}
}
// Launcher
com.Svet.Launcher = {};
com.Svet.Launcher = function() {
	this.funcs = Array();
}
com.Svet.Launcher.prototype = {
	add:function(func) {
		this.funcs.push(func);
	},
	run:function() {
		var numOfFuncs = this.funcs.length;
		for(var i=0; i<numOfFuncs; i++) {
			this.funcs[i]();
		}
	}
}
// Address
com.Svet.Address = {};
com.Svet.Address = function() {
	this.location = location;
	this.hash = location.search || location.hash;
	this.flashObj = null;
}
com.Svet.Address.prototype = {
	getQuaryParameter:function(param) {		
		if(this.hash){
			var startIndex = this.hash.indexOf(param +"=");
			var endIndex = (this.hash.indexOf("&", startIndex) > -1) ? this.hash.indexOf("&", startIndex) : this.hash.length;
			if (this.hash.length > 1 && startIndex > -1) {
				return this.hash.substring(this.hash.indexOf("=", startIndex)+1, endIndex);
			}
		}
		return "";
	},
	getPath:function() {
		if(this.hash.indexOf("#") < 0){
			return "";
		} else {			
			if(this.hash.charAt(this.hash.length-1) == "/"){
				return this.hash.slice(2, this.hash.length-1);
			} else {
				return this.hash.slice(2);
			}
		}
	},
	getPathAsArray:function() {	
		if(this.hash.indexOf("#") < 0){
			return "";
		} else {
			this.hash = this.hash.slice(1);
			arr = this.hash.split("/");
			temp = Array();
			for(i=0; i<arr.length; i++){
				if(arr[i] != ""){
					temp.push(arr[i]);
				}
			}
			return temp;
		}
	},
	setPath:function(path) {
		if(this.location) {
			this.hash = path;
			top.location = path;
		}
	},
	last:function(index) {
		var res = String(this.location).split("/");
		return res[res.length-(1 + index)];
	},
	loop:function() {
		if(this.hash != (top.location.search || top.location.hash)) {
			this.hash = (top.location.search || top.location.hash);
			this.flashObj.e.updateAddress(this.getPath());
		}
		setTimeout("loop()", 500);
	},
	setFlashObj:function(id) {
		this.flashObj = S.get(id);
		if(this.flashObj) {
			this.loop();
		} else {
			alert("Wrong flash object ID !!!");
		}
	}
}
// Utils
com.Svet.Utils = {};
com.Svet.Utils = function() {
	
}
com.Svet.Utils.prototype = {
	printAttrs:function(obj) {
		var result = "";
		for(var i in obj) {
			result += i + ", ";
		}
		alert(result);
	},
	browserType:function() {
		return navigator.appName;
		/*
			FF - Netscape
			IE - Microsoft Internet Explorer
			Opera - Opera
			Safari - Netscape
			Chrome - Netscape
		*/
	},
	transformNewLine:function(str) {
		var replaceWith = "<br />";
		str = escape(str);
		for(i=0; i<str.length; i++) {
			if(str.indexOf("%0D%0A") > -1) {
				str = str.replace("%0D%0A", replaceWith);
			} else if(str.indexOf("%0A") > -1) { 
				str = str.replace("%0A", replaceWith);
			} else if(str.indexOf("%0D") > -1) {
				str = str.replace("%0D", replaceWith);
			}
		}
		str = unescape(str);
		return str;
	},
	replace:function(text, str, replaceWith) {
		text = text.split(str);
		text = text.join(replaceWith);
		return text;
	},
	checkEmail:function(email) {
		var AtPos = email.indexOf("@");
		var StopPos = email.lastIndexOf(".");
		if(email == "") {
			return false;
		}
		if(AtPos == -1 || StopPos == -1) {
			return false;
		}
		if(StopPos < AtPos) {
			return false;
		}
		if(StopPos - AtPos == 1) {
			return false;
		}
		return true;
	},
	delegate:function(scope, method) {
		if(arguments.length > 2) {
			var _params = [];
			for(var n = 2; n < arguments.length; ++n) {
				_params.push(arguments[n]);
			}
			return function() { return method.apply(scope, _params); }
		} else {
			return function() { return method.call(scope); }
		}
	}
}
// Form
com.Svet.Form = {};
com.Svet.Form = function(action, method, S) {
	this.action = action;
	this.method = method;
	this.params = new Array();
	this.S = S;
	this.formId = "SvetForm" + Math.floor(Math.random()*10000);
}
com.Svet.Form.prototype = {
	add:function(name, value) {
		this.params.push({name:name, value:value});
	},
	submit:function() {
		var form = document.createElement("FORM");
		form.method = this.method;
		form.action = this.action;
		form.enctype = "multipart/form-data";
		var numOfParams = this.params.length;
		for(var i=0; i<numOfParams; i++) {	
			var input = document.createElement("INPUT");
			input.name = this.params[i].name;
			input.value = this.params[i].value;
			input.type = "hidden";
			form.appendChild(input);
		}
		document.body.appendChild(form);
		form.submit();
	}
}

function loop() {
	S.address.loop();
}

var S = new com.Svet();
