document.observe("dom:loaded", function() {	
	var IE6 = ( navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('6.0') > 0 ) ? true : false;
	sIFR.replaceElement("h2", "/files/alamain.swf", "#3D3C3A", null, null, null, 0, 0, 0, 0, null);

	if (($("errorMessage") || $("noticeMessage"))) {
		//if (!(false /*@cc_on || @_jscript_version < 5.7 @*/)) {
			kind = $("errorMessage") ? "errorMessage" : "noticeMessage";
			$(kind).hide();
			Effect.Appear(kind);
			Effect.Fade.delay(5, kind);
		//}
	}
	
	// Create Select/Clear Code Buttons
	if ($("clipboard")) {
		var div = new Element("div");
		
		var title = "select permalink";
		var link = new Element("a", { "href":"javascript:void(0);", "title":title }).update(title);
		
		link.observe("click", function(event){
			selectNode($("permalink"));
		}.bind(this));

		$("clipboard").insert({ bottom:div.insert({ top:link }) });
		link.insert({ after:" | " });
		
		var title = "clear selection";
		var link = new Element("a", { "href":"javascript:void(0);", "title":title }).update(title);
		
		link.observe("click", function(event){
			clearSelection();
		}.bind(this));	

		div.insert({ bottom:link });
	}
		
	// Create Popup-Submenues
	if ($("menu")) {		
		$("menu").select("li").each(function(menu) {	
			switch ($("menu").select("li").indexOf(menu)) {
				case 2:
					var subMenu = new Element("ul", { "class":"sub-menu-content" });
						subMenu.setStyle({ left:menu.positionedOffset()[0] + "px" });	
						subMenu.hide();
		
					menu.insert({ bottom:subMenu });	
									
					var link = new Element("a", { "href":"/content/about" }).update("what about?");
						subMenu.insert({ bottom:new Element("li").insert({ top:link }) });
					var link = new Element("a", { "href":"/content/news" }).update("news");
						subMenu.insert({ bottom:new Element("li").insert({ top:link }) });
					var link = new Element("a", { "href":"/content/feedback", "style":"margin:0" }).update("feedback");
						subMenu.insert({ bottom:new Element("li").insert({ top:link }) });
					
					menu.observe("mouseover", function(event){
						subMenu.show();
						menu.down(0).addClassName("curr");
					}.bind(this));
					
					menu.observe("mouseout", function(event){
						subMenu.hide();
						menu.down(0).removeClassName("curr");
					}.bind(this));
					break;
				case 6:
					var subMenu = new Element("ul", { "class":"sub-menu-archive" });
						subMenu.setStyle({ left:menu.positionedOffset()[0] - menu.getWidth() + 9 + "px" });	
						subMenu.hide();
	
					menu.insert({ bottom:subMenu });	
	
					var link = new Element("a", { "href":"/archive/search/timeline" }).update("timeline");
						subMenu.insert({ bottom:new Element("li").insert({ top:link }) });
					var link = new Element("a", { "href":"/archive/search/cities" }).update("cities");
						subMenu.insert({ bottom:new Element("li").insert({ top:link }) });
					var link = new Element("a", { "href":"/archive/search/users", "style":"margin:0" }).update("users");
						subMenu.insert({ bottom:new Element("li").insert({ top:link }) });
					
					menu.observe("mouseover", function(event){
						subMenu.show();
						menu.down(0).addClassName("curr");
					}.bind(this));
					
					menu.observe("mouseout", function(event){
						subMenu.hide();
						menu.down(0).removeClassName("curr");
					}.bind(this));
					break;
			}
		}.bind(this));
	}
	
	// Form Manipulations
	$$(".field-focus").each(function(fieldfocus) {
		fieldfocus.setStyle({ "color":"#a3a2a2" });
		
		var reset = false;
		
		fieldfocus.observe("focus", function(event){
			fieldfocus.setStyle({ "color":"#3d3c3a" });
			if (!reset && !fieldfocus.hasClassName("w8em")) {
				fieldfocus.value = "";
				reset = true;
			}
		}.bind(this));
	});
	
	// Start Animations
	if (!IE6) { //exclude IE6
		if ($("clouds")) {
			moveClouds();
		}	
		if ($("stars")) {
			animateStarsUp();
		}

		$$(".element-hider").each(function(element) {
			var checkbox = element.previous(0);
			
			checkbox.observe("click", function(event){
				if (!checkbox.checked) {
					create_hider(element);
				} else {
					element.down(0).remove();
				}
			}.bind(this));
			
			if (!checkbox.checked) {
				create_hider(element);
			}
		}.bind(this));
	} //else $$(".introduction").first().insert({ after:new Element("span", ({ "class":"ie6-warning" })).update("NO!") });
});

hide_city_option=function(){
	$$("input.toggleRadio").each(function(radio) {
		var id = radio.identify().split("CityExisting");
		
		if (!radio.checked) {			
			switch(id[1]) {
				case "0":
					create_hider("city-existing");
					break;
			
				case "1":
					create_hider("city-new");
					break;
			}
		}

		radio.observe("click", function(event){
			switch(id[1]) {
				case "0":
					if ($("city-existing").down(0).hasClassName("hider")) {
						$("city-existing").down(0).remove();
						create_hider("city-new");
					}
					break;
			
				case "1":
					if ($("city-new").down(0).hasClassName("hider")) {
						$("city-new").down(0).remove();
						create_hider("city-existing");
					}
					break;
			}
		}.bind(this));
	}.bind(this));
}

