﻿var DangerWebAdmin = function(){
	/*
	Autor: Daniel Gerardo Sánchez
	Web: www.danger.com.mx 
	Correo: dangercito@hotmail.com
	Version: 11 de Enero 2010
	*/


	/*
	NOTA: Algunas funciones fuerón escritas basandome en 
		ejemplos de otros sitios, estas funciones tienen
		su descriptiva,	el resto es codigo propio.
	*/
	
	this.g = function(res){
		res = (res=='r') ? [screen.width,screen.height] : 'null'; // resolución
		return res;
	}	


	this.id = function(id){
		return document.getElementById(id);
	}


	// Basada en: http://snipplr.com/view/799/get-url-variables/
	this.GETS = function(){
		var vs = [], h;
		var hs = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(i=0;i<hs.length;i++){
			h = hs[i].split('=');
			vs.push(h[0]);
			vs[h[0]] = h[1];
		}
		return vs;
	}
	this.GET = this.GETS();

	this.validar = function(f,c){
		msg = '';
		for(i=0;i<c.length;i++){
			if(c[i].value == ''){
				msg += c[i].name+': No debe estar vacio.\n';
				c[i].style.border = 'solid 1px red';
			}else{
				c[i].style.border = '';
			}
		}
		if(msg == ''){
			f.submit();
		}else{
			//alert(msg);
		}
	}

	this.pop = function(p,w,h){
		tws = screen.width/2; mw = w/2; ws = tws-mw;
		ths = screen.height/2; mh = h/2; hs = ths-mh;
		tar = window.open(p.href,p.name,'top='+hs+'px,left='+ws+'px,width='+w+'px,height='+h+'px,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1');
		if(!w){ tar.close(); }
	}

	this.rollover = function(ider){
		me = document.getElementById(ider);
		el = me.getElementsByTagName('a');
		bs = [];
		im = [];
		for(i=0;i<el.length;i++){
			im[i]= new Image(100,100); 
			im[i].src=el[i].rel;
			el[i].id='r_'+ider+'_'+i;
			document.getElementById('r_'+ider+'_'+i).onmouseover = function(){this.firstChild.src=this.rel;};
			document.getElementById('r_'+ider+'_'+i).onmouseout = function(){this.firstChild.src=this.rev;};
		}
	}

	// Basadas en: http://www.quirksmode.org/js/cookies.html
	this.sgalleta = function(nombre,info,dias){
		if(dias){
			var date = new Date();
			date.setTime(date.getTime()+(dias*24*60*60*1000));
			var expira = "; expires="+date.toGMTString();
		}else{ var expira = ""; }
		document.cookie = nombre+"="+info+expira+"; path=/";
	}

	this.rgalleta = function(nombre){
		var ntemp = nombre + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(ntemp) == 0) return c.substring(ntemp.length,c.length);
		}
		return null;
	}

	this.dgalleta = function(nombre){
		this.sgalleta(nombre,"",-1);
	}

	// Limitar numero de caracteres en textarea
	this.limitar = function(t){
		max = t.getAttribute('max');
		if(t.value.length > max){
			t.value=t.value.substring(0,max);
			t.style.border='solid 1px yellow';
		}else{
			t.style.border='';
		}
	}


	// Menu del tipo DropDown
	this.Mdrop = function(ider){
		var el = document.getElementById(ider).getElementsByTagName('li');
		for(i=0;i<el.length;i++){
			var ele = el[i];

			var uls = document.getElementById(ider).getElementsByTagName('ul');
			for(o=0;o<uls.length;o++){
				var ul = uls[o];
				ul.style.display='none';
			}

			ele.onmouseover = function(){ this.getElementsByTagName('ul').item(0).style.display='block'; }
			ele.onmouseout = function(){ this.getElementsByTagName('ul').item(0).style.display='none';  }
		}
	}


	// Fadein y Fadeout
	this.op = function(el,n){
		el = document.getElementById(el);
       		el.style.opacity = n;
	        el.style.MozOpacity = n;
        	el.style.KhtmlOpacity = n;
	        el.style.filter = "alpha(opacity=" + (n * 100) + ");";		
	}

	this.fadein = function(ider){
	        for (i=0;i<=1;i+=(1/20)) {
			setTimeout("_.op('"+ider+"'," + i + ")", i * 1000);
	        }
	}

	this.fadeout = function(ider){
	        for (i=0;i<=1;i+=(1/20)) {
			setTimeout("_.op('"+ider+"'," + parseFloat(1-i) + ")", i * 1000);
	        }
	}


	// Slideshow de imagenes
	this.slideshow = function(ider,tiempo,fotos,pos){
		id = document.getElementById(ider);
		if(!pos){
			var pos=0;
			for(i=0;i<fotos.length;i++){
				im[i]= new Image(100,100); 
				im[i].src=fotos[i];
			}
		}
		if(typeof fotos=='string'){ fotos = fotos.split(','); };

		if(fotos[pos]){
			id.src=fotos[pos];
			pos++;
		}else{
			pos=0;
			id.src=fotos[pos];
		}

		setTimeout('_.slideshow("'+ider+'",'+tiempo+',"'+fotos+'",'+pos+')',tiempo*1000);
	}

	
	// Valor por default para campos de texto
	// implementación: <input type="text" onclick="_.val(this);" value="Buscar" val="Buscar" />
	this.val = function(ob){
		ob.value='';
		ob.onblur=function(){if(ob.value == ''){ob.value=ob.getAttribute('val')}}
	}


	// No mostrar errores de codigo
	function err(){
		return true
	};
	window.onerror = err;

};
var lab = new DangerWebAdmin();

