document.observe('dom:loaded', loadItems);

function loadItems() {
	if ($('soft_featured')) Soft.init();
	if ($('iframe')) Iframe.init();
}

var Iframe = {
	init: function() {
		$('iframe').style.height = (AjaxFunctions.windowHeight() - 100) + 'px';
	}
}


var AjaxFunctions = {
	loader: null,
	overlay: null,
	holder: null,
	lock: false,

/* forum */	

	login: function(page) {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			if ($('username')) {
				new Ajax.Request('login.html', {
					method: 'post',
					parameters: {username: $('username').getValue(), password: $('password').getValue()},
					onCreate: function() {
						AjaxFunctions.showSplashScreen('popup');
					},
					onComplete: function(transport) {
						if (transport.responseText == 'reload') window.location=window.location;
						else {
							AjaxFunctions.hideSplashScreen();
							AjaxFunctions.showPopUp(transport.responseText);
						}
					}
				})
			} else {
				new Ajax.Request('login.html', {
					onCreate: function() {
						AjaxFunctions.showSplashScreen(page);
					},
					onComplete: function(transport) {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
					}
				})
			}
		}
	},
	
	register: function() {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			if ($('password2')){
				new Ajax.Request('register.html', {
					method: 'post',
					parameters: {
						username: $('username').getValue(),
						password: $('password').getValue(),
						password2: $('password2').getValue(),
						mail: $('mail').getValue()},
					onCreate: function() {
						AjaxFunctions.showSplashScreen('popup');
					},
					onComplete: function(transport) {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
					}
				})
			} else {
				new Ajax.Request('register.html', {
					onCreate: function() {
						AjaxFunctions.showSplashScreen();
					},
					onComplete: function(transport) {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
					}
				})
			}
		}
	},
	
	logout: function() {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			new Ajax.Request('logout.html', {
				onCreate: function() {
					AjaxFunctions.showSplashScreen();
				},
				onComplete: function(transport) {
					if (transport.responseText.match(/^location=/)) window.location=transport.responseText.match(/http.*/);
					else {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
					}
				}
			})
		}
	},
	
	newTopic: function(category_id) {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			if (!category_id) {
				new Ajax.Request('forum/newtopic.html', {
					method: 'post',
					parameters: {category_id: $('category_id').getValue(), title: $('title').getValue(), content: $('textarea').getValue()},
					onCreate: function() {
						AjaxFunctions.showSplashScreen('forummessage');
					},
					onComplete: function(transport) {
						if (transport.responseText.match(/^location=/)) window.location=transport.responseText.match(/http.*/);
						else {
							AjaxFunctions.hideSplashScreen();
							AjaxFunctions.showPopUp(transport.responseText);
							Forum.init('textarea');
						}
					}
				})
			} else {
				new Ajax.Request('forum/newtopic.html', {
					method: 'post',
					parameters: {category_id: category_id},
					onCreate: function() {
						AjaxFunctions.showSplashScreen();
					},
					onComplete: function(transport) {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
						Forum.init('textarea');
					}
				})
			}
		}
	},

	newPost: function(topic_id, post_id) {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			if (!topic_id) {
				new Ajax.Request('forum/newpost.html', {
					method: 'post',
					parameters: {topic_id: $('topic_id').getValue(), content: $('textarea').getValue()},
					onCreate: function() {
						AjaxFunctions.showSplashScreen('forummessage');
					},
					onComplete: function(transport) {
						if (transport.responseText.match(/^location=/)) window.location=transport.responseText.match(/http.*/);
						else {
							AjaxFunctions.hideSplashScreen();
							AjaxFunctions.showPopUp(transport.responseText);
							Forum.init('textarea');
						}
					}
				})
			} else {
				new Ajax.Request('forum/newpost.html', {
					method: 'post',
					parameters: {topic_id: topic_id, post_id: post_id},
					onCreate: function() {
						AjaxFunctions.showSplashScreen();
					},
					onComplete: function(transport) {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
						Forum.init('textarea');
					}
				})
			}
		}
	},

	editPost: function(post_id) {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			if (!post_id) {
				new Ajax.Request('forum/editpost.html', {
					method: 'post',
					parameters: {post_id: $('post_id').getValue(), content: $('textarea').getValue()},
					onCreate: function() {
						AjaxFunctions.showSplashScreen('forummessage');
					},
					onComplete: function(transport) {
						if (transport.responseText.match(/^location=/)) window.location=transport.responseText.match(/http.*/);
						else {
							AjaxFunctions.hideSplashScreen();
							AjaxFunctions.showPopUp(transport.responseText);
							Forum.init('textarea');
						}
					}
				})
			} else {
				new Ajax.Request('forum/editpost.html', {
					method: 'post',
					parameters: {post_id: post_id},
					onCreate: function() {
						AjaxFunctions.showSplashScreen();
					},
					onComplete: function(transport) {
						AjaxFunctions.hideSplashScreen();
						AjaxFunctions.showPopUp(transport.responseText);
						Forum.init('textarea');
					}
				})
			}
		}
	},

	comment: function() {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			new Ajax.Request('comment.html', {
				method: 'post',	parameters: {
					form: $('comments').serialize()
				},
				onCreate: function() {
					AjaxFunctions.showSplashScreen();
				},
				onComplete: function(transport) {
					AjaxFunctions.hideSplashScreen();
					AjaxFunctions.showPopUp(transport.responseText);
				}
			})
		}
	},
	
	saveprofile: function() {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			new Ajax.Request('saveprofile.html', {
				method: 'post',	parameters: {
					form: $('editprofile').serialize()
				},
				onCreate: function() {
					AjaxFunctions.showSplashScreen();
				},
				onComplete: function(transport) {
					AjaxFunctions.hideSplashScreen();
					AjaxFunctions.showPopUp(transport.responseText);
				}
			})
		}
	},
	
	avatar: function() {
		if (!AjaxFunctions.lock) {
			AjaxFunctions.lock = true;
			new Ajax.Request('avatar.html', {
				onCreate: function() {
					AjaxFunctions.showSplashScreen();
				},
				onComplete: function(transport) {
					AjaxFunctions.hideSplashScreen();
					AjaxFunctions.showPopUp(transport.responseText);
				}
			})
		}		
	},
	
