var peticion = false; 
if (window.XMLHttpRequest) {
      peticion = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
            peticion = new ActiveXObject("Microsoft.XMLHTTP");
}

function ObtenerDatos(datos,divID) { 
if(peticion) {
     var obj = document.getElementById(divID); 
     peticion.open("GET", datos); 
     peticion.onreadystatechange = function()  { 
          if (peticion.readyState == 4) { 
               obj.innerHTML = peticion.responseText; 
          } 
     } 
peticion.send(null); 
}
}

function CambiarEstilo(id) {
	var elementosMenu = getElementsByClassName(document, "li", "activo");
	for (k = 0; k< elementosMenu.length; k++) {
	elementosMenu[k].className = "inactivo";
	}
	var identity=document.getElementById(id);
	identity.className="activo";
}



/**********************
 **********************
 **********************/

 
function showLargeImage(strDisplayPath, ad_id, admin) {
	var ad_id = ad_id ? ad_id : "";
	var admin = admin ? admin : false;
	var image = document.getElementById("display_image" + ad_id).firstChild;

	if (admin) {
		resizeImage(image, strDisplayPath, null, admin);
	} else {
		if (navigator.userAgent.toLowerCase().indexOf('safari') > 0) {
			var next_image = new Image;
			next_image.src = strDisplayPath;

			image.src = next_image.src;
			waitForNextImage(next_image, ad_id);
		} else {
			image.src = strDisplayPath;
		}
	}
}

/* Video */
function flowplayer_conf(video_flv, add_splash, auto_buffer, image_path) {
	var conf = new Object();
	var image_path = image_path ? image_path : false;

	conf.showLoopButton = false;
	conf.showMenu = false;
	conf.autoPlay = false;
	conf.loop = false;
	conf.initialScale = 'scale';
	conf.showFullScreenButton = false;
	conf.fullScreenScriptURL = '';
	conf.bufferLength = 10;
	conf.videoFile = video_flv;
	conf.playList = '';

	if (auto_buffer) {
		conf.autoBuffering = true;
	} else if (add_splash && video_flv.indexOf('videos') > 0) {
		var video_splash = '';
		if (image_path == false) {
			video_splash = video_flv.replace(/.*videos/, add_splash).replace(/flv/, 'jpg');
		} else {
			var image_id = video_flv.substring(video_flv.lastIndexOf("/"));
			image_id = image_id.replace(/flv/, "jpg");
			video_splash = image_path + image_id.substring(0, 3) + image_id;
		}

		conf.autoBuffering = false;
		conf.splashImageFile = video_splash;
		conf.scaleSplash = true;
	} else if (add_splash && video_flv.indexOf('flush') > 0) {
		var video_splash = video_flv.replace(/flv/, 'splash');

		conf.splashImageFile = video_splash+'/.jpg';
		conf.scaleSplash = true;
	}


	return conf;
}

function flowplayer_conf_to_string(conf) {
        var conf_string = '{';
        for (var i in conf) {
                conf_string += i + ':' + (typeof(conf[i]) == 'string'?"'":'') + conf[i] + (typeof(conf[i]) == 'string'?"'":'');
                conf_string += ',';
        }
        conf_string = conf_string.substring(0, conf_string.length - 1);
        conf_string += '}';
        return conf_string;
}

function hide_video(ad_id) {
	ad_id = ad_id || '';

	var video = document.getElementById('flowplayerholder' + ad_id);
	var image = document.getElementById('display_image' + ad_id);

	if (video) {
		video.style.display = 'none';
		var container = video.parentNode;
		container.removeChild(video);
	}

	if (image)
		image.style.display = 'block';
}

function show_video(video_file, width, height, ad_id) {
	ad_id = ad_id || '';
	var image = document.getElementById('display_image' + ad_id);
	var container = image.parentNode;
	var video = document.getElementById('flowplayerholder' + ad_id);

	var conf = flowplayer_conf(video_file, false);
	conf.autoPlay = true;
	conf = flowplayer_conf_to_string(conf);

	if (ad_id && width > 400)  {
		height = Math.round(400 / width * height);
		width = 400;
	}

	if (!video) {
		video = document.createElement('div');

		video.className = 'ad_pict';

		video.id = 'flowplayerholder' + ad_id;
		container.appendChild(video);

		image.style.display = 'none';

		var fo = { 
			movie:"swf/FlowPlayer.swf", 
			width:width, 
			height:height, 
			majorversion:"7", 
			build:"0", 
			allowscriptaccess: "always",
			flashvars:"config=" + conf
			};
	
		UFO.create(fo, "flowplayerholder" + ad_id);

	} else if (video.style.display == 'none')  {
		image.style.display = 'none';
		video.style.display = 'block';
	} else {
		var fo = document.getElementById("flowplayerholder" + ad_id + "_obj");
		var time = fo.getTime();
		var dur = fo.getDuration();
		if (time >= dur)
			fo.Seek(0);


		if(!fo.getIsPaused() && fo.getIsPlaying())  {
			fo.Pause();
		} else { 
			fo.DoPlay(); 
		}
	}	
}

function next_image () {
	if(!images[counter])
		counter = 0;

	/* If next item is an video, play video */
	if(video_exist == 1 && counter == 0) {
		var v_thumb = document.getElementById('thumb' + images.length);
		v_thumb.name = 'video';
		counter = 0;

		thumbnailBorder(v_thumb, images.length + 1);
		show_video(video_url, video_width, video_height); 

		return;
	}
		
	/* Preload next image */
	var thumb = document.getElementById('thumb' + counter);
	var image = new Image;
	image.src = image_url + images[counter];

	thumbnailBorder(thumb, images.length);
	showLargeImage(image_url + images[counter]);			

	set_alt_title('main_pict');

	counter++;
}

function set_alt_title(call_div) {
	var main_image = document.getElementById('main_image');
	var adder = 0;

	if(call_div == 'thumb') {
		adder = 1;	
	}	

	/* When next thumb is a video, display other alt/title */
	if(video_exist == 1 && counter == images.length - 1 + adder)
	{
		main_image.alt = '';
		main_image.title = '';
	} else {
		main_image.alt = '';
		main_image.title = '';
	}
}

var styles = [];
function thumbnailBorder(thumb, image_num, ad_id) {
	var ad_id = ad_id ? ad_id : "";

	if (thumb.name != 'video')
		hide_video(ad_id);

	for (i = 0; i < image_num; i++) {
		var thumb_obj = document.getElementById('thumb' + i + ad_id);
		if (!styles[i]) {
			styles[i] = thumb_obj.className;
			if (styles[i].indexOf('ad_border_solid_black') > 0) {
				styles[i] = styles[i].substring(0, styles[i].indexOf('ad_border_solid_black')) + 'ad_border_solid_grey';
			}
		}
		thumb_obj.className = styles[i];

		if (thumb.id == thumb_obj.id) {
			thumb_obj.className = "ad_thumb ad_border_solid_black";
			if (thumb.name != 'video') {
				document.getElementById('display_image' + ad_id).className = 'ad_pict' + ((styles[i].indexOf('ad_border_dotted') > 0)?' ad_border_dotted':'');
			}
		}
	}
}


function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];      
		if(oRegExp.test(oElement.className)){
			arrReturnElements[arrReturnElements.length] = oElement;
		}   
	}
	return (arrReturnElements)
}

function show_hidden_elements() {
	var elements = getElementsByClassName(document, "*", 'hide');
	for (var i = 0; i < elements.length; i++) {
		elements[i].className = elements[i].className.replace(/hide/, '');
	}
}
