/* global vars */
	var slide_busy 	= false;
	var months_arr = new Array('','January','February','March','April','May','June','July','August','September','October','November','December');

/* sitewide functions */

	function pExists(property) {
	  return (typeof property != 'undefined');
	}

	function set(div_id,val){
		eID(div_id).value = val;
	}

	function win_specs(){ // required by hover & popup
		return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? 
			document.documentElement:
			document.body;
	}
	
	function get_y(obj) {
		if(obj.y) {
			y_coord = obj.y;
		}
		else if(obj.offsetParent){
			var y_coord = 0;
			while (obj.offsetParent) {
				y_coord += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}	
		return y_coord;
	}
	
	function get_x(obj) {
		if(obj.x) {
			x_coord = obj.x;
		}
		else if(obj.offsetParent){
			var x_coord = 0;
			while (obj.offsetParent) {
				x_coord += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		}
		return x_coord;
	}
	
	function eID(id){
		var ea;
		for( var i = 0; i < arguments.length; i++ ) {
			var e = arguments[i];
			if( typeof e == 'string' ) e = document.getElementById(e);
			if( arguments.length == 1 ) return e;
			if( !ea ) ea = new Array();
			ea[ea.length] = e;
		}
		return ea;
	}
	
	function mouseX(event){
		return event.pageX||(event.clientX+(document.documentElement.scrollLeft||document.body.scrollLeft));
	}
	
	function mouseY(event){
		return event.pageY||(event.clientY+(document.documentElement.scrollTop||document.body.scrollTop));
	}
	
	function answer(id){
		eID(id).className = (eID(id).className == 'box_answ')?'box_answ_a':'box_answ';
	}
	
	function set_cookie(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 niceTime(seconds){
		var hours = parseInt(seconds/3600);
		var mins = parseInt(((seconds/60) % 60));
		var secs = parseInt(seconds % 60);
		var string = '';
		if(hours!=0)(hours<10)?string += '0'+hours+'.':string += hours+'.';
		else string += '00.';
		if(mins!=0)(mins<10)?string += '0'+mins+':':string += mins+':';
		else string += '00:';
		if(secs!=0)(secs<10)?string += '0'+secs:string += secs;
		else string += '00';	
		return string;	
	}
	
	function isIE(){
		var nav=navigator.userAgent.toLowerCase();
		var isOpera=(nav.indexOf("opera")!=-1);
		return (nav.indexOf("msie")!=-1)&&!isOpera?true:false;
	}

/* effects */

	function hide(object) {	
		for( var i = 0; i < arguments.length; i++ ) {
			var element = eID(arguments[i]);
			if (element && element.style) element.style.display = 'none';
		}
	}
	
	function show(object) {
		var display_set = (arguments[1])?arguments[1]:'block';
		for( var i = 0; i < arguments.length; i++ ) {
			var element = eID(arguments[i]);
			if (element && element.style) element.style.display = display_set;
		}
	}

	function show_i(object) {	
		eID(object).style.display = 'inline';	
	}
	
	// to be replaced by script.aculo.us stuff soon
	function fader_init(id, opacStart, opacEnd, millisec) { 
		//speed for each frame 
		var speed = Math.round(millisec / 100); 
		var timer = 0; 
	
		//determine the direction for the blending, if start and end are the same nothing happens 
		if(opacStart > opacEnd) { 
			for(i = opacStart; i >= opacEnd; i--) { 
				setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
				timer++; 
			} 
		} else if(opacStart < opacEnd) { 
			for(i = opacStart; i <= opacEnd; i++) { 
				setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
				timer++; 
			} 
		} 
	}
	
	function changeOpac(opacity, id) { 
		var object = eID(id).style;
		object.opacity = (opacity / 101);
		object.filter = "alpha(opacity=" + opacity + ")";
		if(opacity == 0){
			hide(id);
			object.opacity = 100;
			object.filter = "alpha(opacity=100)";
		}
	}
		
	function updateScroller(container_id,content_id,scontainer_id,scroller_id){
		//collect the variables
		var docH = eID(content_id).offsetHeight;
		var contH = eID(container_id).offsetHeight;
		eID(scontainer_id).style.display = 'block';
		eID(scontainer_id).style.height = contH+'px';
		var scrollAreaH = eID(scontainer_id).offsetHeight;
		  
		//calculate height of scroller and resize the scroller div
		//(however, we make sure that it isn't too small for long pages)
		var scrollH = (contH * scrollAreaH) / docH;
		if(scrollH < 15) scrollH = 15;
		eID(scroller_id).style.height = parseInt(scrollH) + 'px';
		
		//what is the effective scroll distance once the scoller's height has been taken into account
		var scrollDist = parseInt(scrollAreaH-scrollH);
		
		//make the scroller div draggable
		Drag.init(eID(scroller_id),null,0,0,0,scrollDist);
		
		//add ondrag function
		eID(scroller_id).onDrag = function (x,y) {
		  var scrollY = parseInt(eID(scroller_id).style.top);
		  var docY = 0 - (scrollY * (docH - contH) / scrollDist);
		  eID(content_id).style.top = parseInt(docY) + 'px';
		}
	}

/* image preloaders */
	function MM_preloadImages() { 
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}
	

/* string manipulators and string input length check */
	function trim(string) {
		while (string.substring(0,1) == ' '){
			string = string.substring(1, string.length);
		}
		while (string.substring(string.length-1, string.length) == ' '){
			string = string.substring(0,string.length-1);
		}
		return string;
	}

	function pwdverify(textObject){
		 var filter = new RegExp(/^[a-zA-Z0-9]+$/);
		 return filter.test(textObject);
	}

	function textverify(textObject){
		 var filter = new RegExp(/^[a-zA-Z\' ]+$/);
		 return filter.test(textObject);
	}
	
	function make_popup(obj){
		if(!eID('pop_UP')){
			var box_popup = document.createElement('div');
			box_popup.setAttribute('id', 'pop_UP');
			document.body.appendChild(box_popup);
			
			eID('pop_UP').innerHTML				= 	'<table border="0" cellspacing="0" cellpadding="0">'
												+	  '<tr>'
												+		'<td class="NW"></td>'
												+		'<td class="N"></td>'
												+		'<td class="NE"></td>'
												+	  '</tr>'
												+	  '<tr>'
												+		'<td class="W"></td>'
												+		'<td class="core"><div id="pop_UP_core"></div></td>'
												+		'<td class="E"></td>'
												+	  '</tr>'
												+	  '<tr>'
												+		'<td class="SW"></td>'
												+		'<td class="S"></td>'
												+		'<td class="SE"></td>'
												+	  '</tr>'
												+	'</table>';
		} else {
			document.body.removeChild(eID('pop_UP'));
			make_popup(obj);
		}
		position_popup(obj);
	}
	
	function position_popup(obj)
	{
		if(eID('pop_UP'))
		{
			
			var x = get_x(obj);
			var y = get_y(obj);
			
			if(!arguments[1])
			{
				var popup_W = eID('pop_UP').offsetWidth;
				var popup_H = eID('pop_UP').offsetHeight;
				
				
				
				var pageDimensions = getPageSize();
				
				var tent_X = x - (popup_W/2);
				var tent_Y = y - (popup_H/2);
				
				
				if(tent_X < 5){
					tent_X = x;
				}
				else if((tent_X + popup_W) > (pageDimensions[0]-5)){
					tent_X = (pageDimensions[0]-5) - popup_W;
				}
				
				if(tent_Y < 5){
					tent_Y = y;
				}
				else if((tent_Y + popup_H) > (pageDimensions[1]-5)){
					tent_Y = (pageDimensions[1]-5) - popup_H;
				}
			} else {
				var tent_X = x - (arguments[1]+5);
				var tent_Y = y - 2;
			}
			
			eID('pop_UP').style.top = tent_Y+'px';
			eID('pop_UP').style.left = tent_X+'px';
			
			show('pop_UP');
		}
	}
	
	function g_search_process()
	{
		window.open('http://www.google.com/custom?'+encodeForm('ddp_google_search'),'google_window','width=800,height=600,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes')
		return false;
	}
	
	function encodeForm(formname){
		var form = document.forms[formname];
		var codedString='';
		var ampersand='';
		var formLength=form.elements.length;
		for(index=0;index<formLength;index++){
			var element=form.elements[index];
			if(!element.name||element.name==''||element.name=='undefined'||!element.value||element.value==''){continue;}
			switch(element.type){
				case'text':
				case'hidden':
				case'password':
				case'textarea':
				case'select-one':
					codedString+=ampersand+element.name+'='+encodeURIComponent(trim(element.value));
					break;
				case'radio':
				case'checkbox':
					if(element.checked)
					codedString+=ampersand+element.name+'='+encodeURIComponent(element.value);
					break;
			}
			ampersand='&';
		}
		return codedString;
	}
	
	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;
	}
	
/****** MENU DROP DOWN ********/
var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = eID(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;