// JavaScript Document
	var myAccordion;
	var timeout=0;
	var main_array;
	
	var tela_efecto;
	var tela_contenido_efecto;
	var tela_html;

	//------------------------------------------------------------------------------------------------------------------------- PAG.PRINCIPAL
	
	function inicio() {
		menu_array = new Array(); 
		main_array = new Array();
		
		new Request.JSON({
			url: '/scripts/proceso_comun.php?op=menu_load', 
			onComplete: function(response) {
				menu_array = response;
				menu_llenar();
			} 
		}).get();
		
		new Request.JSON({
			url: '/scripts/proceso_comun.php?op=main_load', 
			onComplete: function(response) {
				main_array = response;
				main_llenar();
			} 
		}).get();
		
		tela_init();
	}
	
	//----------------------------------------------------------------------
	
	function main_llenar() {
		var aux_array = main_array;
		var faltantes = 0;
		var columnas = 0;

		$('contenido').empty();
		
		if ( (aux_array.length/4) > 3 ) {
			faltantes =  (Math.round(aux_array.length+2/4) * 4) - aux_array.length;
		} else {
			faltantes =  12 - aux_array.length;
		}
		for ( i=0; i<faltantes; i++ ) {
			aux_array.splice( random_num(0, aux_array.length), 0, {'nombre':'', 'foto':''});
		}
		//$('tabla_main').setStyle('width', (columnas*250) );
		for (i=0; i<aux_array.length; i++) {
			$('contenido').grab(
				contenedor({
					id: 'thumb',
					obj: aux_array[i],
					path: '/proyectos/'+ aux_array[i].id +'/',
					contStyle: {'width':'248px', 'height':'148px', 'overflow':'hidden', 'margin':'1px' },
					imgStyle: {'filter':'alpha(opacity=0)', 'opacity':'0', 'width':'248px', 'height':'148px' },
					booImgBack: false,
					imgLink: 'javascript:click_proy('+ aux_array[i].id +')',
					imgClass: 'mini'
				})
			);
		}
		add_effect('.mini');
	}
	
	//----------------------------------------------------------------------
	
	function contenedor( param ) {
		param.booText = (typeof(param.booText)!='undefined')?(param.booText):(false);
		param.booTextVacio = (typeof(param.booTextVacio)!='undefined')?(param.booTextVacio):(true);
		param.booHandle = (typeof(param.booHandle)!='undefined')?(param.booHandle):(false);
		param.booImgBack = (typeof(param.booImgBack)!='undefined')?(param.booImgBack):(true);
		param.imgLink = (typeof(param.imgLink)!='undefined')?(param.imgLink):('');
		param.close = (typeof(param.close)!='undefined')?(param.close):('');
		param.imgUp = (typeof(param.imgUp)!='undefined')?(param.imgUp):(false);
		
		var tabla, row, img;
		var div_container = new Element('div', {'id':param.id, 'class':'thumb_container'});
		//$("log").set('html', $("log").get('html') +'<br>-'+ param.obj.foto +'- '+param.booImgBack);
		if ( param.obj.foto != '' ) {
			if ( param.booImgBack ) {
				div_container.setStyle('background-image', 'url('+ param.path+param.obj.foto +')');
			} else {
				img = new Element('img', {'id':'img_'+param.id, 'src' : param.path+param.obj.foto});
				if ( typeof(param.imgStyle)!='undefined' ) {
					img.setStyles(param.imgStyle);
				}
				if ( typeof(param.imgClass)!='undefined' ) {
					img.set('class', param.imgClass);
				}
				if ( param.imgLink != '' ) {
					div_container.grab( new Element('a', {'href' : param.imgLink}).grab( img ) );
				} else {
					div_container.grab( img );
				}
			}
		}
		if ( typeof(param.contStyle)!='undefined' ) {
			div_container.setStyles(param.contStyle);
		}
		if ( (param.booText) && ((param.booTextVacio) || (!param.booTextVacio && (param.obj.nombre!=''))) ) {
			row = new Array();
			tabla = new HtmlTable({properties: {'width':'100%', 'height':'35px', 'cellspacing':'4px'}});
			row.push( new Element('span', {'id': 'text_'+param.id, 'class':'thumb_edit', 'html': param.obj.nombre}) );
			tabla.push(row);
			div_container.grab( new Element('div', {'class' : 'thumb_text_back'}) );
			div_container.grab(  new Element('div', {'class' : 'thumb_text_front'}).grab( tabla ));
		}
		if ( param.booHandle ) {
			div_container.grab( new Element('div', {'class' : 'thumb_handle'}) );
		}
		if ( param.close != '' ) {
			div_container.grab( 
				new Element('div', {'class' : 'thumb_cerrar'}).grab(
					new Element('a', {'href' : param.close }).grab(
						new Element('img', {'src' : '/images/ico/cerrar18.png'})
					)
				)
			);
		}
		if ( param.imgUp ) {
			div_container.grab(
				new Element('div', {'class' : 'thumb_image'}).grab(
					new Element('a', {'id' : 'img_'+ param.id, 'class':'thumb_image_up', 'href' : '#'}).grab(
						new Element('img', {'src' : '/images/ico/foto18.png'})
					)
				)
			);
		}
		return div_container;
	 }
	 
	 
	 
	 
	//------------------------------------------------------------------------------------------------------------------------- TELA
	
	function tela_init() {
		tela_efecto = new Fx.Morph($('tela_back'), { duration: 300 });
		tela_contenido_efecto = new Fx.Morph($('tela_front'), { duration: 300 });
	}

	function efecto_tela(abrir) {
		if ( abrir ) {
			tela_efecto.start({'height': $('main_todo').getStyle('height') });
			tela_contenido_efecto.start({'height': $('contenido').getStyle('height') });
		} else {
			$('tela_front').set('html','');
			tela_efecto.start({'height':0});
			tela_contenido_efecto.start({'height':0});
		}
	}
	
	
	//------------------------------------------------------------------------------------------------------------------------- scroll

	var step = 1;
	
	function scrollDown(id){ 
		clearTimeout(timeout);
		document.getElementById(id).scrollTop+=step;
		timeout = setTimeout("scrollDown('"+id+"')",10);
	}  
	
	function scrollUp(id){ 
		clearTimeout(timeout);
		document.getElementById(id).scrollTop -= step; 
		timeout = setTimeout("scrollUp('"+id+"')",10);
	} 
	
	function scrollStop(){ 
		clearTimeout(timeout);
	}
	
	//------------------------------------------------------------------------------------------------------------------------- scroll
	
	var scroll_timeout;
	var scroll_slider;
	var scroll_lastvalue;
	
	function scroll_init( xcontent, xscrollbar, horizontal ) {
		clearTimeout(scroll_timeout);
		scroll_slider = null;
		scroll_lastvalue = 0;
		scroll_timeout = setTimeout("scroll_make('"+xcontent+"', '"+xscrollbar+"');", 500);
	}
	
	function scroll_make(xcontent, xscrollbar){
		var ignoreMouse = false;
		var horizontal = true;
		var content = $(xcontent);
		if ( content == null ) {
			clearTimeout(scroll_timeout);
			return;
		}
		var container = $(xscrollbar+"_container");
		var scrollbar = $(xscrollbar+"_range");
		var handle = $(xscrollbar+"_handle");
		var handle_linea = $(xscrollbar+'_handle_linea');
		var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y));
		var inicio = 0;
		
		if ( steps!=0 && steps!=scroll_lastvalue ) {
			scroll_lastvalue = steps;
			container.setStyle('visibility', 'visible');
			if ( scroll_slider != null ) {
				scroll_slider.detach();
				inicio = scroll_slider.step;
			}
			scroll_slider = new Slider(scrollbar, handle, {    
				steps: steps,
				wheel: true,
				mode: (horizontal?'horizontal':'vertical'),
				onChange: function(step){
					var x = (horizontal?step:0);
					var y = (horizontal?0:step);
					content.scrollTo(x,y);
				}
			}).set( inicio );
			if( !(ignoreMouse) ){
				$$(content, scrollbar).addEvent('mousewheel', function(e){  
					e = new Event(e).stop();
					var step = scroll_slider.step - e.wheel * 30;  
					scroll_slider.set(step);               
				});
			}
			//$(document.body).addEvent('mouseleave',function(){scroll_slider.drag.stop()});
			handle_linea.set('morph', {duration: 300});
			handle.addEvent('mouseover', function(e){  
				e = new Event(e).stop();
				handle_linea.morph({'border-bottom-color': '#d6de23'});
			});
			handle.addEvent('mouseout', function(e){  
				e = new Event(e).stop();
				handle_linea.morph({'border-bottom-color': '#FFF'});
			});
		}
		scroll_timeout = setTimeout("scroll_make('"+xcontent+"', '"+xscrollbar+"');", 500);
	}

	//------------------------------------------------------------------------------------------------------------------------- VARIOS
	
	function add_effect(id) {
		var rollover = function(){$(this).tween('opacity', '1');};
		var rollout = function(){$(this).tween('opacity', '.5');};
		$$( id ).each(function(el){
			el.set('tween', {duration: 200});
			el.addEvent('mouseenter', rollover);
			el.addEvent('mouseout', rollout);
			el.fade('.5');
		});
	}
	
	//----------------------------------------------------------------------
	
	function print_r(theObj){
		var resultado="";
		if(theObj.constructor == Array || theObj.constructor == Object){
			resultado+="<ul>";
			for (var p in theObj) {
				if( theObj[p].constructor == Array || theObj[p].constructor == Object){
					resultado+="<li>["+p+"] => "+typeof(theObj)+"</li>";
					resultado+="<ul>";
					print_r( theObj[p] );
					resultado+="</ul>";
				} else {
					resultado+="<li>["+p+"] => "+theObj[p]+"</li>";
				}
			}
			resultado+="</ul>";
		}
		return resultado;
	}
	
	//----------------------------------------------------------------------
	
	function trim(s) {
		return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
	}
	
	//----------------------------------------------------------------------
	
	function random_num(bottom,top){
		return Math.floor(Math.random()*(top-(bottom-1))) + bottom;  
	} 
	


