//=======================================================\\
//                    13thparallel.org                   \\
//                   Copyright (c) 2002                  \\ 
//   see (13thparallel.org/?title=about) for more info   \\
//=======================================================\\

var px = document.layers || window.opera ? "" : "px";

// basic object constructor
function make (id) {
	this.elm = document.getElementById ? document.getElementById(id) : document.all ? document.all[id] : null;
	if (!this.elm) return null;
	this.css = this.elm.style;
	this.obj = id + 'Obj';
	eval(this.obj + ' = this');
	this.x = this.elm.offsetLeft ? this.elm.offsetLeft : 0;
	this.y = this.elm.offsetTop ? this.elm.offsetTop : 0;
	this.w = this.elm.offsetWidth ? this.elm.offsetWidth : 0;
	this.h = this.elm.offsetHeight ? this.elm.offsetHeight : 0;
}

make.prototype.bg = function(ref){	
	this.css.backgroundColor=ref;
}

make.prototype.show = function() {
	this.css.visibility = "visible";
}

make.prototype.hide = function() {
	this.css.visibility = "hidden";
}

make.prototype.moveTo = function(x, y) {
	if (x != null) {
		x = Math.round(x);
		this.x = x;
		this.css.left = x + px;
	}
	if (y != null) {
		y = Math.round(y);
		this.y = y;
		this.css.top = y + px;
	}
}

make.prototype.moveBy = function(x, y) {
	this.moveTo(this.x + x, this.y + y);
}

make.prototype.getW = function() {
	if (this.elm.offsetWidth) return this.elm.offsetWidth;
	else if (parseInt(this.css.width)) return parseInt(this.css.width);
	else return 0;
}

make.prototype.setW = function(num) {
	this.css.width = num + px;
}

make.prototype.getH = function() {
	if (this.elm.offsetHeight) return this.elm.offsetHeight;
	else if (parseInt(this.css.height)) return parseInt(this.css.height);
	else return 0;
}

make.prototype.setH = function(num) {
	this.css.height = num + px;
}

getViewportWidth = function() {
	var width = 0;
	if( document.documentElement && document.documentElement.clientWidth ) {
		width = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) {
		width = document.body.clientWidth;
	}
	else if( window.innerWidth ) {
		width = window.innerWidth - 18;
	}
	return width;
};

getViewportHeight = function() {
	var height = 0;
	if( document.documentElement && document.documentElement.clientHeight ) {
		height = document.documentElement.clientHeight;
	}
	else if( document.body && document.body.clientHeight ) {
		height = document.body.clientHeight;
	}
	else if( window.innerHeight ) {
		height = window.innerHeight - 18;
	}
	return height;
};

// measure the left position of an element, relative to the page
// note that the element does not need to have any css positioning
function getPageLeft(elm) {
	var x = 0;
	while (elm.offsetParent && typeof elm.offsetLeft != "undefined") {
		x += elm.offsetLeft;
		elm = elm.offsetParent;
	}
	return x;
}

// measure the top position of an element, relative to the page
// note that the element does not need to have any css positioning
function getPageTop(elm) {
	var y = 0;
	while (elm.offsetParent && typeof elm.offsetTop != "undefined") {
		y += elm.offsetTop;
		elm = elm.offsetParent;
	}
	return y;
}

// end


//=======================================================\\
//                  start meddle add-ons                 \\
//=======================================================\\

function doPop(where,w,h,t,l) {
	if (!t) t=0;
	if (!l) l=0;
	// very very weird bug in Opera7 ?????
	if(window.opera) where = where+"/";
if (document.getElementById && document.getElementsByTagName && document.createElement 
&& !brw.konq && typeof document.getElementsByTagName("head")[0].innerHTML != "undefined" ) {
window.open(where, 'popupwindow', 'width='+w+',height='+h+',top='+t+',left='+l+',scrollbars=no,resizable=no'); 
}
else window.location.href = where;
}


// generic cookie check in javascript:
function getCookie(nombrecookie) {
var buscamos = nombrecookie + "=";
if (document.cookie.length > 0) {
    i = document.cookie.indexOf(buscamos);
    if (i != -1) {
      i += buscamos.length;
      j = document.cookie.indexOf(";", i);
      if (j == -1)
        j = document.cookie.length;
      return unescape(document.cookie.substring(i,j));
    }
  }
}

// EVENTS methods by Scott Andrew (www.scottandrew.com):
function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false
  }
} 

function removeEvent(elm, evType, fn, useCapture)
{
  if (elm.removeEventListener){
    elm.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.detachEvent){
    var r = elm.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

// method by Aaron Boodman (youngpup.net):
Array.prototype.indexOf = function(foo) { 
     for (var i = 0; i < this.length; i++)  
          if (foo == this[i]) return i;  
     return -1;  
}

// header script extracted from http://13thparallel.net
addStyleSheet = function(href, media) {
	// Trying createStyleSheet first, because that seems to work best with IE5/Mac.
	if (document.createStyleSheet) {
		document.createStyleSheet(href);
		return true;
	}
	else if (document.getElementsByTagName && document.createElement) {
		var head = document.getElementsByTagName("head")[0];
		if (!head || !head.appendChild) return false;
		
		var link = document.createElement("link");
		link.rel = "stylesheet";
		link.type = "text/css";
		if (media) link.media = media;
		link.href = href;
		link = head.appendChild(link);
		
		return true;
	}
	return false;
}

// PRINTING STUFF
// disableStyleSheet(media) (in _api.js) disables all css files but that media type expressed in media att.:

function addPrinter() {
	var prLang = (lang=="cat")?"Imprimir article" : (lang=="spa")?"Imprimir artículo" : (lang=="eng")?"Print article" : "Imprimir article";
	document.getElementById('idioprint').innerHTML += "<br /><a href='#' onclick='doPrint();return false' title='"+prLang+"'>"+prLang+"</a>";		
}

function doPrint() {
	disableStyleSheet("print"); 
	//alert("Styles might look strange while printing. Please be patient. Everything will become pretty again.");
	window.print();
	enableStyleSheet();
}

function disableStyleSheet(media) {
   var i, a;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1) {
       a.disabled = true;
       if(a.getAttribute("media") == media) a.disabled = false;
     }
  }
}

function enableStyleSheet() {
   var i, a;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1) {
	a.disabled = false;
     }
  }
}

wrapElement = function()  {
	var wW = wrapperObj.getW();
	var wH = wrapperObj.getH();

	var setX = ( getViewportWidth() - wW ) / 2;
	var setY = ( getViewportHeight() - wH ) / 2;
	
	if( setX < 0 ) setX = 0;
	if( setY < 0 ) setY = 0;

	wrapperObj.moveTo(setX,setY);
}


function checker(){
	this.agent=navigator.userAgent.toLowerCase();
	this.mac=this.agent.indexOf("mac") > -1;
	this.ie = this.agent.indexOf("msie") != -1;
	this.iemac = (this.mac && this.ie)?1:0;
    	this.konq = this.agent.indexOf("konqueror") != -1;
}
var brw = new checker();

//=======================================================\\
//                  end meddle add-ons                   \\
//=======================================================\\
