/** 
 * -------------------------------------- 
 * PORTAL DE ARTISTAS
 * @file: GENERIC JS FUNCTIONS
 * @author: Jonas
 * @date: 24-09-2009
 *
 * --------------------------------------
*/
 
 
 
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function autoHideBoxes(){

	$$('.infoBox').each(function(element){
		element.appear({ duration: 0.8 });
		element.onclick = function(){ this.fade() };
	});
	$$('.errorBox').each(function(element){
		element.appear({ duration: 0.8 });
		element.onclick = function(){ this.fade() };
	});
	$$('.warningBox').each(function(element){
		element.appear({ duration: 0.8 });
		element.onclick = function(){ this.fade() };
	});
	$$('.helpBox').each(function(element){
		element.appear({ duration: 0.8 });
		element.onclick = function(){ this.fade() };
	});
	
	setTimeout(function(){
			$$('.infoBox').each(function(element){
			element.fade({ duration: 2.5 });
			});
			$$('.errorBox').each(function(element){
				element.fade({ duration: 2.5 });
			});
			$$('.warningBox').each(function(element){
				element.fade({ duration: 2.5 });
			});
			$$('.helpBox').each(function(element){
				element.fade({ duration: 2.5 });
			});
		},(15*1000));	
}

/** 
 * @info Document on load function
 * 
*/
function DocumentLoad(){
	//CompileTopMenu();
	//fixIEborders();
	//fixColumnHeights();
	//paginateTables();
	return true;
}
function fixIEborders(){
 
	$$('input[type=checkbox]').each(function(element){
	   element.style.border="";
	   element.up().style.border="";
	});
	
	$$('input[type=radio]').each(function(element){
	   element.style.border="";
	   element.up().style.border="";
	});
}
 
 
function CompileTopMenu(){
	
	$$('.topMenuBot').each(function(element){
		
		var menuID = element.id.split('_')[1];
		$('topMenuSub_'+menuID).hide();
		$('topMenu_'+menuID).onmouseover  = function(){
		
			var menuID = this.id.split('_')[1];
	
			$('topMenuSub_'+menuID).show();
			$('topMenuBot_'+menuID).addClassName('topMenuOver');
			
		}
		
		$('topMenu_'+menuID).onmouseout  = function(){
			var menuID = this.id.split('_')[1];
			$('topMenuSub_'+menuID).hide(); 
			$('topMenuBot_'+menuID).removeClassName('topMenuOver');
		}
		
	});
	
	
}

function fixColumnHeights(){
	if($('ContentRight'))
	var right_height = $('ContentRight').getHeight();
	
	if($('ContentLeft'))
	var left_height = $('ContentLeft').getHeight();
	
	if($('ContentHomepage')){
		var homepage_height = $('ContentHomepage').getHeight();
		if(homepage_height<400 ){
			$('ContentHomepage').style.height = "400px";
		}
	}

	if($('ContentRight'))
	if(left_height<400 && right_height<400){
		$('ContentRight').style.height = "400px";
	}
	
	if($('ContentLeft'))
	if(right_height<left_height){
		$('ContentRight').style.height = left_height+"px";
	} 
	
	
}


var ImageSlider = {
	
	current_image:0,
	number_images:0,
	enabled:true,
	
	start : function(){
		this.number_images = $$('.SliderImage').length;
		this.current_image = 0;
		for(var k=1; k<this.number_images; k++){
			$('SliderImage_'+k).hide();
		}
		
	},
	
	
	next: function(){
		if(this.enabled==false) return false;
		this.enabled = false;
		var next_image = this.current_image+1;
		
		if( next_image == this.number_images){
			next_image = 0;
		}
		
		$('SliderImage_'+next_image).style.zIndex=2;
		$('SliderImage_'+this.current_image).style.zIndex=3;
		$('SliderImage_'+next_image).show();
		Effect.BlindLeft('SliderImage_'+this.current_image );
 
		this.PostAnimation(next_image);
	},
	
	prev: function(){
		if(this.enabled==false) return false;
		this.enabled = false;
		var next_image = this.current_image-1;
		
		if( next_image < 0){
			next_image = this.number_images-1;
		}
		
		$('SliderImage_'+next_image).style.zIndex=3;
		$('SliderImage_'+this.current_image).style.zIndex=2;
		
		Effect.BlindRight('SliderImage_'+next_image  );
		
		setTimeout("$('SliderImage_"+this.current_image+"').hide()",999);
		
		this.PostAnimation(next_image);
		
	},
	
	PostAnimation: function(next_image){ 
		setTimeout("ImageSlider.enabled = true",1000);
		this.current_image = next_image;
	}
}

