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



/** 
 * @info Document on load function
 * 
*/
function DocumentLoad(){ 

	//fixIEborders();
	//fixColumnHeights('content',510);
	//StartGallery();
	autoHideBoxes();
	return true;
}

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

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


function addToFavorites(){
	var urlAddress = APP_URL; 
	var pageName = APP_TITLE; 
	
	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 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();
}
 */
 



/**
 * @info PAGINATION
 * @Author Jonas
 */
function paginateTables(){
	
	$$('.sortable').each(function(table){
		Paginate(table,0);
	});
	
}

var CanAnimateTable = true;
function Paginate(table,gotoPage){
	var itemPerPage = KC_ITEM_PER_PAGE;
	 
	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 itemCount = trArray.length;
	var pageCount = Math.ceil(itemCount/itemPerPage);	
	
	if(pageCount<=1) return true;
 
	
	var tr = document.createElement('tr');
		var td = document.createElement('td');
			td.colSpan = thead.getElementsBySelector('th').length;
			td.innerHTML =((pageCount>6)?pageCount:"")+" Pages: ";
			
			
		//GOTO FIRST PAGE
		if(pageCount>6){
			var b = document.createElement('b');
				b.innerHTML = "[<<]";
				b.id = table.id+"_first";
				b.style.cursor = "pointer";
				b.className = "kc_table_first";
				b.onclick = Function("changePage('"+table.id+"',0)");
			td.appendChild(b);
		}	
		
		//DRAW PAGES
	for(var k=0; k<pageCount; k++){
			var a = document.createElement('a');
				a.innerHTML = "[ "+(k+1)+" ]"; 
				a.id = table.id+"_page_"+k;
				a.className = "kc_table_page";
				a.onclick = Function("changePage('"+table.id+"',"+k+")");
			td.appendChild(a);
	}
		
		//GOTO LAST PAGE
		if(pageCount>6){
			var b = document.createElement('b');
				b.innerHTML = "[>>]";
				b.id = table.id+"_last";
				b.style.cursor = "pointer";
				b.className = "kc_table_last";
				b.onclick = Function("changePage('"+table.id+"',"+(pageCount-1)+")");
			td.appendChild(b);
		}
		
		
		var a = document.createElement('a');
			a.innerHTML = "[ Show All ]";
			a.className = "kc_table_showall";
			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');
	
	var minPage = gotoPage-3;
	var maxPage = gotoPage+3;
	
	if(minPage<=0){
		maxPage+=Math.abs(minPage);
	}
	if(maxPage>=pageCount){
		minPage-=Math.abs(maxPage-pageCount);
	}
	for(var k=0; k<tfootA.length-1; k++){
		if(k<minPage || k>maxPage){
			tfootA[k].hide();
		 }else{
			tfootA[k].show();
		 }
		
		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 ITEM PAGINATION

var itemPerPage = 19;
var itemPageArray = new Array();  
function itemPages(){
	
	itemPageArray = $$('#itemList .pageItem');
	
	
	var itemCount = itemPageArray.length;
	var pageCount=1; 
	
	//paginar
 	for(var k=0; k<itemCount; k++){   
		if((k+1) % itemPerPage == 0 ){  
			var a = document.createElement("A");
				a.innerHTML = pageCount;
				a.className="botPage"
				a.onclick=Function("itemGotoPage("+(pageCount)+"); this.blur();");
				a.id = "item_page_"+pageCount;
				a.href="javascript:"
			$('Pages').appendChild(a);;
			pageCount++;  
		} 
	}
	
	//restantes
	if(itemCount % itemPerPage !=0){ 
		var a = document.createElement("A");
			a.innerHTML = pageCount;
			a.className="botPage"
			a.onclick=Function("itemGotoPage("+(pageCount)+"); this.blur();");
			a.id = "item_page_"+pageCount;
			a.href="javascript:"
		$('Pages').appendChild(a);
	}
	
	
	return itemGotoPage(1);
}

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

function itemGotoPage(gotoPage){
	divPagesHide();
	var itemCount = itemPageArray.length;
	var pageCount=1;
	var itemStack = Array();
	for(var k=0; k<itemCount; k++){   	
		if(pageCount==gotoPage){ 
			itemPageArray[k].show();
		}
		if((k+1) % itemPerPage == 0){ 
			pageCount++; 
		}
	}
	
	$('item_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');
		 
}
  */
 