write_message=function() {	
	var maxValue = 140;
	
	if ($("writeMessage")) {
		$("writeMessage").observe("keypress", function(textarea){
			var newValue = maxValue - $("writeMessage").value.length;
			$("number").update(newValue);
			
			newValue < 0 ? $("characters").setStyle({ "color":"#ff0000" }) : $("characters").setStyle({ "color":"#000000" }).removeClassName("over");
		}.bind(this));
	}
}

create_hider=function(where) {
	var hider = new Element("div", { "class":"hider" }).setStyle({ "height":"3000px" });
	$(where).insert({ top: hider });
}

selectNode=function(node) {
	var selection, range, doc, win;

	if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined') {
		range = doc.createRange();
		range.selectNode(node);
		selection.removeAllRanges();
		selection.addRange(range);
	} else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())) {
		range.moveToElementText(node);
		range.select();
	}
}

clearSelection=function() {
	if (document.selection) {
		document.selection.empty();
	} else if (window.getSelection) {
		window.getSelection().removeAllRanges();
	}
}

moveClouds=function () {
	new Effect.Move("clouds",{ x: 110, y: 0, mode: "relative", duration: 50});
	new Effect.Move("clouds",{ x: 0, y: 0, mode: "absolute", queue: "end", duration: 50});
	
	setTimeout("moveClouds()", 120000);
}

animateStarsUp=function() {
	new Effect.Fade("stars",{duration: 1, from: 1, to:0.1});
	setTimeout("animateStarsDown()", 2000);
}

animateStarsDown=function() {
	new Effect.Appear("stars",{duration: 1, from: 0.1, to:1});
	setTimeout("animateStarsUp()", 2000);
}

getDateTime=function(offset,server_time,countseconds,where) {
	var securityOffset = 20;
	
	var now = new Date(
		Date.UTC(
		server_time[0],
		server_time[1]-1,
		server_time[2],
		server_time[3],
		server_time[4],
		server_time[5] + countseconds - securityOffset
	) + (parseInt(offset) - 3) * 60 * 60 * 1000);
		
	// Time
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	var time = "" + hours;
		time += ((minutes < 10) ? ":0" : ":") + minutes;
		time += ((seconds < 10) ? ":0" : ":") + seconds;
	$(where).select(".timer").first().update(time);
	
	// Date
	var day = now.getDate();
	var month= now.getMonth()+1;
	var year= now.getYear();
	if ((year > 99) && (year < 1900)) year += 1900;
	
	var date = "" + day;
		date += ((month<10) ? ".0" : ".") + month;
		date += "." + year;
	$(where).select(".dater").first().update(date);
	
	countseconds = countseconds + 1;
	setTimeout("getDateTime(" + offset + ", [" + server_time[0] + ", " + server_time[1] + ", " + server_time[2] + ", " + server_time[3] + ", " + server_time[4] + ", " + server_time[5] + "], " + countseconds + ", '" + where + "')", 1000);
}

function countdown(goal_time,server_time,countseconds) {
	var securityOffset = 20;
	var goalDate = new Date(goal_time[0],goal_time[1]-1,goal_time[2],goal_time[3],goal_time[4],goal_time[5]);
		
	var startDate = new Date(
		Date.UTC(
		server_time[0],
		server_time[1]-1,
		server_time[2],
		server_time[3],
		server_time[4],
		server_time[5] + countseconds - securityOffset
	) - 2 * 60 * 60 * 1000);
			
	if(startDate<goalDate) {
		var years=0, months=0, days=0, hours=0, minutes=0, seconds=0;
	
		while(startDate<goalDate) {
			years++;
			startDate.setFullYear(startDate.getFullYear()+1);
		}
		startDate.setFullYear(startDate.getFullYear()-1);
		years--;
	
		// months
		while(startDate<goalDate) {
			months++;
			startDate.setMonth(startDate.getMonth()+1);
		}
		startDate.setMonth(startDate.getMonth()-1);
		months--;

		// days
		while(startDate.getTime()+(24*60*60*1000)<goalDate) {
			days++;
			startDate.setTime(startDate.getTime()+(24*60*60*1000));
		}

		// hours
		hours=Math.floor((goalDate-startDate)/(60*60*1000));
		startDate.setTime(startDate.getTime()+hours*60*60*1000);

		// minutes
		minutes=Math.floor((goalDate-startDate)/(60*1000));
		startDate.setTime(startDate.getTime()+minutes*60*1000);

		// seconds
		seconds=Math.floor((goalDate-startDate)/1000);
	
		// format
		if(seconds<10) seconds="0"+seconds;
	
		$("countdown").update(minutes + ":" + seconds);

		countseconds = countseconds + 1;
		setTimeout('countdown([' + goal_time[0] + ', ' + goal_time[1] + ', ' + goal_time[2] + ', ' + goal_time[3] + ', ' + goal_time[4] + ', ' + goal_time[5] + '],[' + server_time[0] + ', ' + server_time[1] + ', ' + server_time[2] + ', ' + server_time[3] + ', ' + server_time[4] + ', ' + server_time[5] + '],' + countseconds + ')', 1000);
	} else {
		$("countdown").update("0:00");
		window.location.reload();
	}
}

preload_area=function(where){
	var hider = new Element("div", { "class":"hider", "id":"hider" });
	var top = ( $(where).getHeight()/2 - 20 ) > 200 ? 200 : $(where).getHeight()/2 - 20;
		
	var preloader = new Element("img", { "src":"/img/content/preloader.gif", "alt":"Preloader" });
		preloader.setStyle({ "marginTop":top + "px" });
		$(where).insert({ top: hider.insert({ top: preloader }) });
}