var Browser = {
	IE: (document.all ? true : false),
	getVersion: function(){
		return parseFloat(navigator.appVersion.split('MSIE')[1]);
	},
	Safari:((navigator.userAgent.indexOf('Safari')!= -1)? true : false),
	Firefox:((!document.all&&window.XMLHttpRequest)? true : false),
	Opera:((typeof window.opera != 'undefined')? true : false),
	scrollbarWidth: function(){
		if(typeof this.scrollbarWidthStored != 'undefined'){
			return this.scrollbarWidthStored;
		}
		var wNoScroll = 0;
	    var wScroll = 0;

	    // Outer scrolling div, Start with no scrollbar
	    var a = $$.create('div');
		$$.style(a, 'position', 'absolute', 'top', '-1000px', 'left', '-1000px','width', '100px', 'height', '50px', 'overflow', 'hidden');
	    
	    // Inner content div
	    var b = $$.create('div');
		$$.style(b, 'width', '100%', 'height', '200px');

	    // Put the inner div in the scrolling div
	    $$.addChild(a, b);
	    // Append the scrolling div to the doc
	    $$.addChild(document.body, a);

	    // Width of the inner div sans scrollbar
	    wNoScroll = b.offsetWidth;
	    // Add the scrollbar
	    a.style.overflow = 'auto';
	    // Width of the inner div width scrollbar
	    wScroll = b.offsetWidth;

	    // Remove the scrolling div from the doc
	    document.body.removeChild(a);

	    // Pixel width of the scroller
		this.scrollbarWidthStored = wNoScroll - wScroll;
	    return this.scrollbarWidthStored;
	},
	windowSize: function(){
		//Firefox, mozilla, safari, opera, netscape
		var w = window.innerWidth;
		var h = window.innerHeight;

		if(typeof w == 'undefined'){
			try{
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}catch(e){
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		}
		return {x: w, y: h};
	},
	pngfix: function (img,w,h){
		if(Browser.IE && (Browser.getVersion() >= 5.5) && (Browser.getVersion() < 7) && (document.body.filters)){
			//only IE less than version 7
			var src = $$.getA(img, 'src');
			img.onload = null;
			if(!w && !h){
				w = parseInt(img.currentStyle.width, 10);
				h = parseInt(img.currentStyle.height, 10);
			}
			$$.style(img, 'width', w + 'px', 'height', h + 'px');
			var fimg = img.cloneNode(true);
			$$.setA(fimg, 'src', baseURL + 'graphics/blank.gif');
			$$.style(fimg, 'filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')");
			img.parentNode.replaceChild(fimg, img);
		}
	},
	dropdownfix: function(outerNode){
				
		if(Browser.IE && Browser.getVersion() == 6){
			var ddPlacesList = outerNode.childNodes[2];
			outerNode.onmouseout = function(e){
				var event = $$.event.parse(e);
				if(event.target.parentNode != this){
					$$.removeClass(this, 'hover');
				}
			};
			outerNode.onmouseover = function(e){
				$$.addClass(this, 'hover');
			};
		}
	},
	getTimezone: function (){
		var rightNow = new Date();
		var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
		var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
		var temp = jan1.toGMTString();
		var jan2 = new Date(temp.substring(0, temp.lastIndexOf(' ') - 1));
		temp = june1.toGMTString();
		var june2 = new Date(temp.substring(0, temp.lastIndexOf(' ') - 1));
		var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
		var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
		var dst;
		if (std_time_offset == daylight_time_offset) {
			dst = false; // daylight savings time is NOT observed
		}else{
			// positive is southern, negative is northern hemisphere
			var hemisphere = std_time_offset - daylight_time_offset;
			if (hemisphere >= 0){
				std_time_offset = daylight_time_offset;
			}
			dst = true; // daylight savings time is observed
		}
		
		return {
			'timezone': daylight_time_offset,
			'hasDst': dst
		};
	}
};

// DOM object reference
function $(id){return (typeof id == 'string')? document.getElementById(id) : id;}
function $tag(x, tag){return x.getElementsByTagName(tag);}

//DOM global object
var $$ = {
	create: function(t){
		var a = document.createElement(t);
		for(var i=1; i<arguments.length; i+=2){
			switch(arguments[i]){
				case 'class': $$.setClass(a,arguments[i+1]);break;
				case 'valign': a.vAlign=arguments[i+1];break;
				case 'align': a.align=arguments[i+1];break;
				case 'colspan': a.colSpan=arguments[i+1];break;
				case 'rowspan': a.rowSpan=arguments[i+1];break;
				case 'cellpadding': a.cellPadding=arguments[i+1];break;
				case 'cellspacing': a.cellSpacing=arguments[i+1];break;
				case 'valign': a.vAlign=arguments[i+1];break;
				case 'innerText': $$.text(a,arguments[i+1]);break;
				default: $$.setA(a, arguments[i], arguments[i+1]);break;
			}
		}
		return a;
	},
	show: function(){
		var s = '';
		var n = null;
		for(var i=0; i < arguments.length; i++){
			n = $(arguments[i]);
			switch(n.tagName){
				case 'DIV': case 'UL': s = 'block';break;
				case 'TABLE': s = 'table';break;
				case 'TR': s = 'table-row';break;
				case 'TD': s = 'table-cell';break;
				case 'TABLE': s = 'table';break;
				case 'TBODY': case 'THEAD': s = 'table-row-group';break;
				default:
					s = 'inline';
			}
			try{n.style.display = s;}catch(e){n.style.display = 'block';}
		}
	},
	hide: function(){
		for(var i = 0; i < arguments.length; i++){
			$(arguments[i]).style.display = 'none';
		}
	},
	chop: function(){
		var n = '';
		for(var i = 0; i < arguments.length; i++){
			n = $(arguments[i]);
			n.parentNode.removeChild(n);
		}
	},
	toggle: function(n, os, oh){
		//node, onShow, onHide
		n = $(n);
		if(n.style.display == 'none'){
			$$.show(n);
			if(os){os();}
		}else{
			$$.hide(n);
			if(oh){oh();}
		}
	},
	toggleForce: function(n, tf){
		if(tf){
			$$.show(n);
		}else{
			$$.hide(n);
		}
	},
	opacity: function(n, o){
		n = $(n);
		n.style.opacity = o;
		if(Browser.IE){
			n.style.filter = 'alpha(opacity='+(o*100)+')';
			n.style.zoom = "1";//to force hasLayout
		}else{
			n.style.MozOpacity = o;
			n.style['-moz-opacity'] = o;
		}
	},
	setClass: function(n, c){
		$$.setA(n, 'class', c);
		$$.setA(n, 'className', c);
	},
	addClass: function(n, c){
		n = $(n);
		var cs = n.className.split(' ');
		var nc = '';
		for(var i in cs){
			//check if this class is already added
			if(c == cs[i]){
				return;
			}
			nc += cs[i] + ' ';
		}
		$$.setClass(n, nc + ' ' + c);
	},
	removeClass: function(n, c){
		n = $(n);
		var cs = n.className.split(' ');
		var cn = '';
		for(var i in cs){
			if(c !== cs[i]){//check if this class is NOT being removed
				cn += cs[i] + ' ';
			}
		}
		$$.setClass(n, cn);
	},
	replaceClass: function (n, classToRemove, classToAdd){
		n = $(n);
		classToAdd = (classToAdd) ? classToAdd : '';
		var currClasses = n.className.split(' ');
		var newClasses = '';
		var repl = false;
		var numClasses = currClasses.length;
		for(var i = 0; i < numClasses; i++){
			var currClass = '';
			if (currClasses[i] != classToRemove) {
				if(currClasses[i] == classToAdd){
					currClass = classToAdd;
					repl = true;
				}else{
					currClass = currClasses[i];
				}
			}else{
				currClass = classToAdd;
				repl = true;
			}
			newClasses += currClass + ' ';
		}
		if (!repl){ newClasses += classToAdd;}
		$$.setClass(n, newClasses);
	},
	toggleClass: function(n, isAdd, classToChange){
		if(isAdd){
			$$.addClass(n, classToChange);
		}else{
			$$.removeClass(n, classToChange);
		}
	},
	style: function(n){
		n = $(n);
		var l = arguments.length;
		for(var i=1; i < l; i += 2){
			n.style[arguments[i]] = arguments[i + 1];
		}
	},
	addChild: function(n){
		n = $(n);
		var t = false;
		for(var i=1; i<arguments.length; i++){
			var t = arguments[i];
			if(typeof(t) == 'string'){
				n.appendChild(document.createTextNode(t));
			}else{
				n.appendChild(t);
			}
		}
	},
	text: function(n, t){
		n = $(n);
		while(n.childNodes.length){
			n.removeChild(n.firstChild);
		}
		if(typeof(t) == 'string'){
			n.appendChild(document.createTextNode(t));
		}else{
			n.appendChild(t);
		}
	},
	size: function(n){
		n = $(n);
		return {x: n.offsetWidth, y: n.offsetHeight};
	},
	position: function(n){
		n = $(n);
		var dx = n.offsetLeft;
		var dy = n.offsetTop;
		if(n.offsetParent){
			while( n = n.offsetParent){
				dx += n.offsetLeft;
				dy += n.offsetTop;
			}
		}
		return{x: dx, y: dy};
	},
	scrollTo: function(n, pos, notParentNode){
		n = $(n);
		var p = notParentNode? $(notParentNode) : n.parentNode;
		var a = n.offsetTop + n.offsetHeight;
		var b = p.scrollTop + p.offsetHeight;
		var c = (n.offsetTop > p.scrollTop);
		switch(pos){
			case 'bot':
				if (c && a < b){return;}
				var newScroll = p.offsetTop - p.offsetHeight + a;
				break;
			case 'top':
				if (c && a < b){return;}
			default:
				var newScroll = n.offsetTop - p.offsetTop;
		}
		p.scrollTop = newScroll;		
	},
	//get attribute
	getA: function(n, a){
		return $(n).getAttribute(a);
	},
	//set attribute
	setA: function(n, a, v){
		$(n).setAttribute(a, v);
	},
	//get value
	getV: function(n){
		n = $(n);
		if(n.placeholder && n.value == n.placeholder){
			return '';
		}
		return n.value;
	},
	//set value
	setV: function(n, v){
		n = $(n);
		n.value = v;
		if(n.placeholder){
			Placeholder.blur(n);
		}
	},
	event:{
		parse: function(e){
			if(!e){e = window.event;}
			var t = false;
			if (e.target){
				t = e.target;
			}else if (e.srcElement){
				t = e.srcElement;
			}
			if (t.nodeType == 3){ // defeat Safari bug
				t = t.parentNode;
			}
			return {event:e, target:t};
		},
		cancel: function(e){
			e = e ? e : window.event;
			if(e.stopPropagation){e.stopPropagation();}
			e.cancelBubble = true;
			return true;
		}
	}
};
