var resized = false;
var defaultBackgroundImg = "img/arvore0.jpeg";
var backgroundImg = "img/arvore0.jpeg";
var timer;

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}


$(document).ready(function(){
	$(".hide").hide();
	
    var bversion = detectBrowserVersion();
    if ($.browser.msie && bversion<7){
    	$('body').css('background-image','none');
    	$('body').load("/ie6_index.html");
    	return 1;
    }
	
	
	$(window).resize(function() {
		var h_window = $(window).height();
		var h_menu = $("#logo").height();
		var menuY = ((h_window/2) - (h_menu/2));
		$("#logo").css('top',menuY);
		if (!resized){
			resized = true;
			setBackground($(window).width(),$(window).height(),backgroundImg);
		}else{
			clearInterval(timer);
			timer = setInterval('setBackground('+ $(window).width() +',' + $(window).height() + ',backgroundImg)',500);
		}
		
		if (h_window>700){
			$("#wrapper").css('padding-top',(h_window-600)/2 + 'px');
		}else{
			$("#wrapper").css('padding-top','50px');
		}
	});
	
	$(window).trigger('resize');
	$("#menu a").each(function(){
		if ($(this).attr('href')!='javascript:void(0);' && !$(this).hasClass('noaddress')){
			$(this).address();
		}
	});
    
	$.address.init(function(event){
	
	}).change(function(event) {
        var path = event.value.substr(1,event.value.length-1);
        if (path!="")
        	showUri(path);
        
        selectMenu(path);
    });
	
	$("#logo").click(function(){
		if ($("#imgToHide").css('display') == 'none'){
			$("#imgToHide").show();
		}else{
			$("#imgToHide").hide();
		}
	});
	
});

function detectBrowserVersion(){
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var version = 0;

	if($.browser.msie){
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}

	if($.browser.chrome){
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	
		$.browser.safari = false;
	}

	if($.browser.safari){
		userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}

	if($.browser.mozilla){
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
			userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
			userAgent = userAgent.substring(0,userAgent.indexOf('.'));
			version = userAgent;
		}
		else{
		}
	}
	
	if($.browser.opera){
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}
	return version;
} 


function selectMenu(path){
	var menu = $('#menu a[href|="'+path+'"]');
	menu.trigger('click');
}

function showUri(uri){
	$('#wrapper').fadeOut(500, function() {
		$.get(uri, function(data) {
			  $('#wrapper').html(data);
			  $('#wrapper').fadeIn(500);
			  $.address.title(page_title);
			  if (new_img){
				  if (new_img!=backgroundImg){
					  backgroundImg = new_img;
					  setBackground($(window).width(),$(window).height(),backgroundImg);
				  }
			  }
			});
	});
}

function setBackground(w,h,img){
	clearInterval(timer);
	var params = "action=resizeImage&width=" + w + "&height=" + h + "&file=" + img;
	$.ajax({
		type: "POST",
		url: "actions.php",
		data: params,
		success: function(msg){
			$("#body").css('background-image','url(' + msg + ')');
		}
	});
}
