$(document).ready(function(){
	var paginaActual=QS('p');
	$("img[@class=menu]").mouseover(function(){
		//console.log($(this).attr("src"));

			$(this).attr("src",$(this).attr("src").replace(/.jpg/g, "_hover.jpg"));

	});
	
	$("img[@class=menu]").mouseout(function(){
		//console.log($(this).attr("src"));

			$(this).attr("src",$(this).attr("src").replace(/_hover.jpg/g, ".jpg"));

	});

	if(paginaActual){
		if(paginaActual!='360'){
			$('#'+paginaActual).html('<img src="img/menu/'+paginaActual+'_selected.jpg"/>');
		}
	}else{
		$('#home').html('<img src="img/menu/home_selected.jpg"/>');
	}
	
	//activar scroll
	$('#nucleo_central').jScrollPane({showArrows:true});
	
	if(!paginaActual || paginaActual=="home"){
		init();
	}

});

function QS(variable) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == variable) {
			return ft[1];
		}
	}
}

function implode(myArray,mySeparator){
	var returnString='';
	for(var i in myArray){
		returnString+=myArray[i]+'|';
	}
	return returnString.substr(0,returnString.length-1);
}

function explode(myString,mySeparator){
	return myString.split(mySeparator);
}

function c(str){
	//console.log(str);	
}

function setArrays(img_array,receptacles_array){
	var max_img_per_receptacle=(Math.floor(img_array.length/receptacles_array.length));
	var imgs_left = img_array.length - (max_img_per_receptacle * receptacles_array.length);
	var img_attr_rec=new Array(receptacles_array.length);
	var cnt_fotos=0;
	for(var i in receptacles_array){
		for(j=0;j<max_img_per_receptacle;j++){
			var elements_holder = new Array();
			elements_holder[0]=receptacles_array[i];
			elements_holder[1]=img_array[cnt_fotos];
			img_attr_rec[cnt_fotos++]=elements_holder;
			//c("i="+i+" j="+j);
		}
	}
	
	for(k=0;k<imgs_left;k++){
		var elements_holder = new Array();
		elements_holder[0]=receptacles_array[k];
		elements_holder[1]=img_array[cnt_fotos];
		img_attr_rec[cnt_fotos++]=elements_holder;
		//c("i="+i+" j="+j);
	}
	//c(img_attr_rec);
	return img_attr_rec;
}

var fade_duration = 3000;
window.goOn=true;
function chngImg(element1, element2, imgarray, nextimgid, nextelement){
	//c('chngImg('+element1+', '+element2+', '+imgarray+', '+nextimgid+', '+nextelement+')');
	imgarray=explode(imgarray,"|")
	if(nextimgid=='' || nextimgid>=imgarray.length){
		nextimgid=0;
		//c("reset");
	}
	if(nextelement=='1'){
		//c("elemento="+nextelement);
		$('#'+element2).css("background-image","url("+imgarray[nextimgid]+")");
		$('#'+element1).fadeOut(fade_duration);		
		$('#'+element2).fadeIn(fade_duration);
		setTimeout('chngImg("'+element1+'","'+element2+'", "'+implode(imgarray,"|")+'", "'+(++nextimgid)+'", "'+(nextelement=='1'?'0':'1')+'")',Math.round(Math.random()*5000)+5000);
	}else{
		//c("elemento="+nextelement);
		$('#'+element1).css("background-image","url("+imgarray[nextimgid]+")");
		$('#'+element2).fadeOut(fade_duration);		
		$('#'+element1).fadeIn(fade_duration);
		setTimeout('chngImg("'+element1+'","'+element2+'", "'+implode(imgarray,"|")+'", "'+(++nextimgid)+'", "'+(nextelement=='1'?'0':'1')+'")',Math.round(Math.random()*5000)+5000);
	}
}

