getScrollOffSet = function()
{
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body

	var docsleft=document.all? iebody.scrollLeft : pageXOffset
	var docstop=document.all? iebody.scrollTop : pageYOffset

	return Array(docsleft,docstop);
}

// similar to php's in_array function
function in_array(needle, haystack)
{
	if(typeof(haystack) == 'object' && haystack != null)
	{
		for (var i = 0;i<haystack.length;i++)
		{
			if (haystack[i].toString()===needle.toString())
			{
				return true;
			}
		}
	}
	return false;
}


openWindow = function(url,w,h,tb,stb,l,mb,sb,rs,x,y)
{
	var t=(document.layers)? ',screenX='+x+',screenY='+y: ',left='+x+',top='+y; //A LITTLE CROSS-BROWSER CODE FOR WINDOW POSITIONING
	tb=(tb)?'yes':'no'; stb=(stb)?'yes':'no'; l=(l)?'yes':'no'; mb=(mb)?'yes':'no'; sb=(sb)?'yes':'no'; rs=(rs)?'yes':'no';
	var x=window.open(url, 'newWin'+new Date().getTime(), 'scrollbars=yes,width='+w+',height='+h+',toolbar='+tb+',status='+stb+',menubar='+mb+',links='+l+',resizable=yes');
	x.focus();
}

mouseX = function(evt)
{
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	return evt.clientX + (document.documentElement.scrollLeft ?
	document.documentElement.scrollLeft :
	document.body.scrollLeft);
	else return null;
}

mouseY = function(evt)
{
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	return evt.clientY + (document.documentElement.scrollTop ?
	document.documentElement.scrollTop :
	document.body.scrollTop);
	else return null;
}

findPosX = function(obj)
{
	if(typeof obj == 'string')
	obj = $(obj);

	var curleft = 0;
	if(obj.offsetParent)
	while(1)
	{
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.x)
	curleft += obj.x;
	return curleft;
}

findPosY = function(obj)
{
	if(typeof obj == 'string')
	obj = $(obj);

	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.y)
	curtop += obj.y;
	return curtop;
}

findPos = function(obj)
{

	curleft = findPosX(obj);
	curtop = findPosY(obj);
	return new Array(curleft,curtop);
}

switchsymbol = function(id,close,open,status)
{
	symb = $(id);

	if(close && open)
	{
		if(status)
		{
			if(status =='-')
			{
				symb.innerHTML = close;
			}else{
				symb.innerHTML = open;
			}

		}else{

			if(symb.innerHTML == open)
			{
				symb.innerHTML = close;
			}else{
				symb.innerHTML = open;
			}
		}
	}else{
		if(status)
		{
			if(status =='-')
			{
				symb.innerHTML = '[-]';
			}else{
				symb.innerHTML = '[+]';
			}

		}else{
			if(symb.innerHTML == '[+]')
			{
				symb.innerHTML = '[-]';
			}else{
				symb.innerHTML = '[+]';
			}
		}
	}

}

showhidediv = function(id)
{
	$(id).style.display=($(id).style.display!="block")? "block" : "none"
}

showdiv = function(id)
{
	$(id).style.display="block";
}

hidediv = function(id)
{
	$(id).style.display="none";
}

switchbgcolor = function(id,oncolor,offcolor)
{
	if($(id).style.background == oncolor)
	{
		$(id).style.background = offcolor;
	}else{
		$(id).style.background = oncolor;
	}
}

function createCookie(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=/";
}

readCookie = function(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;
}

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

showcrud = function()
{
	var items = $$('.crudlink');

	cookie = readCookie("crudlink");

	items.each(function(val,index){
		val.show();
	})
	createCookie("crudlink","on",7);

}

hidecrud = function()
{
	var items = $$('.crudlink');

	cookie = readCookie("crudlink");
	items.each(function(val,index){
		val.hide();
	})
	createCookie("crudlink","off",7);
}


function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function getWindowDimensions()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return[myWidth,myHeight]
}

