var imgDir = '/img/bento_black/';

/** Validacion del formulario **/

var listaCampos= new Array();
var listaEtiquetas = new Array();

var requiredClassname = 'required';

var validateEvents = function () {
	listaEtiquetas = new Array();
	listaCampos = new Array();
	// etiquetas
	var labels = document.getElementsByTagName("label");
	for (i=0; i<labels.length; i++) {
		if (labels[i].className == requiredClassname) {
			listaEtiquetas.push(labels[i].firstChild.data);
			// parchecito para IE (como siempre!)
			var targetname=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
			var targetfield = document.getElementById(targetname);
			if (targetfield != null) {
				listaCampos.push(targetfield.name);
			}
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

var imageMobileEvents = function() {
	resizeImage();
	
	window.onorientationchange = function () {
		resizeImage();
	}
}

var resizeImage = function () {
	var vp_width = window.innerWidth;
	var img = document.getElementById("mainimg");
	if (img != null) {
		var img_width = img.width;
		img.width = parseInt(vp_width*.90);
	}
}


var lightboxEvents = function () {
	// lightbox
	var lb_link = $(".lightbox");
	if (lb_link != null) {
		if (lb_link.hasClass("remove")) {
			lb_link.bind("click", function () {
				if (this.rel != '') {
					removeLightBox(this.rel);
				}
				return false;
			});
		} else {
			lb_link.bind("click", function () {
				if (this.rel != '') {
					addToLightBox(this.rel);
				}
				return false;
			});
		}
	}
	// confirmacion de seleccion
	var lb_button = $("#lb_button");
	if (lb_button != null) {
		lb_button.bind("click", function () {
			var url = "/ajax/confirm_lightbox.php";
			var _data = "site="+$("#site").val();
			
			$.ajax({
					url: url, 
					type: 'get', 
					dataType: "text", 
					data: _data, 
					success: showConfirm
				}
			);
			return false;
		});
	}
}

var showConfirm = function (response) {
	var json = eval(response);
	var success = json.success;
	if (success == 1) {
		alert(msgConfirmedLightbox);
	}
}

var addToLightBox = function (id_img) {
	var url = "/ajax/lightbox.php";
	var siteid = $("#siteid").val();
	var _data = "id_img="+id_img+"&action=add&siteid="+siteid;
	
	$.ajax({
			url: url, 
			type: 'get', 
			dataType: "text", 
			data: _data, 
			success: showAddToLB
		}
	);
}

var removeLightBox = function (id_img) {
	var url = "/ajax/lightbox.php";
	var _data = "id_img="+id_img+"&action=remove";
	
	$.ajax({
			url: url, 
			type: 'get', 
			dataType: "text", 
			data: _data, 
			success: showAddToLB
		}
	);
}

var showAddToLB = function (response) {
	var json = eval(response);
	var action = json.action;
	var id_img = json.id_img;
	var success = json.success;
	
	if (success == 1) {
		if (action == 'remove') {
			// elimina el div que contiene el thumb
			// console.log(id_img);
			var img = document.getElementById("thumb_"+id_img);
			if (img != null) {
				img.className = 'transpa';
			} else {
				// es un link en la pagina de la imagen
				$(".lightbox").html(txtAddtoLightbox);
				$(".lightbox").bind("click", function() {
					addToLightBox(id_img);
					return false;
				});
			}
		} else {
			var link = document.getElementById("nav_lightbox");
			if (link == null) {
				$("#nav").append("<li><a href=\"/lightbox/\" class=\"lb\" id=\"nav_lightbox\">"+txtLightBox+"</li>");
			}
			// cambia el texto del link
			$(".lightbox").html(txtRemovefromLightbox);
			$(".lightbox").bind("click", function() {
				removeLightBox(id_img);
				return false;
			});
			alert(msgLightboxAdded);
		}
	} else {
		alert(msgLightboxAddError);
	}
	return false;
}


var parseImgUrl = function (url) {
	var re = new RegExp(/images\/([0-9]{1,})\/([0-9]{1,})\/\?p=([0-9]{1,})$/);
	if (re.test(url)) {
		var imgid = RegExp.$1;
		var galid = RegExp.$2;
		var page = RegExp.$3;
		
		var res = Array(imgid, galid, page);
		return res;
	}
	return null;
}

