/*
plainCMS popups and rollovers

This program was written By Martin Szyszlican.
It is allowed to use it in your proyects as long as you leave this notice
and, if you can, put a link in your site to www.ylisto.info

Este programa fu? escrito por Martin Szyszlican.
Esta permitido usarlo en sus proyectos siempre y cuando deje esta nota
y, si es posible, ponga un enlace en su sitio a www.ylisto.info

Created by Martin Szyszlican on 26/11/2005 (dd/mm/yy)
This version: 24/04/2006 (dd/mm/yy)
*/

EventMgr = // avoid leaking event handlers, crossbrowser version
{
  _registry:null,
  initialize:function() {
    if(this._registry == null) {
      this._registry = [];
      EventMgr.add(window, "_unload", this.cleanup);
    }
  },
  add:function(o, t, fn, uc) {
    this.initialize();
    if(typeof o == "string")
      o = document.getElementById(o);
    if(o == null || fn == null)
      return false;
    if(t == "unload") {
      // call later when cleanup is called. don't hook up
      this._registry.push({obj:o, type:t, fn:fn, useCapture:uc});
      return true;
    }
    var realType = t=="_unload"?"unload":t;

	if (o.addEventListener)
		o.addEventListener(realType,fn,uc);

    else if (o.attachEvent)
		o.attachEvent('on' + realType, fn);

	else {
		o["on"+realType] = fn;
		if (o["on"+realType] != fn) {
			alert("Error adding listener\nObject:"+o+" ID:"+o.id+" Name:"+o.name+" TagName:"+o.tagName+"\nEvent:"+realType);
		}
	}
    this._registry[this._registry] = {obj:o, type:t, fn:fn, useCapture:uc};

    return true;
  },
  cleanup:function() {
    for(var i = 0; i < EventMgr._registry.length; i++)
      with(EventMgr._registry[i])
        if(type=="unload")
          fn();
        else {
          if(type == "_unload") type = "unload";
          remove(obj,type,fn,useCapture);
        }
    EventMgr._registry = null;
  },
  remove: function (o,type,fn,uc) {
		if (o.removeEventListener)
			o.removeEventListener(type,fn,uc);
		else if (o.detachEvent)
			o.detachEvent("on" + type,fn);
		else {
			o["on"+type] = window.undefined;
			if (o["on"+type]!=window.undefined) return false;
		}
		return true;
	}
};

var fittimeout;
var loaded_images = new Array();
var toggled;

if (!document.getElementsByTagName && document.all)
	if(document.all.tags)
		document.getElementsByTagName = document.all.tags;

EventMgr.add(window,"load",generate_popups);

if (window.undefined!==window.rollover_images)
	EventMgr.add(window,"load",make_rollovers);


function generate_popups() {
	if (document.getElementsByTagName) {
		as = document.getElementsByTagName("a");
		for (a=0;a<as.length;a++) {
			a_obj=as[a];
			if (a_obj.className == "cms_img_popup") {
				//AGREGAR KEYPRESS!
				EventMgr.add(a_obj,"click",open_popup);
			}
		}
	}
}

function open_popup(e) {
	a = this;
	if (!a.href) a = e.srcElement.parentElement; 	//For images in IE
	if (!a.href) a = e.srcElement; 	//For IE
	if (!a.href) return false;

	if (!a.img_src)
		a.img_src = a.href;

	img_name = a.img_src.substr(a.img_src.lastIndexOf("/")+1);
	img_name = img_name.substr(0,img_name.lastIndexOf("."));

	whtml = 		'	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'+
					'	    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'+"\n"+
					'	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">'+"\n"+
					'	<head>'+"\n"+
					'		<title>Image - '+img_name+' </title>'+"\n"+
					'		<script type="text/javascript">'+"\n"+
					'		function loaded() {'+"\n"+
					'			document.getElementById("loading").style.display="none";'+"\n"+
					'			img=document.getElementById("image");'+"\n"+
					'			ww = img.width+30; '+"\n"+
					'			wh = img.height+70; '+"\n"+
					'			if(ww > (screen.width - 40)) { ww = (screen.width - 40); }'+"\n"+
					'			if(wh > (screen.height - 90)) { wh = (screen.height - 90); }'+"\n"+
					'			window.resizeTo(ww,wh);'+"\n"+
					'			if(ww > (screen.width - 100)) { window.moveTo(window.top,10); }'+"\n"+
					'			if(wh > (screen.height - 120)) { window.moveTo(10,window.left); }'+"\n"+
					'		}'+"\n"+
					'		window.onload = loaded;'+"\n"+
					'		window.onerror = loaded;'+"\n"+
					'		</script>'+"\n"+
					'		<style type="text/css">'+"\n"+
					'			body,html,img { margin: 0;  height: 100%; }'+"\n"+
					'			img, body * { overflow: auto; }'+"\n"+
					'			div#loading { height: 100px; background: #EEEEFF }'+"\n"+
					'		</style>'+"\n"+
					'	</head>'+"\n"+
					'	<body>'+"\n"+
					'		<div id="loading"><strong>Loading Image</strong><br />'+img_name+'</div>'+"\n"+
					'		<div id="img"><img src="'+a.img_src+'" alt="'+img_name+'" id="image" onclick="window.close();" /></div>'+"\n"+
					'		<script type="text/javascript">'+"\n"+
					'			document.getElementById("loading").style.display="block";'+"\n"+
					'		</script>'+"\n"+
					'	</body>'+"\n"+
					'	</html>'+"\n";

	a.href="#";

	w=window.open("",null,"toolbar=no,menubar=no,location=no,directories=no,personalbar=no,resizable=yes,width=100,height=100");
	
	w.resizeTo(130,170);
	
	w.focus();
	
	w.document.write(whtml);

	w.document.close();  
	  
	a=null;

	return true;
}