/**
 * @author Jonas
 * @Info Image Fader Class
 *
 * @Example
	var Fader = new ImageFader('element_id');
	clientFader.Timer = setInterval('Fader.rotateImage()', 10000);
 * 
 */
function ImageFader(idDiv){
	 
	this.element=$(idDiv);
	this.images = this.element.childElements();
	this.currentImage = 0;
	this.Timer;
	
	
	this.init = function(){
		this.images.each(function(img){
			img.id="img_"+Math.random().toString();
			img.hide();
		});
		this.rotateImage();
	}
	
	this.rotateImage = function(){
		//hide current
		if(this.images[this.currentImage]){
			new Effect.Fade(this.images[this.currentImage].id);
		}
		//++
		this.currentImage++;
		//reset?
		if(!this.images[this.currentImage]){
			this.currentImage = 0;
		}
		//show next
		new Effect.Appear(this.images[this.currentImage].id , { queue: 'end' });
	}

	this.init();
}

 
function paginateTables(){
	
	$$('.sortable').each(function(table){
		Paginate(table,0);
	});
	
}



function addToFavorites(){
	var urlAddress = "http://www.e-mission.pt/"; 
	var pageName = "e)mission"; 
	
	if (window.sidebar){ // firefox
		window.sidebar.addPanel(pageName, urlAddress, "");
	}else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',urlAddress);
		elem.setAttribute('title',pageName);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}else if(document.all){// ie
		window.external.AddFavorite(urlAddress, pageName);
	}

} 
/*
var CurrentPage = 0;
var itemPerPage = 3;
var CanAnimateTable = true;
function Paginate(table,gotoPage){
	 
	var thead = table.getElementsBySelector('thead')[0];
	var tbody = table.getElementsBySelector('tbody')[0];
	var tfoot = table.getElementsBySelector('tfoot')[0];
	
	//if(table.id=="") table.id ="table_"+Math.round(Math.random()*10000);
	
	var trArray = tbody.getElementsBySelector('tr');
	
	//var gotoPage = gotoPage;
	var itemCount = trArray.length;
	var pageCount = Math.ceil(itemCount/itemPerPage);	
	
	if(pageCount<=1) return true;
	
  
 
	$('newsLeftBot').onclick = Function("changePage('"+table.id+"',-1)");
	$('newsRightBot').onclick = Function("changePage('"+table.id+"',1)");
			
	
	return changePage(table.id,0);
	
}

function changePage(table,gotoPage){
	var table = $(table);
	
 
	if(!CanAnimateTable) return;
	
	CanAnimateTable = false;
	
	var thead = table.getElementsBySelector('thead')[0];
	var tbody = table.getElementsBySelector('tbody')[0];
	var tfoot = table.getElementsBySelector('tfoot')[0];
	
	var trArray = tbody.getElementsBySelector('tr');
	
	var itemCount = trArray.length;
	var pageCount = Math.ceil(itemCount/itemPerPage);	
 
	if(CurrentPage+gotoPage>=pageCount){
		var gotoPage = CurrentPage;
	}else if(CurrentPage+gotoPage<0){
		var gotoPage = 0;
	}else{
		var gotoPage = CurrentPage + gotoPage;
	}
	
	CurrentPage = gotoPage;
	
	
	for(var k=0; k<itemCount; k++){
		if(gotoPage<0){
			trArray[k].show();
			continue;
		}
		if( k>=(itemPerPage*gotoPage) && k<=(itemPerPage*gotoPage)+itemPerPage ){
			//console.log(":"+k+"=Sim");
			trArray[k].show();
		}else{
			//console.log(":"+k+"=Nao");
			trArray[k].hide();
		}
	}
 
	CanAnimateTable = true;
	
}

 */
 
 
