/*******************************\
 * Begin Dollar functions      *
\*******************************/

function $(){
    var elements = new Array();
    for(var x=0; x<arguments.length; x++){
        var o = arguments[x];
        if(typeof arguments[x] == typeof ' ')
            o = document.getElementById(o);
        
        if(arguments.length == 1)
            return o;

        elements.push(o);
    }
    return elements;
}

function $CE(tagName, attr, children){
    if(document.createElement){
        var tag = document.createElement(tagName);
        if(!tag)return false;

        if(attr){
            for(var x in attr){
                if(x.indexOf('on') == 0){
                    if(typeof attr[x] == typeof ' ')
                        tag[x] = new Function(attr[x]);
                    else
                        tag[x] = attr[x];
                } else if(x == 'style' && typeof attr[x] != typeof ' '){
                    for(var y in attr[x])
                        tag.style[y] = attr[x][y];
                } else if(x == 'class'){
                    tag.className = attr[x];
                } else if(tag.setAttribute){
                    tag.setAttribute(x, attr[x]);
                } else if(x == 'for'){
                    tag.htmlFor = attr[x];
                } else {
                    tag[x] = attr[x];
                }
            }
        }

        if(children && typeof children == typeof ' ')
            tag.appendChild(document.createTextNode(children));
        else
            for(var x=0;children && x<children.length;x++)
                if(typeof children[x] == typeof ' ')
                    tag.appendChild($CT(children[x]));
                else
                    tag.appendChild($(children[x]));

        return tag;
    }
    return false;
}

function $CT(text){
    if(document.createTextNode)
        return document.createTextNode(text);

    return null;
}

/*******************************\
 * End Dollar functions        *
\*******************************/

/***********************************\
 * Event adding/removing functions *
\***********************************/

function addLoadEvent(func) {
  return Event.add( window, 'load', func, false );
}

var Event = function(){
    return {
        //http://www.dustindiaz.com/rock-solid-addevent/
        add: function( obj, type, fn ) {
            if (obj.addEventListener) {
                obj.addEventListener( type, fn, false );
                EventCache.add(obj, type, fn);
            }
            else if (obj.attachEvent) {
                obj["e"+type+fn] = fn;
                obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
                obj.attachEvent( "on"+type, obj[type+fn] );
                EventCache.add(obj, type, fn);
            }
            else {
                var oldonload = obj["on"+type];
                obj["on"+type] = function(e){
                    oldonload(e);
                    fn(e);
                }
            }
        },
        remove: function(obj, type, fn){
            EventCache.remove(obj, type, fn);
        },

        getEvent: function(e) {
            return e || window.event;
        },
        getCharCode: function(ev) {
            return ev.charCode || ev.keyCode || 0;
        },
        stopEvent: function(ev) {
            this.stopPropagation(ev);
            this.preventDefault(ev);
        },

        stopPropagation: function(ev) {
            if (ev.stopPropagation) {
                ev.stopPropagation();
            } else {
                ev.cancelBubble = true;
            }
        },

        preventDefault: function(ev) {
            if (ev.preventDefault) {
                ev.preventDefault();
            } else {
                ev.returnValue = false;
            }
        }
    };
} ();
var EventCache = function(){
    var listEvents = [];
    return {
        listEvents: listEvents,
        add: function(node, sEventName, fHandler){
            listEvents.push(arguments);
        },
        flush: function(){
            var i;
            for(i = listEvents.length - 1; i >= 0; i = i - 1){
                EventCache._remove(listEvents[i]);
            };
        },
        remove: function(node, sEventName, fHandler){
            for(var i = listEvents.length - 1; i >= 0; i = i - 1){
                if(listEvents[i][0] === node 
                    && (!sEventName || sEventName == listEvents[i][1]) 
                    && (!fHandler || fHandler === listEvents[i][2])
                ){
                    EventCache._remove(listEvents[i]);
                    listEvents.splice(i,1);
                }
            }
        },
        _remove: function(listEvent){
            if(listEvent[0].removeEventListener){
                listEvent[0].removeEventListener(listEvent[1], listEvent[2], listEvent[3]);
            };
            if(listEvent[1].substring(0, 2) != "on"){
                listEvent[1] = "on" + listEvent[1];
            };
            if(listEvent[0].detachEvent){
                listEvent[0].detachEvent(listEvent[1], listEvent[2]);
            };
            listEvent[0][listEvent[1]] = null;
        }
    };
} ();
Event.add(window,'unload',EventCache.flush);

/***********************************\
 * End Event functions             *
\***********************************/

function URLencode(_sStr) {
	return escape(_sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

function ltrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
	var j=0, i = s.length;
	while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		j++;
		s = s.substring(j, i);
	}
	return s;
}

function rtrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		i--;
		s = s.substring(0, i+1);
	}
	return s;
}

function trim(str) {
	return rtrim(ltrim(str));
}

