function ObtenerP(nParametro)
{
//Capturamos la URL
var callingURL = document.location.href;


//Separamos los parametros
var cgiString = callingURL.substring(callingURL.indexOf('#')+1,callingURL.length);

// Para manejar control ya que el arreglo empieza en cero restaremos 1 al parametro recibido
nParametro -=1;

// Dividimos mediante la función split los parámetros separados por numeral
var col_array=cgiString.split("#");

var part_num=0;

// Ahora hacemos un recorrido del arreglo y buscamos el # de parametro solicitado!
while (part_num < col_array.length)

{ if ( part_num == nParametro )
		return col_array[part_num];
else
   part_num+=1;
}

// evaluar que devolver cuando no lo
// encuentra...
return 0;    
} 



function ObtenerEstado(nParametro)
{
//Capturamos la URL
var callingURL = document.location.href;

//Separamos los parametros
var cgiString = callingURL.substring(callingURL.indexOf('Mexico')+7,callingURL.length);


// Para manejar control ya que el arreglo empieza en cero restaremos 1 al parametro recibido
nParametro -=1;

// Dividimos mediante la función split los parámetros separados por numeral
var col_array=cgiString.split("/galeria");
var part_num=0;

// Ahora hacemos un recorrido del arreglo y buscamos el # de parametro solicitado!
while (part_num < col_array.length)

{ if ( part_num == nParametro )
		return col_array[part_num];
else
   part_num-=1;
}

// evaluar que devolver cuando no lo
// encuentra...
return 0;    
} 



// Obtención de la URL.
var urlStates = document.location.href ;
// División en trozos con la barra como delimitador.
var partes = urlStates.split('/');
// Obtención del nombre de la página y sus parámetros.
//alert(partes[partes.length-2]); 
var urlEstados;
var estados = partes[partes.length-3].toLowerCase();
var destino = '/'+partes[partes.length-2];  

if (estados=="mexico")
{ urlEstados = partes[partes.length-2];}
else
{ urlEstados = estados + destino;}


// Number of photos in this gallery
var photoNum = photoArray.length;
// initial counter
var counter = 0;
// current height of image
var currentheight = photoArray[counter][2];
// current height of image
var currentwidth = photoArray[counter][1];
// current caption
var currentcaption = photoArray[counter][3];
// transition speed
var speed = 180;
//	var to check if busy doing transition
var transition_busy = 0;


// prevPhoto
function prevPhoto($number) {
	// only if not busy doing a transition
	if (transition_busy == 0) {
		// decrease counter if there is not a direct $number
		if ($number == null) {counter --} else {counter = ($number-1)};
		// loop to end if we hit below 0
		if (counter < 0) { counter = photoNum-1 };
		//document.getElementById('mooshow_caption').innerHTML = counter;
		// now resize image container
		loadPhoto(counter);
	}
}



// nextPhoto
function nextPhoto($number) {
	// only if not busy doing a transition
	if (transition_busy == 0) {
		// decrease counter if there is not a direct $number
		if ($number == null) {counter ++} else {counter = ($number-1)};
		// loop to end if we hit below 0
		if (counter == photoNum) { counter = 0 };
		// now resize image container
		loadPhoto(counter);
	}
}

// loads new photo
function loadPhoto() {
	// update navigation if navigation on
	if(document.getElementById('mooshow_navigation')){navigation();}
	// hide overlay nav and caption
	hide_elements();
	// show laoding animation
	document.getElementById('mooshow_loading').style.display = 'inline';
	// load in new image
	imgPreloader = new Image();
	// if image is preloaded
	imgPreloader.onload=function(){
		// when loaded
		// hide laoding animation
		document.getElementById('mooshow_loading').style.display = 'none';	
		// hide current photo
		document.getElementById('mooshow_image').style.opacity = 0;
		// resize container to new size
		resize();
	}
	// preloader src
	imgPreloader.src = photoArray[counter][0];
}


// resize image container
function resize() {
	// get current photo width and height
	currentwidth = document.getElementById('mooshow_image').width;
	currentheight = document.getElementById('mooshow_image').height;
	// call moo.fx effect	
	resizeContatinerHeight.custom(currentheight,photoArray[counter][2]);
	/*resizeContatinerWidth.custom(currentwidth,photoArray[counter][1]);*/
}



