var MAIN_CONTAINER_WIDTH = 903;
var LIQUID_MINIMUM_HEIGHT = 0;
var LIQUID_FRONTPAGE_MINIMUM_HEIGHT = 50;

window.addEvent("resize", resizeLiquid);
window.addEvent("load", function() {
	if ($("ContentClientsFlash")) {
		//$("ContentClientsFlash").setStyle("width", 902);
		//$("ContentClientsFlash").setStyle("height", 460);
	}
	resizeLiquid();	
});
window.addEvent("domready", function() {
	// Setup all mouseovers
	$$("img.mouseover").each(function(img) {
		var src = img.getProperty("src");
		img.addEvent("mouseenter", function() { img.setProperty("src", src.replace(".gif", "_active.gif")); });
		img.addEvent("mouseleave", function() { img.setProperty("src", src); });
	});

	if ($("ContentJobsOpen")) {
		$("ContentJobsOpen").addEvent("click", function(e) {
			e.stop();
			$("ContentJobsFormError").setStyle("visibility", "hidden");
			popupOpen2($("ContentJobsForm"), "Avoin hakemus");
		});
		$("ContentJobsFormForm").addEvent("submit", function(e) {
			e.stop();
			$("ContentJobsFormError").setStyle("visibility", "hidden");
			this.set("send", {
				onComplete: function(resp) {
					var fade = new Fx.Tween($("ContentJobsForm"), {duration: 350 });
					
					// Save ok
					if (resp == "saved=1") {
						fade.addEvent("complete", function() {
							popupOpen($("ContentJobsThanks"));
							(function() { popupClose($("ContentJobsThanks")); }).delay(1500);
						});
						fade.start("opacity", 0);
					// Error
					} else {
						$("ContentJobsFormError").setStyle("visibility", "visible");
					}
				}
			});
			this.send();
		});
		$("ContentJobsFormClose").addEvent("click", function() { popupClose($("ContentJobsForm")); });	
	}

	if ($("ContentContactOpen")) {
		$("ContentContactFormForm").addEvent("submit", function(e) {
			e.stop();
			$("ContentContactFormError").setStyle("visibility", "hidden");
			this.set("send", {
				onComplete: function(resp) {
					var fade = new Fx.Tween($("ContentContactForm"), {duration: 350 });

					// Save ok
					if (resp == "saved=1") {
						fade.addEvent("complete", function() {
							popupOpen($("ContentContactThanks"));
							(function() { popupClose($("ContentContactThanks")); }).delay(1500);
						});
						fade.start("opacity", 0);
					// Error
					} else {
						$("ContentContactFormError").setStyle("visibility", "visible");
					}
				}
			});
			this.send();
		});
		$("ContentContactFormClose").addEvent("click", function() { popupClose($("ContentContactForm")); });	
	}

	resizeLiquid();
});

function openContactForm() {
	$("ContentContactFormError").setStyle("visibility", "hidden");
	popupOpen($("ContentContactForm"));
}

function popupOpen(element) {
	var scroll;
	if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scroll = document.body.scrollTop;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scroll = document.documentElement.scrollTop;
	  }

	//var scroll = document.viewport.getScrollOffsets()[1];
	element.setStyle("opacity", 0);
	element.setStyle("top", 200+scroll+"px");
	element.setStyle("display", "block");
	var fade = new Fx.Tween(element, { duration: 350 });
	fade.start("opacity", 1);
}

function popupOpen2(element, head){
	$("formH").innerHTML = head;
	$("hakemuksen_tyyppi").value = head;
	popupOpen(element);
}

function popupClose(element) {
	var fade = new Fx.Tween(element, {duration: 350 });
	fade.start("opacity", 0);
}

// Resize background liquid area
function resizeLiquid() {
	// Calculate new size
	var bodyHeight = $("Body").getSize().y;
	var pageHeight = $("Container").getSize().y + $("Liquid").getSize().y + $("Footer").getSize().y;
	var height = bodyHeight - pageHeight;
	var minimum = $("Liquid").getProperty("class") == "index" ? LIQUID_FRONTPAGE_MINIMUM_HEIGHT : LIQUID_MINIMUM_HEIGHT;
			
	height = height > minimum ? height : minimum;
	
	$("Filler").setStyle("height", height);
	var bodyWidth = $("Body").getSize().x;
	var mw = 913;
	if(bodyWidth < mw) {
		$("Liquid").setStyle("width", mw);
		$("Filler").setStyle("width", mw);
		$("Footer").setStyle("width", mw);
	}
	else {
		$("Liquid").setStyle("width", '100%');
		$("Filler").setStyle("width", '100%');
		$("Footer").setStyle("width", '100%');			
	}
}

var max = 10;
var pointer = max;
var pointer2 = max;
function startFading(){
	setTimeout('fadeElem();', 2000);
}

function fadeElem(){
	pointer2--;
	if (pointer2<1) pointer2 = max;
	$("nauha"+pointer).get('tween', {property: 'opacity', duration: 1000}).start(0);
//fade(0, {duration: 500});
	$("nauha"+pointer2).get('tween', {property: 'opacity', duration: 0}).start(1);//fade(1, {duration: 500});
	pointer--;
	if (pointer<1) pointer = max;
	
	setTimeout('startFading();', 1000);
}