removeflashmessagealert = function()
{
	if($('flashmessagealert'))
	document.body.removeChild($('flashmessagealert'))
}

flashmessagealert = function(msg)
{
	var flashmessage = document.createElement('div');
	flashmessage.setAttribute("id","flashmessagealert")
	flashmessage.style.position = 'absolute';

	flashmessage.className = 'flashmessagealert';

	flashmessage.innerHTML = msg;

	document.body.appendChild(flashmessage)

	var windowdimensions = getWindowDimensions();

	var IpopTop = (windowdimensions[1] - flashmessage.offsetHeight)/2;
	var IpopLeft = (windowdimensions[0] - flashmessage.offsetWidth)/2;

	flashmessage.style.left=IpopLeft + document.body.scrollLeft+"px";
	flashmessage.style.top=IpopTop + document.body.scrollTop+"px";

	Event.observe(flashmessage,'click',function(){removeflashmessagealert()});
	window.setTimeout("removeflashmessagealert()",2000);
}

displaywindowloader = function()
{
	var windowdimensions = getWindowDimensions();
	var scroll = getScrollXY();

	var IpopTop = (((windowdimensions[1]-19)/2)+scroll[1]);
	var IpopLeft = (((windowdimensions[0]-220)/2)+scroll[0]);

	//modalheight = (windowdimensions[1]+scroll[1])
	//modalwidth = (windowdimensions[0]+scroll[0])

	//html = '<div id="windowloader_modal" style="width:'+modalwidth+'px;height:'+modalheight+'px;">&nbsp;</div>';
	//new Insertion.Top(document.body, html);
	html = '<div style="position:absolute;left:'+IpopLeft+'px;top:'+IpopTop+'px" id="windowloader" class="windowloader"><img src="'+WEBDIR+'img/ajax-loader.gif"></div>';
	new Insertion.Top(document.body, html);

	$('windowloader').style.left=IpopLeft+"px";
	$('windowloader').style.top=IpopTop+"px";

	$('windowloader').observe('click',function(){$('windowloader').remove()})
	//$('windowloader_modal').observe('click',function(){$('windowloader_modal').remove()})
}

hidewindowloader = function()
{
	if(parent.$('windowloader'))
	{
		parent.$('windowloader').remove();
		//parent.$('windowloader_modal').remove();
	}else if($('windowloader')){
		$('windowloader').remove();
		//$('windowloader_modal').remove();
	}
}

create_tips = function ()
{
	var tip = "";

	$$("*").findAll(function(node){
		return node.getAttribute('title');
	}).each(function(node){
		tip = ""+node.title+"";
		new Tooltip(node,tip);
		node.removeAttribute("title");
	});
}

cookie_callback = function(){
	cookie = readCookie("crudlink");

	if(cookie=="off")
	hidecrud();

}
editinplace = function(url,obj,options)
{
	if(typeof(options)=='undefined')
	var options = [];

	if(typeof options['areaclass'] == 'undefined')
	options['areaclass'] = 'editinplacearea';

	if(typeof options['cancelclass'] == 'undefined')
	options['cancelclass'] = 'cancellink';

	if(typeof options['submitclass'] == 'undefined')
	options['submitclass'] = 'submitlink';

	if(typeof(obj)=='string')
	var obj = $(obj);

	var parent = obj.parentNode;
	var self = this;

	if(!$('ta_'+obj.id))
	{
		var ta = document.createElement('textarea');
		ta.className = options['areaclass'];
		ta.innerHTML = obj.innerHTML;
		ta.value = obj.innerHTML;
		ta.setAttribute('name','name');
		ta.setAttribute('id','ta_'+obj.id);
		parent.appendChild(ta);

		var cancel = document.createElement("input");
		cancel.className = options['cancelclass'];
		cancel.setAttribute('id','cancel_'+obj.id);
		cancel.setAttribute('type','button');
		cancel.onclick = '';

		$(cancel).onclick = function(){
			editinplace(url,""+obj.id+"",options);
		};

		cancel.value = 'cancel';
		parent.appendChild(cancel);

		var submit = document.createElement("input");
		submit.className = options['submitclass'];
		submit.setAttribute('id','submit_'+obj.id);
		submit.setAttribute('type','button');

		$(submit).onclick = function(){
			self.submitarea(url,obj);
		};

		submit.value = 'submit';
		parent.appendChild(submit);

		obj.style.display = 'none';
	}else{
		obj.innerHTML = $('ta_'+obj.id).value;
		parent.removeChild($('cancel_'+obj.id));
		parent.removeChild($('submit_'+obj.id));
		parent.removeChild($('ta_'+obj.id));
		obj.style.display = 'block';
	}

	submitarea = function(url,obj)
	{
		var querystring = '';
		querystring = "value="+$('ta_'+obj.id).value;
		new Ajax.Request(url,{postBody:querystring,onComplete:function(t) {flashmessagealert(t.responseText);editinplace(url,obj)}});
	}

}