function chngBigImg(element1, element2, imgarray, nextimgid, nextelement){
	//c('chngBigImg('+element1+', '+element2+', '+imgarray+', '+nextimgid+', '+nextelement+')');
	imgarray=explode(imgarray,"|")
	if(nextimgid=='' || nextimgid>=imgarray.length){
		nextimgid=0;
		//c("reset");
	}
	if(nextelement=='1'){
		//c("elemento="+nextelement);
		if(window.goOn) {
			$('#'+element2).css("background-image","url("+imgarray[nextimgid]+")");
			$('#'+element1).css("z-index","1");
			$('#'+element2).css("z-index","2");
			$('#'+element1).fadeOut(fade_duration);		
			$('#'+element2).fadeIn(fade_duration);
		}
		setTimeout('chngBigImg("'+element1+'","'+element2+'", "'+implode(imgarray,"|")+'", "'+(++nextimgid)+'", "'+(nextelement=='1'?'0':'1')+'")',5000);
	}else{
		//c("elemento="+nextelement);
		if(window.goOn) {
			$('#'+element1).css("background-image","url("+imgarray[nextimgid]+")");
			$('#'+element2).css("z-index","1");
			$('#'+element1).css("z-index","2");
			$('#'+element2).fadeOut(fade_duration);		
			$('#'+element1).fadeIn(fade_duration);
		}
		setTimeout('chngBigImg("'+element1+'","'+element2+'", "'+implode(imgarray,"|")+'", "'+(++nextimgid)+'", "'+(nextelement=='1'?'0':'1')+'")',5000);
	}
}



function init(){
	var varray=setArrays(imgs,receptacles);
	
	for(var i in receptacles){
		var imgArray=Array();
		imgArrayNum=0;
		for(var j in varray){
			if(receptacles[i]==varray[j][0]) imgArray[imgArrayNum++]=varray[j][1];
		}
		chngImg(receptacles[i]+"_e1", receptacles[i]+"_e2", implode(imgArray,"|"), "","1");
		
		//colocar as imagens pequenas clicaveis
		$("#"+receptacles[i]+"_e1").css("cursor","pointer");
		$("#"+receptacles[i]+"_e2").css("cursor","pointer");
		$("#"+receptacles[i]+"_e1").click(function(){
			if($(this).css("background-image").indexOf("small_cinza")==-1){
				$("#imgbig1").css("background-image",$(this).css("background-image").replace(/small/,"big"));
				$("#imgbig2").css("background-image",$(this).css("background-image").replace(/small/,"big"));
				window.goOn=false;
				setTimeout('makeItGo()',10000);
			}
		});
		
		$("#"+receptacles[i]+"_e2").click(function(){
			if($(this).css("background-image").indexOf("small_cinza")==-1){
				$("#imgbig1").css("background-image",$(this).css("background-image").replace(/small/,"big"));
				$("#imgbig2").css("background-image",$(this).css("background-image").replace(/small/,"big"));
				window.goOn=false;
				setTimeout('makeItGo()',10000);
			}
		});
	}
	
	//imagem grande
	chngBigImg("imgbig1", "imgbig2", implode(imgsbig,"|"), "",'1');
	
	setTimeout('fadeCover()',10000);
}

function fadeCover(){
	$('#imgbigCOVER').fadeOut(3000);
}

function makeItGo(){
	window.goOn=true;
}

function showWindow(str){
	$.prompt(str);	
}

function captchaRenew(){
	$('#captcha_img').attr("src","captcha.php?seed="+Math.random());
}

var lastDiv='';
function showDiv(id){
	if(lastDiv) $('#'+lastDiv).hide();
	if(lastDiv!=id){
		lastDiv=id;
		$('#'+id).slideDown(1000);
		
	}
}

var lastNoticia='';
function showNoticia(id){
	if(lastNoticia){
		$('#corpo'+lastNoticia).hide();
		$('#expand'+lastNoticia).show();
		$('#introducao'+lastNoticia).show();
	}
	if(lastNoticia!=id){
		$('#expand'+id).hide();
		$('#introducao'+id).hide();
		$('#corpo'+id).slideDown(1000,function(){
			$('#nucleo_central').jScrollPane({showArrows:true});
		});
		lastNoticia=id;
	}
}

var lastId=0;
function changeImage(imagens,way){
	if(way=='fwd'){
		lastId++;
		if(lastId>=imagens.length){
			//showWindow(lastId+' - '+imagens.length);
			lastId=0;	
		}
		//showWindow(imagens[lastId]);
		$('#imagem').fadeOut(500,function(){;
										  c(lastId);
			$('#imagem').attr("src",imagens[lastId]);
			$('#imagem').fadeIn(500);
		});
	}else{
		lastId--;
		if(lastId<0){
			//showWindow(lastId+' - '+imagens.length);
			lastId=imagens.length-1;	
		}
		//showWindow(imagens[lastId]);
		$('#imagem').fadeOut(500,function(){;
										  c(lastId);
			$('#imagem').attr("src",imagens[lastId]);
			$('#imagem').fadeIn(500);
		});
	}
}

jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}