function getPageCoords (element) {
	var coords = { x: 0, y: 0 , w:0, h:0};
	coords.w = element.offsetWidth;
	coords.h = element.offsetHeight;
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

function warn(_title, _msg, _href, _obj) {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	warningDivID = Math.floor(Math.random()*10001);
    warningDiv =
        $CE('div',{'id':'warning' + warningDivID,'class':'lookupPopdown','style':{'position':'absolute','top':arrayPageScroll[1] + (arrayPageSize[3] / 3) + 'px','left':(getPageCoords(document.body).w/2)-160 + 'px','zIndex':'10'}},[
            $CE('div',{'class':'infoBoxLiteWrapper','style':{'margin':'0','width':'320px'}},[
                $CE('div',{'class':'infoBoxLite','style':{'padding':'10px','background':'#FFF'}},[
                    $CE('div',{'style':{'textAlign':'left','borderBottom':'1px solid #CCC','paddingBottom':'10px','font':'bolder 18px Arial'}},_title),
                    $CE('div',{'style':{'textAlign':'left','padding':'10px'}},_msg),
                    $CE('div',{'style':{'textAlign':'right','borderTop':'1px solid #CCC','paddingTop':'10px'}},[
                    	$CE('button',{'onclick':function(e){location.href=_href;}}, 'Yes'),
                    	$CE('button',{'onclick':function(e){darkBackdrop(warningDivID,false); document.body.removeChild($('warning' + warningDivID)); document.body.removeChild(iframeShim); return false;},'style':{'marginLeft':'10px'}}, 'No')
                    ])
                ])
            ])
        ]);
    document.body.appendChild(warningDiv);

	iframeShim = $CE('iframe',{'id':'divShim' + warningDivID,"src":"javascript:false","scrolling":"no","frameborder":"0",'style':{'position':'absolute','top':warningDiv.style.top,'left':warningDiv.style.left,'height':warningDiv.offsetHeight+'px','width':warningDiv.offsetWidth+'px','zIndex':'9'}});

	document.body.appendChild(iframeShim);
	darkBackdrop(warningDivID,true);
}

function ok(_title, _msg, _obj) {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	okDivID = Math.floor(Math.random()*10001);
    okDiv =
        $CE('div',{'id':'ok' + okDivID,'class':'lookupPopdown','style':{'position':'absolute','top':arrayPageScroll[1] + (arrayPageSize[3] / 3) + 'px','left':(getPageCoords(document.body).w/2)-160 + 'px','zIndex':'10'}},[
            $CE('div',{'class':'infoBoxLiteWrapper','style':{'margin':'0','width':'320px'}},[
                $CE('div',{'class':'infoBoxLite','style':{'padding':'10px','background':'#FFF'}},[
                    $CE('div',{'style':{'textAlign':'left','borderBottom':'1px solid #CCC','paddingBottom':'10px','font':'bolder 18px Arial'}},_title),
                    $CE('div',{'style':{'textAlign':'left','padding':'10px'}},_msg),
                    $CE('div',{'style':{'textAlign':'right','borderTop':'1px solid #CCC','paddingTop':'10px'}},[
                    	$CE('button',{'onclick':function(e){darkBackdrop(okDivID,false); document.body.removeChild($('ok' + okDivID));document.body.removeChild(iframeShim);return false;},'style':{'marginLeft':'10px'}},'OK')
                    ])
                ])
            ])
        ]);
    document.body.appendChild(okDiv);

	iframeShim = $CE('iframe',{'id':'divShim' + okDivID,"src":"javascript:false","scrolling":"no","frameborder":"0",'style':{'position':'absolute','top':okDiv.style.top,'left':okDiv.style.left,'height':okDiv.offsetHeight+'px','width':okDiv.offsetWidth+'px','zIndex':'9'}});

	document.body.appendChild(iframeShim);
	darkBackdrop(okDivID,true);
}

function darkBackdrop(_id,_bool) {
	if(_bool) {
		var arrayPageSize = getPageSize();
		backdropDiv = $CE('div',{'id':'editOverlay'+_id,'class':'editOverlay','style':{'display':'block','height':arrayPageSize[1]+'px','width':document.body.scrollWidth+'px'}});
		document.body.appendChild(backdropDiv);
	} else {
		document.body.removeChild(backdropDiv);
	}
}
/**************************\
 * Page Size/Scrool       *
\**************************/

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


/**************************\
 * List functions         *
\**************************/

var List = function(){
    return {
        defaultSeperator: ',',
        last: function(string, seperator){
            if(!seperator)seperator = this.defaultSeperator;
            var items = string.split(seperator);
            if(items.length > 0)
                return items[items.length-1];

            return null;
        },
        append: function(list, item, seperator){
            if(!seperator)seperator = this.defaultSeperator;
            return list + (list.length > 0 ? seperator : '') + item;
        },
        remove: function(list, index, seperator){
            if(!seperator)seperator = this.defaultSeperator;
            var items = list.split(seperator);
            if(items.length > index){
                items.splice(index, 1);
                return items.join(seperator);
            }

            return null;
        },
        indexOf: function(list, item, seperator){
            if(!seperator)seperator = this.defaultSeperator;
            var items = list.split(seperator);

            for(var i=items.length-1;i>=0;i--)
                if(items[i] == item)
                    return i;

            return -1;
        },
        length: function(string, seperator){
            if(!seperator)seperator = this.defaultSeperator;
            if(string.length == 0)return 0;

            var items = string.split(seperator);
            return items.length;
        },
        item: function(string, index, seperator){
            if(!seperator)seperator = this.defaultSeperator;
            var items = string.split(seperator);
            if(items.length > index)
                return items[index];
            return null;
        }
    };
} ();