var is_safari = (navigator.userAgent && navigator.userAgent.indexOf("Safari")>-1);
var Gallery = Class.create();
Gallery.prototype = {
	imgs: [],
	old_srcs: [],
	new_srcs: [],
	img_alts: [],
	initialize: function(){
		//if(is_safari)return false;
		this.imgs = $$(".gallery-image img");
		if(this.processImgs())
			this.newDiv();
	},
	processImgs: function(){
		//copia imgs y borra actuales.
		if(this.imgs.length < 1)return false;
		for(var i=0,total = this.imgs.length;i<total;i++){
			this.img_alts.push(this.imgs[i].alt || "");
			this.old_srcs.push(this.imgs[i].src);
			this.new_srcs.push(this.toThumb(this.imgs[i].src));
			Element.remove(this.imgs[i]);//remueve foto original
		}
		return true;
	},
	// Crea nuevo div con imgs chicas
	newDiv: function(){
		var gallery = document.createElement("div");
		gallery.id = "project-gallery";
		gallery.className = "clearfix";
		try{
		for(var i=0,total=this.new_srcs.length;i<total;i++){
			var img_html = "<img src='"+this.new_srcs[i]+"' />";
			new Insertion.Bottom(gallery, "<a href='"+this.old_srcs[i]+"' rel='lightbox[gallery]' title='"+this.img_alts[i]+"'>"+img_html+"</a>");
		}
		}
		catch(e){
			alert(e);
		}
		this.cleanHtml();
		var project_body_html = $("project").innerHTML;
		$("project").innerHTML = "";
		var body = document.createElement("div");
		body.id = "project-body";
		body.innerHTML = project_body_html;
		$("project").appendChild(body);
		$("project").appendChild(gallery);
	},
	cleanHtml: function(){
		// Limpia los spans que genera tinyMCE
		var spans = $$(".gallery-image");
		spans.collect(function(e,i){
			Element.remove(e);
		});
	},
	toThumb: function(src){
		var sp = src.split("/");
		sp[sp.length-1] = "thumb." + sp[sp.length-1];
		return sp.join("/");
	}
}
var gallery = function(){
	var gal = new Gallery();
	initLightbox()
}
var hay_galeria = true;
//Event.observe(window,"load",gallery);