Event.observe(window, "load", onLoad);
Event.observe(window, "load", downloadsSession);
Event.observe(window, "load", quizSession);

function onLoad() 
{
	//Pré-carregamento de imagens
	var images = new Array('http://www.universoilhabela.com.br/imgs/titles/menu_colecao_02.png', 'http://www.universoilhabela.com.br/imgs/titles/menu_pegatinho_02.png', 'http://www.universoilhabela.com.br/imgs/titles/menu_questionario_02.png', 'http://www.universoilhabela.com.br/imgs/titles/menu_blog_02.png', 'http://www.universoilhabela.com.br/imgs/titles/menu_comunidade_02.png', 'http://www.universoilhabela.com.br/imgs/titles/menu_downloads_02.png', 'http://www.universoilhabela.com.br/imgs/titles/menu_contato_02.png');
	images.each( function(img)
	{
		var img_load = new Image();
		img_load.src = img;
	});	
	
	Event.observe("btnLogin","click",function(){
		$$(".boxLinksLogar")[0].addClassName('hide');
		$$(".boxLogar")[0].removeClassName('hide');
		$("logAs").value = "normal";
	});

	Event.observe("btnSouQueen","click",function(){		
		$$(".boxLinksLogar")[0].addClassName('hide');
		$$(".boxLogar")[0].removeClassName('hide');
		$("logAs").value = "queen";
	});

	Event.observe("btnLogOk","click",function(){
		new Ajax.Request(rootUrl+"user/login/email/"+$('logEmail').value+"/password/"+$('logPassword').value,{
			parameters: "email="+$('logEmail').value+"&password="+$('logPassword').value,
			onSuccess: function(response){
				data = response.responseText;
				if(data == "success=0"){
					$("logPassword").value = "";
				}else{					
					$$(".boxLogar")[0].update(data);
				}
			}
		});			
	});

    $$('form.post_comment').each(function(elm){
        Event.observe($(elm),'submit', function(evt){            
            var elm = evt.element();
            var formName = $(elm).down('input#comment_name');
            var formEmail = $(elm).down('input#comment_email');
            var formComment = $(elm).down('textarea#comment_text');
            
            if (formName.value == "" || formName.value == formName.title) {
                $(elm).down('div#error-message').empty();
                $(elm).down('div#error-message').update('<p>O nome é obrigatório.</p>');
                evt.stop();
            } else if (formEmail.value == "" || formEmail.value == formEmail.title) {
                $(elm).down('div#error-message').empty();
                $(elm).down('div#error-message').update('<p>O email é obrigatório.</p>');  
                evt.stop();          
            } else if (formComment.value == "" || formComment.value == formComment.title) {
                $(elm).down('div#error-message').empty();
                $(elm).down('div#error-message').update('<p>A mensagem é obrigatória.</p>');
                evt.stop();
            } else {
                return true;
            }            
            return false;
        });
    });
	 
	$$('.txtFocus').each(function(elm){
		Event.observe(elm, 'focus', function(e){
			var element = e.target;
			if (element.value == element.title) {
				element.value = '';
			}
		});
	 })
	 
	 $$('.txtFocus').each(function(elm){
		Event.observe(elm, 'blur', function(e){
			var element = e.target;
			if (element.value == "") {
				element.value = element.title;
			}
		});
	 })	 
	
}


/* DOWNLOADS SESSION ----------------------------------------------------- */
var LAYER_MOVE_TO_X 	= 0;
var LAYER_MOVE_TO_Y 	= 400;
var LAYER_MOVE_DURATION = 1.0;

function downloadsSession()
{
	if ($('downloads'))
	{
		repositionLayers();
		
		//Reposiciona logo w3haus
		$('footer').setStyle({top:'150px'});
		
		//Scraps Layer Buttons
		Event.observe($('open_scraps_layer'), "click", function() {
			animateLayerIn('scraps_layer');
			return false;
		});
		Event.observe($('close_scraps_layer'), "click", function() {
			animateLayerOut('scraps_layer');
			return false;
		});
		
		//Wallpapers Layer Buttons
		Event.observe($('open_wallpapers_layer'), "click", function() {
			animateLayerIn('wallpapers_layer');
			return false;
		});
		Event.observe($('close_wallpapers_layer'), "click", function() {
			animateLayerOut('wallpapers_layer');
			return false;
		});
		
		//Emoticons Layer Buttons
		Event.observe($('open_emoticons_layer'), "click", function() {
			animateLayerIn('emoticons_layer');
			return false;
		});
		Event.observe($('close_emoticons_layer'), "click", function() {
			animateLayerOut('emoticons_layer');
			return false;
		});
	}
}


function repositionLayers() 
{
	//Esconde layers
	$$(".layer").each(function(elm) {
		elm.setStyle({position:'absolute', top:'-450px', left:'90px'});
	});
}

function animateLayerIn( layer ) 
{
	//new Effect.Move(layer, { x:LAYER_MOVE_TO_X, y:LAYER_MOVE_TO_Y, duration:LAYER_MOVE_DURATION, mode:'relative', transition:Effect.Transitions.spring, afterFinish:function() { alert('fim'); } });
	new Effect.Move(layer, { x:LAYER_MOVE_TO_X, y:LAYER_MOVE_TO_Y, duration:LAYER_MOVE_DURATION, mode:'relative', transition:Effect.Transitions.spring });
}

function animateLayerOut( layer ) 
{
	new Effect.Move(layer, { x:LAYER_MOVE_TO_X*-1, y:LAYER_MOVE_TO_Y*-1, duration:LAYER_MOVE_DURATION/2, mode:'relative', transition:Effect.Transitions.sinoidal });
}


/* DOWNLOADS SESSION ----------------------------------------------------- */
function quizSession()
{
	if ($('questionnaire_home')) 
	{
		Event.observe($('concorra_vencedoras'), "click", showHideLayer);
		Event.observe($('close_layer'), "click", showHideLayer);
	}
}

function showHideLayer() {
	if ($('layer_vencedoras').hasClassName('hide')) {
		$('layer_vencedoras').removeClassName('hide');
	}
	else {
		$('layer_vencedoras').addClassName('hide');
	}
}

function hideLayer (){
	if($('dark_layer'))
		$('dark_layer').remove();
	if($('layer'))
		$('layer').remove();
}

function getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	return [pageWidth,pageHeight];
}