/* end forum */

	showPopUp: function(data) {
		if (!AjaxFunctions.overlay) {
			AjaxFunctions.overlay = document.createElement('div');
			AjaxFunctions.overlay.setAttribute('id', 'ajaxoverlay');
			AjaxFunctions.overlay.style.display = 'none';
			document.body.appendChild(AjaxFunctions.overlay);
			$('ajaxoverlay').style.width = AjaxFunctions.windowWidth()+'px';
			$('ajaxoverlay').style.height = AjaxFunctions.windowHeight()+'px';
			$('ajaxoverlay').style.zIndex = 100;
			if (AjaxFunctions.version() < 7) {
				$('ajaxoverlay').style.position = 'absolute';
				$('ajaxoverlay').style.top = AjaxFunctions.scrollTop()+'px';
			} else {
				$('ajaxoverlay').style.position = 'fixed';
				$('ajaxoverlay').style.top = '0px';
			}
			$('ajaxoverlay').style.left = '0px';
			
			AjaxFunctions.holder = document.createElement('div');
			AjaxFunctions.holder.setAttribute('id', 'ajaxholder');
			AjaxFunctions.holder.style.display = 'none';
			document.body.appendChild(AjaxFunctions.holder);
			$('ajaxholder').style.position = 'absolute';
			$('ajaxholder').style.zIndex = 150;
		}
		$('ajaxoverlay').style.display = 'block';
		$('ajaxholder').innerHTML = data;
		$('ajaxholder').style.display = 'block';
		$('ajaxholder').style.left = Math.round((AjaxFunctions.windowWidth()-$('ajaxholder').offsetWidth)/2)+'px';
		$('ajaxholder').style.top = (AjaxFunctions.scrollTop()+Math.round((AjaxFunctions.windowHeight()-$('ajaxholder').offsetHeight)/2))+'px';
	},
	
	hidePopUp: function() {
		$('ajaxoverlay').style.display = 'none';
		$('ajaxholder').style.display = 'none';
	},
	
	showSplashScreen: function(element) {
		if (!AjaxFunctions.loader) {
			AjaxFunctions.loader = document.createElement('div');
			AjaxFunctions.loader.setAttribute('id', 'ajaxloader');
			AjaxFunctions.loader.style.display = 'none';
			document.body.appendChild(AjaxFunctions.loader);
			var ajaxbar = document.createElement('div');
			ajaxbar.setAttribute('id', 'ajaxbar');
			AjaxFunctions.loader.appendChild(ajaxbar);
		}
		if (!element) {
			AjaxFunctions.loader.width = AjaxFunctions.windowWidth();
			AjaxFunctions.loader.height = AjaxFunctions.windowHeight();
		} else {
			AjaxFunctions.loader.width = $(element).getWidth();
			AjaxFunctions.loader.height = $(element).getHeight();
		}
		$('ajaxloader').style.width = AjaxFunctions.loader.width+'px';
		$('ajaxloader').style.height = AjaxFunctions.loader.height+'px';
		swfobject.embedSWF("/img/preload.swf", "ajaxbar", AjaxFunctions.loader.width, AjaxFunctions.loader.height, "9.0.0", "expressInstall.swf", null, {quality:'high', scale:'noscale', wmode:'transparent', salign:'tl'});
		$('ajaxloader').style.display = 'block';
		$('ajaxloader').style.zIndex = 200;
		if (!element) {
			if (AjaxFunctions.version() < 7) {
				$('ajaxloader').style.position = 'absolute';
				$('ajaxloader').style.top = AjaxFunctions.scrollTop()+'px';
			} else {
				$('ajaxloader').style.position = 'fixed';
				$('ajaxloader').style.top = '0px';
			}
			$('ajaxloader').style.left = '0px';
		} else {
			$('ajaxloader').style.position = 'absolute';
			var poz = $(element).cumulativeOffset();
			$('ajaxloader').style.top = poz.top+'px';
			$('ajaxloader').style.left = poz.left+'px';
		}
	},
	
	hideSplashScreen: function() {
		$('ajaxloader').style.display = 'none';
		AjaxFunctions.lock = false;
	},

	scrollTop: function() {
		var result = window.pageYOffset?window.pageYOffset:0;
		result = (document.documentElement && result == 0)?document.documentElement.scrollTop:result;
		result = (document.body && result == 0)?document.body.scrollTop:result;
		return result;
	},

	windowWidth: function() {
		var result = window.innerWidth?window.innerWidth:0;
		result = (document.documentElement && result == 0)?document.documentElement.clientWidth:result;
		result = (document.body && result == 0)?document.body.clientWidth:result;
		return result;
	},

	windowHeight: function() {
		var result = window.innerHeight?window.innerHeight:0;
		result = (document.documentElement && result == 0)?document.documentElement.clientHeight:result;
		result = (document.body && result == 0)?document.body.clientHeight:result;
		return result;
	},

	version: function() {
		var version = 999;
		if (navigator.appVersion.indexOf("MSIE") != -1) version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		return version;
	}
	
}

