/** 
 * -------------------------------------- 
 * 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();
	//StartGallery();
	autoHideBoxes();
	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 FocusInput(obj, label) {
	if(obj.value == label) obj.value = "";
}

function BlurInput(obj, label) {
	if(obj.value == "") obj.value = label;
}



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($('Content')){
	
		var height = $('Content').getHeight();
		
		if(height<510){
			$('Content').style.height = "510px";
		}
		
	}
}

/**
 * @info ImageSlider
 * @author Jonas
 *
 
 
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 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);
	}

} 
/**
 * @info PAGINATION
 * @Author Jonas
 */
function paginateTables(){
	
	$$('.sortable').each(function(table){
		Paginate(table,0);
	});
	
}
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;
	
}
// --

 
/**
* @info OFFER PAGINATION
*/
var offerPerPage = 5;
var offerPageArray = new Array();  
function offerPages(){
	
	offerPageArray = $$('#offerList .pageItem');
	
	
	var itemCount = offerPageArray.length;
	var pageCount=1; 
	
	//paginar
 	for(var k=0; k<itemCount; k++){   
		if((k+1) % offerPerPage == 0 ){  
			var a = document.createElement("A");
				a.innerHTML = pageCount;
				a.className="botPage"
				a.onclick=Function("offerGotoPage("+(pageCount)+"); this.blur();");
				a.id = "offer_page_"+pageCount;
				a.href="javascript:"
			$('Pages').appendChild(a);;
			pageCount++;  
		} 
	}
	
	//restantes
	if(itemCount % offerPerPage !=0){ 
		var a = document.createElement("A");
			a.innerHTML = pageCount;
			a.className="botPage"
			a.onclick=Function("offerGotoPage("+(pageCount)+"); this.blur();");
			a.id = "offer_page_"+pageCount;
			a.href="javascript:"
		$('Pages').appendChild(a);
	}
	
	
	return offerGotoPage(1);
}

function divPagesHide(){
	$$('.pageItem').each(function(div){
		div.hide();
	});
	$$('.botPage').each(function(a){
		a.removeClassName('botPageSelected');
	});
	return true;
}

function offerGotoPage(gotoPage){
	divPagesHide();
	var itemCount = offerPageArray.length;
	var pageCount=1;
	var itemStack = Array();
	for(var k=0; k<itemCount; k++){   	
		if(pageCount==gotoPage){ 
			offerPageArray[k].show();
		}
		if((k+1) % offerPerPage == 0){ 
			pageCount++; 
		}
	}
	
	$('offer_page_'+gotoPage).addClassName('botPageSelected');
	
	return true;
}

/**
* @info GALLERY

//init
function kGallery(id_gallery){
	var GalleryName = "Gallery_"+id_gallery;
	var Gallery = $(GalleryName);

	var widthSum = 0;
	var widthCount = 0;
	$$("#"+GalleryName+" a img").each(function(img){
			widthSum+=img.getWidth()
		widthCount++;
	});
	
	$$("#"+GalleryName+" .GalleryContentHolder")[0].style.width = parseInt(widthSum+(widthCount*10))+"px";
	$$("#"+GalleryName+" .GalleryContentHolder")[0].style.marginLeft = "0px";
}
//move left
function kGallery_Left(id_gallery){ 
	var GalleryName = "Gallery_"+id_gallery;
	$(GalleryName).intervalMove = setInterval('kGallery_MoveLeft('+id_gallery+')',50);
	
}
//move right
function kGallery_Right(id_gallery){
	var GalleryName = "Gallery_"+id_gallery;
	$(GalleryName).intervalMove = setInterval('kGallery_MoveRight('+id_gallery+')',50);
	
}
//clear interval
function kGallery_clear(id_gallery){
	var GalleryName = "Gallery_"+id_gallery;
	clearInterval($(GalleryName).intervalMove)
}

//animate move left
function kGallery_MoveLeft(id_gallery){
	var GalleryName = "Gallery_"+id_gallery;
	var step = 5;
	var ContentHolder = $$("#"+GalleryName+" .GalleryContentHolder")[0];
	var CurrentMargin = parseInt(ContentHolder.style.marginLeft.split("px")[0]);
	
	if(CurrentMargin<0){
		ContentHolder.style.marginLeft = parseInt(CurrentMargin+step)+"px";
	} 
}
//animate move right
function kGallery_MoveRight(id_gallery){
	var GalleryName = "Gallery_"+id_gallery;
	var step = 5;
	var ContentHolder = $$("#"+GalleryName+" .GalleryContentHolder")[0];
	var CurrentMargin = parseInt(ContentHolder.style.marginLeft.split("px")[0]);
	
	if(-ContentHolder.getWidth()+500 < CurrentMargin){
		ContentHolder.style.marginLeft = parseInt(CurrentMargin-step)+"px";
	}
}
*/ 


 
 /**
  * @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);
	}
	
}
 */


/**
 * @info Load Dynamic Gallery

 
function StartGallery(){
	if(!$('Gallery')) return false;
	var Gallery = new dw_scrollObj('Gallery', 'GalleryIn', 'GalleryTable');
		Gallery.setUpScrollbar("dragBar", "track", "h", 1, 1);
		Gallery.setUpScrollControls('scrollbar');
		 
}
  */
 