// hide various mooShow elements
function hide_elements() {
	// clear cpation. fill with space
	if(document.getElementById('mooshow_caption')){
	document.getElementById('mooshow_caption').innerHTML = '';}
	// overlay navigation
	document.getElementById('mooshow_prev').style.display = 'none';
	document.getElementById('mooshow_next').style.display = 'none';
}



function switch_photo() {
	document.getElementById('mooshow_image').src=photoArray[counter][0];
	document.getElementById('mooshow_image').alt='Foto Galeria';/*photoArray[counter][2];*/
	document.getElementById('mooshow_image').width=photoArray[counter][1];
	document.getElementById('mooshow_image').height=photoArray[counter][2];
	document.getElementById('mooshow_image').name=counter;
	// update caption  if it exists
	if(document.getElementById('mooshow_caption')){caption();}
	
	// show new image
	show_elements();
}



// show various mooShow elements
function show_elements() {
	// fade in photo
	fade_in('mooshow_image',speed*2);
	// fade in caption if captions on
	if(document.getElementById('mooshow_caption')){fade_in('mooshow_caption',speed);}
}


// show nav elements
function show_nav_overlay() {
	// overlay navigation
	document.getElementById('mooshow_prev').style.display = 'inline';
	document.getElementById('mooshow_next').style.display = 'inline';
}


// build navigation
function navigation(){
	// clear current links

	document.getElementById('mooshow_navigation').innerHTML = '';
	var i=0
	
	for (i=0;i< photoNum;i++) {
		// get current content	
		var content = document.getElementById('mooshow_navigation').innerHTML;
		// write links
		if(i==counter){
			document.getElementById('mooshow_navigation').innerHTML = content + ' <img src=\"http://imagebank.visitehotelesdemexico.com/mexico/'+urlEstados+'/imagenes/photoGallery/thumbs_c/thumbs_0' +(i+1)+ '.jpg" class=\"border_s" width=\"65" height=\"45" ></a> ';} 
			else {
			document.getElementById('mooshow_navigation').innerHTML = content + ' <a href=\'#' +(i+1)+ '\' class=\'thumbs \' onclick=\'nextPhoto('+(i+1)+');\'><img src=\"http://imagebank.visitehotelesdemexico.com/mexico/'+urlEstados+'/imagenes/photoGallery/thumbs_c/thumbs_0' +(i+1)+ '.jpg" class=\"border" width=\"65" height=\"45" ></a> ';}
		   // alert((i+1));
		// add | between links
		if(i < photoNum-1) {
			var content = document.getElementById('mooshow_navigation').innerHTML; document.getElementById('mooshow_navigation').innerHTML = content + '  ';}
		}
	//update next / prev links
	if (counter == photoNum-1) {document.getElementById('nextphoto').href = '#1'} else {document.getElementById('nextphoto').href = '#' + (counter+2)};
	if (counter == 0) {document.getElementById('prevphoto').href = '#'+photoNum} else {document.getElementById('prevphoto').href = '#' + (counter)};
}


// caption
function caption(){
	// uodate caption
	var caption;
	// add   if no caption otherwise dive jumps
	if(photoArray[counter][3]=='') {caption=' '} else {caption=photoArray[counter][3]}
	document.getElementById('mooshow_caption').innerHTML = caption;
}



// moo.fx

// resize
window.onload = function() {
	transition_busy = 1;
	resizeContatinerHeight = new fx.Height('mooshow_image_container', {duration: speed, onComplete:function() {
	// display new image etc.
	switch_photo();
	transition_busy = 0;
	}});
	/*resizeContatinerWidth = new fx.Width('mooshow_container', {duration: speed});	*/
	
	// load first photo
	transition_busy = 0;
	var photoNumber = ObtenerP(1);
	//alert(photoNumber);
	if(photoNumber > 0){
	nextPhoto(photoNumber);}
	else{
	nextPhoto(1);	
	}
	
} 


// fader effect
function fade_in($element,$speed) { 
	var fader = new fx.Opacity($element, {duration: $speed, onComplete:function() {
	// overlay navigation
	show_nav_overlay();
	}});
	fader.hide();
	fader.toggle();
}


