/***********************
*
*
*	PLEASE DO NOT MODIFY THIS WITHOUT SPEAKING WITH CHRIS (chrisf@infoex.com)
*	OR DANIEL (daniel@infoex.com) FIRST AS (a) WE WOULD LIKE TO KEEP IT IN SYNC AND 
*	(b) LICENSING HASN'T BEEN RESOLVED
*
*
************************/

//File Information:
//Version: $Revision: 974 $
//Last Changed By: $Author: cfrommann $ on $Date: 2007-07-02 23:19:51 -0400 (Sun, 02 Jul 2007) $

// lib/shared.js
//  JS shared lib

/* iWebPress - A Newspaper Administration tool
 * http://www.iwebpress.com
 * Copyright (C) 2002-2009 iWebPress, Inc., et al except where otherwise noted
 *
 * This program is protected by copyright.  Redistribution 
 * is prohibited without express written consent of the
 * authors.  You can find contact information on iwebpress.com.
 *
 * This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the 
 * implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE.
 */

var bugRiddenCrashPronePieceOfJunk = (
	navigator.userAgent.indexOf('MSIE 5') != -1
	&&
	navigator.userAgent.indexOf('Mac') != -1
);

var ua = navigator.userAgent.toLowerCase();

var isSupported = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);

var editing = false;

$ = function(o) {
	return document.getElementById(o);
}

Array.prototype.exists = function (x) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == x) {
			return true;
		}
    }
    return false;
}

String.prototype.toCamelCase = function() {
	var s = this;
	for(var exp = /-([a-z])/; 
		exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()) );
	return s;	
}

function confirmLogout(link,url) {
	txt = "<div style='font-size: 120%'>Are you sure you want to log out?</div><br />" +
	"<div class='buttons' style='text-align: center;'>" +
    "<a href='"+url+"' onclick='sContents.collapse(); hContents.setOpacity(100); axContents.collapse();'>Logout</a> &nbsp;" +
    "<a href='javascript:void(0)' onclick='sContents.collapse(); hContents.setOpacity(100); axContents.collapse();'>Cancel</a>" +
    "</div>";
	return buildDialogue(link,txt);
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = obj.offsetHeight;
	if (obj.offsetParent) {
		while (obj) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
	
}
getParentWithClass = function(obj,findClass) {
	if(obj && obj.parentNode) {
		if(obj.parentNode.className == findClass)
			return obj.parentNode;
		else
			getParentWithClass(obj.parentNode,findClass);
	}
	return false;
}
getClosestRelativeWithClass = function(obj,findClass) { //goes from obj upwards (and sideways)
	var sibling, i, j, k, l, child;
	if(obj && obj.parentNode) {
		for(i = 0; i < obj.parentNode.childNodes.length; i++) {
			sibling = obj.parentNode.childNodes[i];
			if(sibling.className == findClass) {
				return sibling;
			}
			if(sibling != obj) { //only go through those we haven't already encountered (sideways and down)
				child = getChildWithClass(sibling,findClass);
				if(child) return child;
			}
		}
		return getClosestRelativeWithClass(obj.parentNode,findClass);
	} else {
		return false;
	}
}

getChildWithClass = function(obj,findClass) {
	var child,child2;
	for(var i = 0; i < obj.childNodes.length; i++) {
		child = obj.childNodes[i];
		if(child.className == findClass) {
			return child;
		}
		child2 = getChildWithClass(child,findClass);
		if(child2) return child2;
	}
	return false;
}

getElsByClassName = function(searchClass,tag, node) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
/*var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			alert(returnElements.length);
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};*/
/*
If only we lived in a world w/o IE:
Node.prototype.getStyle = function(style)
Instead:
*/
getStyle = function(node,style) {
	var value = node.style[style];
    if(!value)
        if(document.defaultView) { //DOM
            value = document.defaultView.getComputedStyle(node, "").getPropertyValue(style);
        } else if(node.currentStyle) { //IE
            value = node.currentStyle[style.toCamelCase()];
            if(value == 'auto')
            	if(style == 'height')
            		value = node.offsetHeight; //this should subtract padding, margin, border, but
            									//there's no way we could possibly anticipate 

            									//properties we need to look at (well, not really...)
            	else if(style == 'width')
            		value = node.offsetWidth; //see above
            		
        }
    return value;
}

function defineAttribute(obj,attribute,value) {
	if(document.all) {
		var eventhandlers = new Array ("onclick","onmouseover","onmouseout");
		if(eventhandlers.exists(attribute)) {
			eval("obj." + attribute + " = function () { " + value + " }");		
		} else {
			eval("obj." + attribute + ' = "' + value + '"');
		}
	} else if(document.getElementById) {
		eval("obj.setAttribute(attribute,value)");
	} else {
		return false;
	}
	return true;
}

function cookieCreate(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function cookieRead(name) {
	var nameEQ = name + "=";
	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(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function cookieErase(name) {
	createCookie(name,"",-1);
}

function isCollapsed(obj) {
	for(var i = 0; i < obj.childNodes.length; i++) {
		if(obj.childNodes[i].nodeName == "DIV") {
			if(getStyle(obj.childNodes[i],'display') == "block") {
				return false;
			}
		}
	}
	return true;
}

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