var t;

var c=6;
var tt;
timedCount = function()
{
	if($('txt'))
	{
		$('txt').value=c;
		$('counterz').src = WEBDIR+'img/icons/'+c+'.gif';
		c=c-1;
		tt=setTimeout("timedCount()",1000);
	}
}
function stopCount()
{
	c = 6;
	clearTimeout(tt);
}

crudmenu = function(e,module,pass,fields,operations)
{

	if(!$('active_crud_menu'))
	{

		c = 6;

		windowdims = getWindowDimensions();

		if(!e.id)
		{
			// the element that triggered the event
			var element = Event.element(e);
			// gets the mouse position
			var mouseX = Event.pointerX(e),
			mouseY = Event.pointerY(e);
			// stop default behaviour and event propagation
			Event.stop(e);
		}else{
			mouseX = e.offsetTop;
			mouseY = e.offsetLeft;
		}

		var crudmenu = document.createElement("div");
		crudmenu.setAttribute("id","active_crud_menu");
		crudmenu.setAttribute("class","active_crud_menu");
		crudmenu.style.textAlign = 'left';
		crudmenu.style.position = 'absolute';
		crudmenu.style.zIndex = 9999;
		crudmenu.style.left = (mouseX-10)+"px";
		crudmenu.style.top = (mouseY-10)+"px";
		document.body.appendChild(crudmenu);


		t = window.setTimeout('rmcrud()',6000);

		Event.observe($('active_crud_menu'),'mouseover',function(){exptimeout()});
		new Ajax.Updater('active_crud_menu',WEBDIR+'structure/itemcontrol/'+module+'/'+pass+'/'+fields+'/'+operations,{onComplete : function(){timedCount()}});
	}else{

		Effect.Shake('active_crud_menu',{distance : 2.0,duration : .2});
		//				new Effect.Pulsate('active_crud_menu');

		x = $('active_crud_menu').offsetTop;
		y = $('active_crud_menu').offsetLeft;
		//		flashmessagealert('Crud Menu is Active at ('+x+'px - '+y+'px)');
	}
}

exptimeout = function()
{
	window.clearTimeout(t);
	stopCount();

	t = window.setTimeout('rmcrud()',6000);
	timedCount();
}

rmcrud = function()
{
	//	new Effect.Fade($("active_crud_menu"),{})
	//new Effect.SwitchOff('active_crud_menu')
	if($("active_crud_menu"))
	$("active_crud_menu").remove();
}

function a_crud_delete(module,pass,element_to_remove)
{
	new Ajax.Request(WEBDIR+'structure/delete/'+module+'/'+pass,{onComplete : function(t){parent.$(element_to_remove).remove();flashmessagealert(t.responseText);}})
}

function strstr (s, find) {
	var str = new String (s);
	var r = str.indexOf (find);
	return (-1 !== r);
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}


function showarticle(id,className)
{
	if(!className)
	className = 'article';

	$$('.'+className).each(function(val,index){
		if(val.id != id)
		val.style.display = 'none';
	})

	if($(id).style.display == 'none')
	{
		$(id).style.display = 'block';
	}else{
		$(id).style.display = 'none';
	}


}