var Forum = {
	content: 'content',
	helpline: 'helpline',
	buttons: [
		{id: 'boldtext', title: 'Bold text: [b]text[/b]', opentag: '[b]', closetag: '[/b]', close: null},
		{id: 'italictext', title: 'Italic text: [i]text[/i]', opentag: '[i]', closetag: '[/i]', close: null},
		{id: 'underlinetext', title: 'Underline text: [u]text[/u]',	opentag: '[u]', closetag: '[/u]', close: null},
		{id: 'quotetext', title: 'Quote text: [quote]text[/quote]', opentag: '[quote]', closetag: '[/quote]', close: 'all'},
		{id: 'insertimage', title: 'Insert image: [img]http://image_url[/img]', opentag: '[img]', closetag: '[/img]', close: 'self'},
		{id: 'inserturl', title: 'Insert URL: [url]http://url[/url] or [url=http://url]title[/url]', opentag: '[url]', closetag: '[/url]', close: null},
		{id: 'closetag', title: 'Close open bbCode tag', opentag: null, closetag: null, close: ''}
	],
	opentags: Array(),
	selfclose: false,
	
	init: function(content, helpline) {
		if (content) Forum.content = content;
		if (helpline) Forum.helpline = helpline;
		for (i=0; i<Forum.buttons.length; i++) {
			$(Forum.buttons[i].id).name = i;
			Event.observe($(Forum.buttons[i].id), "mouseover", function() {
				$(Forum.helpline).value = Forum.buttons[this.name].title;
			});
			Event.observe($(Forum.buttons[i].id), "mouseout", function() {
				$(Forum.helpline).value = '';
			});
			Event.observe($(Forum.buttons[i].id), "click", Forum.addTag);
		}
	},
	
	addTag: function() {
		if (Forum.selfclose) {
			Forum.selfclose = false;
			$(Forum.content).value = $(Forum.content).value + Forum.buttons[Forum.opentags.last()].closetag;
			if (Forum.opentags.last() == this.name) {
				Forum.opentags.pop();
				$(Forum.content).focus();
				return;
			} else {
				Forum.opentags.pop();
			}
		}
		if (Forum.opentags.last() == this.name || Forum.buttons[this.name].opentag == null) {
			if (Forum.opentags.last() != 'undefined') {
				$(Forum.content).value = $(Forum.content).value + Forum.buttons[Forum.opentags.last()].closetag;
				Forum.opentags.pop();
			}
		} else if (Forum.buttons[this.name].close == 'all') {
			while (tag = Forum.opentags.pop()) {
				$(Forum.content).value = $(Forum.content).value + Forum.buttons[tag].closetag;
			}
			$(Forum.content).value = $(Forum.content).value + Forum.buttons[this.name].opentag;
			Forum.opentags.push(this.name);
		} else {
			if (Forum.buttons[this.name].close == 'self') Forum.selfclose = true;
			$(Forum.content).value = $(Forum.content).value + Forum.buttons[this.name].opentag;
			Forum.opentags.push(this.name);
		}
		$(Forum.content).focus();
		return;
	}
}

