//creates the ajax calls from page
function ajax ( parameters, updateID )
{
	//if the module/page is edit_page.php we needt oadd the module if its set..
	str = location.href ;
	if ( str.search("edit_page.php" ) > 0 )
	{
		obj =  JSON.encode ( parameters );
		obj = obj.substr ( 0, (obj.length) -1);
		//get variables
		var a_str = str.split("?");
		//alert ( a_str.length() ) ;
		a_vars = a_str[1].split ( '&' ) ;
		a_vars.each( function(el)
			{
				//get the variable anme and value to inject
				a_var = el.split("=");
				//inject the module number
				obj  = obj + ',"'+a_var[0]+'":"'+a_var[1]+'"';
			}
		);
		parameters = JSON.decode ( obj+'}' );
	}

	//note parameters is an array or the required values
	var myRequest = new Request.JSON(
		{
			useSpinner:true,
			update: updateID,
			onSuccess: function(responseJSON, responseText )
			{
			    eval ( responseJSON.script ) ;
			},
			onFailure: function(responseJSON, responseText)
			{
			    console.debug("failed:-");
			}
		}
	).get( parameters );

}

function tips_dynamic ()
{
	if ( $$('a.tips_d').length > 0 )
	{
		//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]);
			}
		);

		var tips = new Tips( $$('.tips_d'), {
			fixed:  true,
			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');
			}
		});
	}
}

function tips ()
{
	if ( $$('a.tips').length > 0 )
	{
		//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]);
			}
		);


		var tips = new Tips( $$('.tips'), {
			hideDelay : 200,
			fixed:  true,
			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');
			}
		});
	}
}

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 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/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')
					}
				});
			});
		});
	});
