/*
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)
*/

/*external d EventMgr open_popup */

function generate_popups() {
	var as,a,a_obj;

	if (d.getElementsByTagName) {
		as = d.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 fix_eolas() {
	var objects = d.getElementsByTagName("object");

	for (var i=0; i<objects.length; i++) {
		a = ""+objects[i].outerHTML;
		objects[i].outerHTML = a;
	}
}

function open_popup(e) {
	var img_name,whtml;

	var 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;
		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"+
					'		<style type="text/css">'+"\n"+
					'			body,html,#img { margin: 0;  height: 100%; width: 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"+
					'			function loaded() {'+"\n"+
					'				document.getElementById("loading").style.display="none";'+"\n"+
					'				ww = document.getElementById("image").width+30; '+"\n"+
					'				wh = document.getElementById("image").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"+
					'			document.getElementById("loading").style.display="block";'+"\n"+
					'			window.onload = loaded;'+"\n"+
					'			window.onerror = loaded;'+"\n"+
					'		</script>'+"\n"+
					'	</body>'+"\n"+
					'	</html>'+"\n";


	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 = window.undefined;

	return false;
}

function make_rollovers() {
	preload_images(rollover_images);
	if (d.getElementsByTagName) {
		imgs = d.getElementsByTagName("img");
		apply_rollovers(imgs);
	}
}

function apply_rollovers(collection) {
	elem = 0;
	for (elem=0;elem<collection.length;elem++) {
		img_obj=collection[elem];
		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;
		}
	}
}


//Forms:
function select_toggle(obj,value) {
	o = d.g(value)
	if (o == window.undefined) return false;
	o = o.style;

	if(obj.value==value)
		o.display="block";
	else
		o.display="none";

	return o.display;
}

// 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();


var 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 = d.g(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;

	this.realAdd(o,realType,fn,uc);

    this._registry[this._registry.length] = {obj:o, type:t, fn:fn, useCapture:uc};

    return true;
  },
  realAdd:function(o,realType,fn,uc) {
  	if (realType == "load") {
		fn = EventMgr.init;
  		this.realAdd(o,"error",fn,uc);
		this.realAdd(o,"DOMContentLoaded", fn, false);
		/* for Internet Explorer */
		/*@cc_on @*/
		/*@if (@_win32)
		if (document.getElementById) {
			script = document.getElementById("__ie_onload");
			if (script == window.undefined) {
				var proto = "javascript:void(0)";
				if (location.protocol == "https:") proto = "src=//0";
				document.write("<scr"+"ipt id=__ie_onload defer src=" + proto + "><\/scr"+"ipt>");

				var script = document.getElementById("__ie_onload");
				script.onreadystatechange = function() {
					if (this.readyState == "complete") {
						fn();
					}
				};
			}
		}
		/*@end @*/
		/* for Safari */
		if (/KHTML|WebKit/i.test(navigator.userAgent)) { // sniff
		    this._timer = setInterval(function() {
		        if (/loaded|complete/.test(document.readyState)) {
					clearInterval(this._timer);
					delete this._timer;
		            fn(); // call the onload handler
		        }
		    }, 10);
		}
	}
	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);
		}
	}
  },
  init:function () {
	// quit if this function has already been called
	if (arguments.callee.done) return;
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	// execute each function in the stack in the order they were added
	for (var i=0;i < EventMgr._registry.length;i++) {
		if (EventMgr._registry[i].type=="load") {
			p=EventMgr._registry[i].fn();
		}
	}
  },
  remove:function (o,type,fn,uc) {
		if (o.removeEventListener)
			o.removeEventListener(type,fn,uc);
		else if (o.detachEvent)
			o.detachEvent("on" + type,fn);
		else {
			//TODO: Disabled 'cos it throws errors in IE4/Win
//			o["on"+type] = window.undefined;
//			if (o["on"+type]!=window.undefined) return false;
		}
		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";
          EventMgr.remove(obj,type,fn,useCapture);
        }
    delete EventMgr._registry;
  }
};

//Init
var fittimeout;
var loaded_images = new Array();
var toggled;
var htmlfields = new Array;

var d = document;
d.g = d.getElementById;

if (!d.getElementsByTagName && d.all) {
	if(d.all.tags) {
		d.getElementsByTagName = d.all.tags;
	}
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	EventMgr.add(window,"load",fix_eolas);
/*@end @*/

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

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