var itemPerPage = 15;
var CanAnimateTable = true;
function Paginate(table,gotoPage){
	 
	var thead = table.getElementsBySelector('thead')[0];
	var tbody = table.getElementsBySelector('tbody')[0];
	var tfoot = table.getElementsBySelector('tfoot')[0];
	
	if(table.id=="") table.id ="table_"+Math.round(Math.random()*10000);
	
	var trArray = tbody.getElementsBySelector('tr');
	
	//var gotoPage = gotoPage;
	var itemCount = trArray.length;
	var pageCount = Math.ceil(itemCount/itemPerPage);	
	
	if(pageCount<=1) return true;
	
	//console.log("de:"+(itemPerPage*gotoPage));
	//console.log("até:"+(itemPerPage*gotoPage)+itemPerPage);
	/*		
	for(var k=(itemPerPage*gotoPage); k<itemCount; k++){
		if( k>=(itemPerPage*gotoPage) && k<=(itemPerPage*gotoPage)+itemPerPage ){
		//console.log(":"+k+"=Sim");
			trArray[k].show();
		}else{
			//console.log(":"+k+"=não");
			trArray[k].hide();
		}
	}
	*/
	
	var tr = document.createElement('tr');
		var td = document.createElement('td');
			td.colSpan = thead.getElementsBySelector('th').length;
			td.innerHTML = "Pages: ";
		
	for(var k=0; k<pageCount; k++){
			var a = document.createElement('a');
				a.innerHTML = "[ "+(k+1)+" ]";
				a.id = table.id+"_page_"+k;
				a.onclick = Function("changePage('"+table.id+"',"+k+")");
			td.appendChild(a);
	}
	
		var a = document.createElement('a');
			a.innerHTML = "[ Show All ]";
			a.onclick = Function("changePage('"+table.id+"',-1)");
		td.appendChild(a);
	
		tr.appendChild(td);
	tfoot.appendChild(tr);
	
	
	return changePage(table.id,0);
	
}

function changePage(table,gotoPage){
	var table = $(table);
	
 
	if(!CanAnimateTable) return;
	
	CanAnimateTable = false;
	
	//var thead = table.getElementsBySelector('thead')[0];
	var tbody = table.getElementsBySelector('tbody')[0];
	var tfoot = table.getElementsBySelector('tfoot')[0];
	
	var trArray = tbody.getElementsBySelector('tr');

	//var gotoPage = gotoPage;
	var itemCount = trArray.length;
	var pageCount = Math.ceil(itemCount/itemPerPage);	
	
	for(var k=0; k<itemCount; k++){
		if(gotoPage<0){
			trArray[k].show(); 
		}else{
			if( k>=(itemPerPage*gotoPage) && k<(itemPerPage*gotoPage)+itemPerPage ){
				//console.log(":"+k+"=Sim");
				trArray[k].show();
			}else{
				//console.log(":"+k+"=Nao");
				trArray[k].hide();
			}
		}
	}
	
	var tfootA = tfoot.getElementsBySelector('a')
	for(var k=0; k<tfootA.length-1; k++){
		if(k==gotoPage){
			tfootA[k].style.fontSize="18px";
			tfootA[k].innerHTML = '[ '+(k+1)+' ]';
		}else{
			tfootA[k].innerHTML = '[ '+(k+1)+' ]';
			tfootA[k].style.fontSize="12px";
		}
	}
	CanAnimateTable = true;
	
}
 
 
 
 /**
  * @info SLIDE SHOW
  */
  
  var SlideShow = null;

function toggleSlideShow(){

	if(!lbox) return false;	
	if($$('#horizontal_carousel a.imgGal').length==0) return false;
	
	if($$('#horizontal_carousel a.imgGal').length==1){
		lbox.start($$('#horizontal_carousel a.imgGal')[0]);
	}else{
		lbox.start($$('#horizontal_carousel a.imgGal')[0]);
		if(!SlideShow);
		SlideShow = setInterval("slideShowTime()",8000);
	}
	
}

function openFirstFoto(){
	if(!lbox) return false;	
	if($$('#horizontal_carousel a.imgGal').length==0) return false;
	lbox.start($$('#horizontal_carousel a.imgGal')[0]);
}

function slideShowTime(){
	
	if(!lbox){
		clearInterval(SlideShow);
		return false;	
	}
	if(lbox.imageArray.length==0){
		clearInterval(SlideShow);
		return false;
	}
	
	if(lbox.imageArray.length-1 == lbox.activeImage){
		//first
		lbox.changeImage(0);
	}else{
		//next
		lbox.changeImage(lbox.activeImage + 1);
	}
	
}
 
