//creates teh ajax calls fro page
function ajax ( parameters, updateID )
{
	//note parameters is an array or the required values
	var myRequest = new Request.JSON(
		{
			useSpinner:true,
			update: updateID,
			onRequest: function() 
			{ 
				// $(document.body).setStyle("cursor", "url(/images/move-spinner.gif), auto" ) ;
			}, 
			onSuccess: function(responseJSON, responseText ) 
			{
				eval ( responseText ) ;
				// $(document.body).setStyle("cursor", "auto" ) ;
			},
			onFailure: function(responseJSON, responseText) 
			{ 
				// $(document.body).setStyle("cursor", "auto" ) ;
				alert ( "Failed") ;
			}
		}
	).get( parameters ); 
	
}
		
function tips_dynamic ()
{
	//mootools tooltips with fades
	$$('a.tips_d').each( function (element,index) 
		{
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
		}
	);
	if ( $('a.tips') != null )
	{
		var tips = new Tips( $$('.tips_d'), {
			timeOut : 700,
			maxTitleChars : 50,
			hideDelay : 200,
			className : 'tipsbody', //for you yo use in the css classes being then .tip .tip-title and .tip-text
			initialize: function() { this.tip.fade('hide'); },
			onShow : function(tip){ tip.fade('in'); },
			onHide: function(tip) { tip.fade('out'); 
			}
		});
		tips.tip.set('tween', {duration: 200} );
	}
}

function tips ()
{
	//mootools tooltips with fades
	$$('a.tips').each( function (element,index) 
		{
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
		}
	);
	
	if ( $('a.tips') != null )
	{
		var tips = new Tips( $$('.tips'), {
			timeOut : 700,
			maxTitleChars : 50,
			hideDelay : 200,
			className : 'tipsbody', //for you yo use in the css classes being then .tip .tip-title and .tip-text
			initialize: function() { this.tip.fade('hide'); },
			onShow : function(tip){ tip.fade('in'); },
			onHide: function(tip) { tip.fade('out'); 
			}
		});
		tips.tip.set('tween', {duration: 200} );
	}
}

function clickclear (thisfield, defaulttext)
{
	if (thisfield.value == defaulttext)
	thisfield.value = "";
	}

function clickrecall (thisfield, defaulttext)
{
	if (thisfield.value == "")
	thisfield.value = defaulttext;
	if ( $('update') )
	$('update').remove() ;
}
 
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

	
//used to call alert in admin for deletion
function remove_confirm (id)
{
	if ( confirm ( 'Delete This Item?') )
	{
		$("ID_"+id).dispose();
		// delete item from DB and page..
		xajax_remove(id);
	}
}

function openInNewWindow() 
{
	var newWindow = window.open (this.getAttribute('href'), '_poped', 'width=800,height=800');
	newWindow.focus();
	return false;
}



function getNewWindowLinks() 
{
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild)
	{
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var objWarningText;
		var strWarningText;
		var link;
		
		var links = $$('a.pop'); //anchors to look for with class
		
		for (var i = 0; i < links.length; i++) 
		{
			link = links[i];
			
			// Create an em element containing the new window warning text and insert it after the link text
			objWarningText = document.createElement("em");
			strWarningText = document.createTextNode(strNewWindowAlert);
			objWarningText.appendChild(strWarningText);
			link.appendChild(objWarningText);
			link.onclick = openInNewWindow;
		}
	
		objWarningText = null;
	}
}
window.addEvent('onload', getNewWindowLinks ( ) ) ;


	
	function load_init ( )
	{
		//run these functions when dom is ready 
		//check for any functions that run on the page . . 
		if (typeof page_functions=="function")
		{	
			page_functions ();
		}
		
		if (typeof h2_fade == "function")
		{
			h2_fade ( '#f0f' );
		}
		
		// auto slisder for one form on teh page..
		if ( document.getElementById("mainForm-opener") != null )
		{
			Slide_myforms = new Fx.Slide('mainForm', { 'duration': 1000, 'transition': Fx.Transitions.circOut  } );
			Slide_myforms.hide();
	
			$('mainForm-opener').addEvent('click', function(e)
			{
				e = new Event(e);
				Slide_myforms.toggle();
				e.stop();
			});	
		}
		
	}
		
	var alreadyrunflag=0 //flag to indicate whether target function has already been run

	if (document.addEventListener)
	  document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; load_init() }, false)
	else if (document.all && !window.opera)
	{
	  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
	  var contentloadtag=document.getElementById("contentloadtag")
	  contentloadtag.onreadystatechange=function()
	  {
		if (this.readyState=="complete")
		{
		  alreadyrunflag=1;
		  load_init();
		}
	  }
	}
	

	window.onload=function()
	{
		setTimeout("if (!alreadyrunflag){ load_init();}", 0) ;
	}
	
	function submitMe ( id )
	{	
		$(id).submit();
	}
	
	function fade_text ( c1, id )
	{
		el =  $(id);
		var myTween = new Fx.Tween (el, { 'duration': 8000, 'link':'chain',
				onComplete: function(el)
				{
					 new Fx.Slide (el, {duration: 1500, onComplete: function ( id ) { el.dispose(); } } ).toggle();
				}
		} ) ;
		myTween.start( 'color', c1 );
	}
	


//ajax spinners		 
window.addEvent('domready',function() {
	//inject image
	var spinner = new Element('img',{
		src: '/images/move-spinner.gif',
		styles: {
			position:'absolute'
		},
		opacity: 0
	}).inject(document.body);
	//form element events
	$$('.ajax').each(function(el) {
		//get coordinates
		var coords = el.getCoordinates(document.body);
		//ajax request object
		var request = new Request({
			url: '',
			method: 'post',
			onRequest: function() {
				spinner.setStyles({
					left: coords.right + 10,
					top: coords.top + 5
				}).fade('in');
			},
			onComplete: function() {
				spinner.fade('out');
			}
		});
		//change event
		el.addEvent('change',function() {
			//ajax request
			request.send({
				data: {
					ajax: 1,
					value: el.get('value')
				}
			});
		});
	});
});
