/*
Tabs automáticos
transforma en tabs los h4 con class="ficha-dyn-tab"
agrega la clase "active" al LI correspondiente al panel activo
@2006 Aardvark / i2b
--------------------------------------------------*/
/*
Namespace
--------------------------------------------------*/
var PCS = PCS || {};
PCS.DynTabsStore = {
	tabs:[],
	panels:[],
	last_open:-1,
	tabClick:function(idx){
		$(this.tabs[idx]).addClassName("active");
		this.panels[idx].toggle();
		if(this.last_open > -1){
			this.panels[this.last_open].toggle();
			$(this.tabs[this.last_open]).removeClassName("active");
		}
		this.last_open = idx;
	}
}
PCS.DynTabs = Class.create();
PCS.DynTabs.prototype = {
	class_name:".ficha-dyn-tab",
	// Fin config
	elementos:null,
	links:[],
	first:0,
	initialize:function(first_selected){
		this.first = first_selected-1;
		this.elementos = $$(this.class_name);
		this.setLinks();
		this.buildTabs();
		this.presetPanels();
	},
	setLinks:function(){
		ref = this;
		this.elementos.each(
			function(elem,index){
				a = elem.getElementsByTagName("a")[0];
				Event.observe(a,"click",function(){PCS.DynTabsStore.tabClick(index);a.blur();return false;});
				ref.links[index] = a;
				PCS.DynTabsStore.panels[index] = $(a.href.split("#")[1]) || null;
				a.href = "javascript:void(0);";
			}
			);
			PCS.DynTabsStore.panels.reverse();
	},
	buildTabs:function(){
		var div = document.createElement("div");
		div.id = "ficha-paneles-nav";
		var ul = document.createElement("ul");
		$(ul).addClassName('clearfix');
		
		this.links.each(
			function(link,idx){
				li = document.createElement("li");
				li.appendChild(link);
				ul.appendChild(li);
				PCS.DynTabsStore.tabs.push(li);
			}
			);
		div.appendChild(ul);
		this.elementos[0].parentNode.insertBefore(div,this.elementos[0]);
	},
	presetPanels:function(){
		//borra elementos originales
		this.elementos.each(
			function(elem,index){
				Element.remove(elem);//remueve los titulos
			}
			);
		PCS.DynTabsStore.tabClick(this.first);
	}
}
/*
Agregar a comparador, simple
----------------------------------*/
PCS.Ficha = {
	gallery_params:{
		mode:'fotos',
		id:0
	},
	gallery_url:"galeria.iws",
	openGallery:function(){
		var params = Object.extend(this.gallery_params,arguments[0] || {});
		var options = {
			//parameters:params,
			method:'get',
			onSuccess:this.gallery_success
			//,
			//onFailure:this.gallery_fail,
			//onError:this.gallery_error
		};
		new Ajax.Request(this.gallery_url+'?id='+params.id,options);
	},
	gallery_success:function(req){
		new PCS.FloatBox('',{remove_after:false,fade_opacity:false,content:req.responseText});
	}
}
/*
Comparador
---------------------------------*/
PCS.Comparador = {
	comparador_id:'comparador-results',
	botonera:'comparador-botonera',
	url:'session2.iws',
	id_eq:0,
	marca:"",
	modelo:"",
	img_home:"",
	tipo:"",
	comparar:function(equipo_id, marca, modelo, img_home,tipo){
		var options = {
			parameters:'add='+equipo_id+'&marca='+marca+'&modelo='+modelo+'&img_home='+img_home+'&tipo='+tipo,
			method:'post',
			onSuccess:PCS.Comparador.ficha_success.bind(this)
		};
		new Ajax.Request(this.url,options);
	},
	ficha_success:function(req){
		if(req.responseText==-1){
			$('option-comparador').innerHTML='<a href="Javascript:;" onclick="PCS.Comparador.comparar('+this.id_eq+',\''+this.marca+'\',\''+this.modelo+'\',\''+this.img_home+'\',\''+this.tipo+'\');return false;" title="Agregar este equipo al comparador">Agregar al comparador</a>';
		}else{
			$('option-comparador').innerHTML='Equipo Agregado';
		}
		PCS.Comparador.show();
	},
	link_comparar:function(id_eq, marca, modelo, img_home,tipo){
		var options = {
			parameters:'busca='+id_eq,
			method:'post',
			onSuccess:PCS.Comparador.ficha_success.bind(this)
		};
		this.id_eq=id_eq;
		this.marca=marca;
		this.modelo=modelo;
		this.img_home=img_home;
		this.tipo=tipo;
		new Ajax.Request(this.url,options);
	},
	add: function(id, marca, modelo, img_home,tipo){
		var options = {
			parameters:'add='+id+'&marca='+marca+'&modelo='+modelo+'&img_home='+img_home+'&tipo='+tipo,
			method:'post',
			onSuccess:PCS.Comparador.success.bind(this)
		};
		new Ajax.Updater(this.comparador_id,this.url,options);
		return false;
	},
	show: function(){
		var options = {
			method:'post',
			onComplete:PCS.Comparador.success.bind(this)
		};
		new Ajax.Updater(this.comparador_id,this.url,options);
		return false;
	},
	remove: function(id){
		var li = "comp-"+id;
		if (id>=0){
			new Ajax.Request(this.url,{parameters:'remove='+id});
			PCS.Comparador.link_comparar(this.id_eq, this.marca, this.modelo, this.img_home,this.tipo);
		}
	},
	success: function(req){
		if (req.responseText.split("img/equipo-NO").length >1){
			$(this.botonera).hide();
			$('option-comparador').innerHTML='Quitar este Equipo';
			$('floatcomparador').style.width='200px';
		}
		else{
			if(this.getCount() > 1 ){
				$('floatcomparador').style.width='290px';
				$(this.botonera).show();
			}
			else{
				$(this.botonera).hide();
				$('floatcomparador').style.width='200px';
			}
		}
	},	
	getCount: function(){
		return $A($(this.comparador_id).getElementsByClassName('con_equipo')).length;
	},
	init:function(){
		if(this.getCount() > 1){
			$(this.botonera).show();
		}
	}
};
//Event.observe(window,'load',function(){PCS.Comparador.init()});

PCS.DynTabs.init = function(){	
	try{
		var tabs = new PCS.DynTabs(1);
	}catch(e){}
}
//Event.observe(window,"load",PCS.DynTabs.init)
Event.onDOMReady(PCS.DynTabs.init);

PCS.GaleriaInit = function(){
	//var _top = window.attachEvent?-200:0;//correcion para IE
	var _top = window.XMLHttpRequest?0:-200;//correcion para IE7
	var f = new PCS.FloatBox("floatbox",{top:_top});
	new PCS.GalleryMaker("thumbs","detail",{
		loading:"<span id='loader'><img src='/medios/ajax-loader.gif' /></span>",
		loading_top:185,
		loading_left:185,
		caption:"caption",
		arrow_class:"arrow"
	});
};