function make_rollovers() {
	preload_images(rollover_images);
	if (document.getElementsByTagName) {
		imgs = document.getElementsByTagName("img");
		for (img=0;img<imgs.length;img++) {
			img_obj=imgs[img];

			if (img_obj.className == "cms_img_rollover") {
				//Keyboard focus?
				EventMgr.add(img_obj,"mouseover",rollover);
				EventMgr.add(img_obj,"mouseout",rollout);
			}
		}
	}
}

function preload_images(images) {
	for (img=0;img<images.length;img++) {
		loaded_images[images[img]] = new Image;
		loaded_images[images[img]].src = images[img];
	}
}

function rollover(e) {
	rolled_img = this;
	if (!rolled_img.src) rolled_img = e.srcElement	//For IE
	if (rolled_img.src) {
		if (rolled_img.src.indexOf("_over") == -1) {
			dotimg = rolled_img.src.lastIndexOf(".");
			img_ext = rolled_img.src.substring(dotimg);
			img_name = rolled_img.src.substring(0,dotimg);
			rolled_img.src=img_name+"_over"+img_ext;
		}
	}
}

function rollout(e) {
	rolled_img = this;
	if (!rolled_img.src) rolled_img = e.srcElement	//For IE
	if (rolled_img.src) {
		if (rolled_img.src.indexOf("_over")>-1) {
			img_ext = rolled_img.src.substring(rolled_img.src.lastIndexOf("."));
			img_name = rolled_img.src.substring(0,rolled_img.src.lastIndexOf(".")-5);
			rolled_img.src=img_name+img_ext;
		}
	}
}

// Simple Debug, written by Chris Klimas
// licensed under the GNU LGPL.
// http://www.gnu.org/licenses/lgpl.txt
//
// Modified by Martin Szyszlican [14/05/06 dd/mm/yy]
// Added cross-browser compatibility
//
// There are three functions defined here:
//
// log (message)
// Logs a message. Every second, all logged messages are displayed
// in an alert box. This saves you from having to hit Return a ton
// of times as your script executes.
//
// inspect (object)
// Logs the interesting properties an object possesses. Skips functions
// and anything in CAPS_AND_UNDERSCORES.
//
// inspectValues (object)
// Like inspect(), but displays values for the properties. The output
// for this can get very large -- for example, if you are inspecting
// a DOM element.


function log (message)
{
	if (! _log_timeout)
		_log_timeout = window.setTimeout(dump_log, 1000);

	_log_messages[_log_messages.length] = message;


	function dump_log()
	{
		var message = '';

		for (var i = 0; i < _log_messages.length; i++)
			message += _log_messages[i] + '\n';

		alert(message);

		_log_timeout = null;
		delete _log_messages;
		_log_messages = new Array();
	}
}

function instanceOf(object, constructorFunction) {
  while (object != null) {
    if (object == constructorFunction.prototype)
     {return true}
	 object = object.__proto__;
  }
  return false;
}

function inspect (obj)
{
	var message = 'Object possesses these properties:\n';

	if (obj)
	{
		for (var i in obj)
		{
			if (obj != window.document) {
				if (obj[i] == window.undefined || (i.toUpperCase() == i) || (instanceOf(obj[i],Function)))
						continue;
			}

			message += i + ', ';
		}

		message = message.substr(0, message.length - 2);
	}
	else
		message = 'Object is null';

	log(message);
}

function inspectValues (obj)
{
	var message = '';

	if (obj)
		for (var i in obj)
		{
			if (obj != window.document) {
				if (obj[i] == window.undefined || (i.toUpperCase() == i) || (instanceOf(obj[i],Function)))
						continue;
			}

			message += i + ': ' + obj[i] + '\n';
		}
	else
		message = 'Object is null';

	log(message);
}

var _log_timeout;
var _log_messages = new Array();


