/*
 * Copyright © 20011, Roelof van Schalkwyk
 * 
 * All rights reserved.
 * 
 * Author: Roelof van Schalkwyk
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * @copyright  Copyright © 2011 Roelof van Schalkwyk
 */

//used to pause a script
function wait(milliseconds)
{
	var date = new Date();
	var curDate = null;
	
	do {
		curDate = new Date();
	}
	while (curDate - date < milliseconds);
}

function isAlnum(aChar)
{
   return (isDigit(aChar) || isAlpha(aChar));
}

function isDigit(aChar)
{
   myCharCode = aChar.charCodeAt(0);

   if((myCharCode > 47) && (myCharCode <  58))
   {
      return true;
   }

   return false;
}

function isAlpha(aChar)
{
   myCharCode = aChar.charCodeAt(0);

   if(myCharCode > 64 && myCharCode <  91)
   {
      return true;
   }

   return false;
}

//perform the equivalent of the php operation by creating an element and pulling out the parsed html (mainly for debugging)
function htmlspecialchars(string)
{
	return $('<span>').text(string).html();
}

function floatBelow(fixedSelector,floatSelector)
{
	var fixed = $(fixedSelector);
	var float = $(floatSelector);

	pos = fixed.offset();
	width = fixed.outerWidth();
	height = fixed.outerHeight();

	float.css({
			position: "absolute",
			display: "block",
			top: pos.top + height,
			left: pos.left,
			width: width,
			height: "auto",
			padding: 0,
			marginTop: 0,
			marginLeft: 0,
			//@todo customise background?
			//background: "#FFFFFF",
			zIndex: 100
		});
	
	//ie6 fix
	float.bgiframe();
}

//hide a popped element
function hideFloating(selector)
{
	var el = $(selector);
	
	el.css({
		width: 0,
		height: 0,
		padding: 0,
		margin: 0,
		border: 0,
		display: "none"
	});
}

function setupAutoCompleteMatches(float,fixed)
{
	var input = $(fixed);
	var el = $(float);
	
	hideFloating(float);
	
	input.bind("focus",
			function () {
				floatBelow(fixed,float);
				//@todo shouldn't this rather just assign a className to allow customisation?
				/*el.css({
					border: "2px solid gray",
					padding: "3px 5px",
					marginTop: "1px",
					marginLeft: "3px",
					background: "#FFFFFF",
					listStyleType: "none",
					cursor: "pointer"
				});*/
			}
		);
	
	input.bind("blur",
		function () {
			//by fading the lis there is time for the click to happen before they disappear..
			//@todo rather check the position of the mouse and don't hide if it's over the lis?
			el.fadeOut(500);
		}
	);
	
	setupAutoCompleteClicks(float);
}

function setupAutoCompleteClicks(float)
{
	var el = $(float);
	
	//add the click handlers
	//this assumes that the ul will be the element after the input - if this changes it won't work!
	//@todo this is a dodgy way of finding the element
	el.children("ul").children("li").each(
		function () {
			var val = $(this).children(".val").text();
			$(this).bind("click",
				function () {
					$(this).parent().parent().prev().val(val);
					//el.remove();
					hideFloating(float);
				}
			);
		}
	);
}

function addLightbox(selector)
{
	$(selector).lightBox({
		imageLoading: uriBase+"images/lib/lightbox-ico-loading.gif",
		imageBtnClose: uriBase+"images/lib/lightbox-btn-close.gif",
		imageBtnPrev: uriBase+"images/lib/lightbox-btn-prev.gif",
		imageBtnNext: uriBase+"images/lib/lightbox-btn-next.gif",
		imageBlank: uriBase+"images/lib/lightbox-blank.gif",
		fixedNavigation:true
	});
}

function addCLEditor(selector,width)
{
	$(selector).cleditor({
		width: width
	});
}

function loadDatePicker(selector)
{
	Date.format = 'yyyy-mm-dd';
	$(selector).datePicker().val(new Date().asString()).trigger('change');
}

function pulseAndFade(selector)
{
	$(selector).fadeTo(750,0.25,
		function() {
			$(this).fadeTo(1000,1,
				function() {
					setTimeout(
						function() {
							$(selector).fadeTo(1500,0,
								function() {
									$(this).slideUp(250);
								}
							);
						},
						3000
					);
				}
			);
		}
	);
}

function suckerFishIE6(ulSelector,hoverClass)
{
	$(ulSelector).find('ul').bgiframe();
	
	$(ulSelector+" li").mouseover(
		function (event) {
			$(this).addClass(hoverClass);
		}
	);
	$(ulSelector+" li").mouseout(
		function (event) {
			$(this).removeClass(hoverClass);
		}
	);
}

//run a set jquery functions (specified as an array of strings) on a given selector
function runJQuery(selector,stmts)
{
	var r = "$(\""+selector+"\").";
	for (var i=0;i<stmts.length;i++) {
		eval(r+stmts[i]);
	}
}