var Soft = {
	timeout: null,
	name: null,
	
	init: function() {
		var items = $('soft_featured').getElementsByTagName('li');
		for (var i = 0; i < items.length; i++) {
			if (Element.childElements(items[i]).length == 2) {
				items[i].name = i;
				Event.observe(items[i], "mouseover", Soft.startCount);
				Event.observe(items[i], "mouseout", Soft.stopCount);
			}
		}
	},

	startCount: function() {
		if (!Soft.timeout) {
			Soft.name = this.name;
			Soft.timeout=setTimeout('Soft.show()', 200);
		}
	},

	stopCount: function() {
		if (Soft.timeout) {
			clearTimeout(Soft.timeout);
			Soft.timeout = null;
		}
	},
	
	show: function() {
		Soft.timeout = null;
		var items = $('soft_featured').getElementsByTagName('li');
		for (var i = 0; i < items.length; i++) {
			if (Element.hasClassName(items[i], 'active')) Element.removeClassName(items[i], 'active');
			if (items[i].name == Soft.name) Element.addClassName(items[i], 'active');
		}
	}
}

function search(url, error) {
	if ($('topsearch').value) {
		window.location = url + $('topsearch').value;
/*	} else if ($('sidesearch').value) {
		window.location = url + $('sidesearch').value;
	} else if ($('bottomsearch').value) {
		window.location = url + $('bottomsearch').value;*/
	} else {
		alert(error);
	}
}

function jumpto(url) {
	window.location = url+$('jumpto').value+'/';
}

function fixImgs(whichId, maxW) {
var pix=document.getElementById(whichId).getElementsByTagName('img');
  for (i=0; i<pix.length; i++) {
    w=pix[i].width;
    h=pix[i].height;
	if(w > 550){
		if (w > maxW) {
		  f=1-((w - maxW) / w);
		  pix[i].width=w * f;
		  pix[i].height=h * f;
		}
	}
  }
}
