function initPictureSection(){
	currentParameters=document.URL.match(/^.*?\?(.*)$/)[1];
	parseParameters(currentParameters);
	Behaviour.apply();
}
function parseParameters(paramsAddr){
	try{q.selected=paramsAddr.match(/(&|^)selected=(.*?)(&|$)/)[2] || '';}catch(e){}
	try{q.selected_id=paramsAddr.match(/(&|^)selected_id=(.*?)(&|$)/)[2] || '';}catch(e){}
	try{q.show_picture=paramsAddr.match(/(&|^)show_picture=(.*?)(&|$)/)[2] || '';}catch(e){}
}

var q={
 section:'pictures',
 selected:'',
 selected_id:'',
 show_picture:''
 };


var params={
	current:'',
	next:'',
	previous:''
}

var myrules = {
		'#button_previous a' : function(el){
			if (el.href=='javascript:void(0);')
				return;
			params.previous=el.href.match(/(&|^)show_picture=(.*?)(&|$)/)[2] || '';
			el.href='javascript:void(0)';
			el.onclick=function(){
				changeImage(false);
			}
		},
		'#button_return a' : function(el){
			cargaLista(el, false);
		},
		'ul.picture_items_list a':function(el){
			cargaLista(el,true);
		},
		'#button_next a' : function(el){
			if (el.href=='javascript:void(0);')
				return;
			params.next=el.href.match(/(&|^)show_picture=(.*?)(&|$)/)[2] || '';
			el.href='javascript:void(0)';
			el.onclick=function(){
				changeImage(true);
			}
		},
		'#picture_show_picture_img':function(el){
			el.onload=function(){
				scaleImage();
			}
		},
		'.pictures_pic_tn a':function(el){
			var picture=el.href.match(/(&|^)show_picture=(.*?)(&|$)/)[2] || '';
			el.href='javascript:void(0)';
			el.onclick=function(){
				var elem=this.parentNode.parentNode.parentNode;
				var dim=Element.getDimensions(elem.parentNode);
				elem.parentNode.style.height=dim.height+'px';
				new Effect.Fade(elem, {duration:0.5, afterFinish:function(){
					new Ajax.Updater('pictures_loader', 'ajaxdata.php', {
					  method:'get', parameters: { loader: 'picture_data' },
					  onComplete:function(){
						inicializaFoto(picture);
					  }
					});
					}
				});
				
			}
		}
	};
Behaviour.register(myrules);

function cargaLista(el, menu){
	if (el.href=='javascript:void(0);')
		return;
	var type='';
	var id='';
	try{type=el.href.match(/(&|^)selected=(.*?)(&|$)/)[2] || ''}catch(e){}
	try{id=el.href.match(/(&|^)selected_id=(.*?)(&|$)/)[2] || ''}catch(e){}
	var namecompare='pic_'+type+'_'+id;
	el.href='javascript:void(0);';
	
	
	el.onclick=function(){
		var domelement='picture_show';
		if ($('picture_show')==null)
			domelement='pictures_list';
		if (menu){
			$$('ul.picture_items_list li').each(function (f){
				Element.removeClassName(f,'selected');
				if (f.id==namecompare){
					Element.addClassName(f,'selected');	
				}
			});
		}
		
		new Effect.Fade(domelement, {duration:0.5, afterFinish:function(){
			new Ajax.Updater('pictures_loader', 'ajaxdata.php', {
			  method:'get', parameters: { loader: 'picture_list' },
			  onComplete:function(){
				inicializaLista(type, id);
			  }
			});
		}
		});
	}
}



function inicializaFoto(id){
	q.show_picture=id;
	var qtemp=Object.clone(q);
	qtemp.init_show_image=true;
	qtemp.ts=(new Date()).getTime();
	new Ajax.Updater('picture_content', 'ajaxdata.php', {
	  method:'get', parameters: qtemp,
	  onComplete:function(transport){
		$('picture_show').setStyle({
			 opacity: 0.0
		});

	  	$('picture_content').style.height='';
	  	Behaviour.apply();
		updateLangButton();
		new Effect.Appear('picture_show', {duration:0.4,queue:'end'});	
		}
	
	});
}

function inicializaLista(type, selected){
	q.show_picture='';
	q.selected=type;
	q.selected_id=selected;
	var qtemp=Object.clone(q);
	qtemp.init_list_image=true;
	qtemp.ts=(new Date()).getTime();
	new Ajax.Updater('picture_content', 'ajaxdata.php', {
	  method:'get', parameters: qtemp,
	  onComplete:function(transport){
	  	Element.hide('pictures_list');
	  	Behaviour.apply()
		updateLangButton();
		new Effect.Appear('pictures_list');
		}
	});
}

function scaleImage(){
	var imagen=$('picture_show_picture_img');
	var dim=Element.getDimensions('picture_show_picture_img');
	var width=dim.width;
	var height=dim.height;
	var izquierda=(imagen.parentNode.parentNode.offsetWidth/2-(width+70)/2)+'px'
	
	new Effect.Morph('picture_show_picture',{
		beforeStart:function(){
			$('picture_show_picture').style.backgroundImage='none';
			},
		style:'left:'+izquierda+';width:'+(width+((Prototype.Browser.IE)?70:0))+'px;height:'+(height+((Prototype.Browser.IE)?70:0))+'px',
  		duration:0.3,
		queue: 'end'
	});
	new Effect.Appear('picture_show_picture_img',{ duration:0.4,queue: 'end'});
}

function changeImage(advance){
	var loadPic=(advance)?params.next:params.previous;
	q.show_picture=loadPic;
	var auxq=Object.clone(q);
	auxq.get_picture_controls=true;
	auxq.ts=(new Date()).getTime();
	$('picture_dowload_button').href='download.php?picture='+loadPic;
	new Ajax.Request('ajaxdata.php',{
		method:'get',
		parameters:auxq,
		onComplete:function(transport){
			var data=eval(transport.responseText);
			if (data[0]==null)
				Element.hide($$('#button_previous a')[0]);
			else	
				Element.show($$('#button_previous a')[0]);
			if (data[1]==null)
				Element.hide($$('#button_next a')[0]);
			else	
				Element.show($$('#button_next a')[0]);
			params.previous=data[0];
			params.next=data[1];
			try{Element.update('value_current_pic',data[2])}catch(e){};
			try{Element.update('value_total_pic',data[3])}catch(e){};
			Element.update('picture_show_text',data[4]);
			updateLangButton();
		}
	});

	
	
	var dim=Element.getDimensions($('picture_show_picture'));
	$('picture_show_picture').style.width=(dim.width-((Prototype.Browser.IE)?0:70))+'px';
	$('picture_show_picture').style.height=(dim.height-((Prototype.Browser.IE)?0:70))+'px';
	if (Prototype.Browser.IE){
		$('picture_show_picture').style.backgroundPosition=((dim.width/2)-15)+'px '+((dim.height/2)-15)+'px';
	}
	new Effect.Fade('picture_show_picture_img',{ duration:0.4,queue: 'end', afterFinish:function(){
		$('picture_show_picture_img').src='data/fotos/'+loadPic+'w.jpg';
		$('picture_show_picture').style.backgroundImage='url(staticimg/sqc_red.gif)';
	}});
	
}
function updateLangButton(){
	var lang=$('swaplang_action').href.match(/(&|^)(lang=.*?)(&|$)/)[2];
	var href='?';
	

	$H(q).each(function(e){

		href+=e.key+'='+e.value+'&';
	});
	href+=lang;
	$('swaplang_action').href=href;
}
