/** ALLPRO extensions for jQuery library
*
*
* @Last Update: 2009-11-17 19:00
*/
var test_Img;

/**
 * $$
 *
 * @description Wrapper function for jQuery $() selector
 * @returns Regular DOM Object, or array of Objects, instead of a jQuery object - null if none
 */
function $$ () {
	// wrapper for jQuery $() 
	// returns "normal" DOM Object(s) instead of a jQuery object
	// equivalent to $([arguments]).elem() - also an extension
	var jObj=null;
	switch (arguments.length) {
		case 0:  jObj=null; break;
		case 1:  jObj=jQuery(arguments[0]); break;
		case 2:  jObj=jQuery(arguments[0],arguments[1]); break;
		case 3:  jObj=jQuery(arguments[0],arguments[1],arguments[2]); break;
		default:  jObj=jQuery(arguments[0],arguments[1],arguments[2],arguments[3]); break;
	}
	return (jObj==null) ? null : jObj.get();
}


jQuery.fn.first = function () {
	return this [0];
}
jQuery.fn.last = function () {
	return (this.length) ? this [this.length-1] : this [0];
}


/**
 * DOM Equivalent Node Commands (various)
 */
function _validateNode (node, f_OnlyReturnNodeIfAnElmentObject) {
	return (!f_OnlyReturnNodeIfAnElmentObject || (elem && elem.nodeType==1 && elem.nodeName.substr (0,1) !="#")) ? $(node) : $();
}

jQuery.fn.hasChildren = function (f_AnyElem) {
	var elem=this[0];
	if (!elem) {return 0;}
	return (f_AnyElem) ? elem.childNodes.length : elem.children.length; // 'children' inlude ONLY 'tag elements'
}
jQuery.fn.firstChild = function (s) {
	if (typeof s=="string") {return this.children (s)[0];}
	this.children().each (function(){
		if (this.nodeType==1) {return $(this);}
		alert ("this.nodeType = " + this.nodeType );
	});
	
	var elem=this [0];
	if (!elem) {return;}
	var child=(f_AnyElem) ? elem.firstChild : elem.children [0];
	return $(child);
}
jQuery.fn.lastChild = function (f_AnyElem) {
	var elem=this [0]; if (!elem) {return;}
	var child=(f_AnyElem) ? elem.lastChild : elem.children [elem.children.length-1];
	return $(child);
}

jQuery.fn.hasSiblings = function (f_AnyElem) {
	var elem=this[0];
	if (!elem) {return 0;}
	var parent=elem.parentNode;
	return (f_AnyElem) ? parent.childNodes.length-1 : parent.children.length-1; // 'children' inlude ONLY 'tag elements'
}
jQuery.fn.firstSibling = function (f_AnyElem) {
	var elem=this [0];
	if (!elem) {return;}
	var parent=elem.parentNode;
	var sibling=(f_AnyElem) ? parent.firstChild : parent.children [0];
	return $(sibling);
}
jQuery.fn.lastSibling = function (f_AnyElem) {
	var elem=this [0];
	if (!elem) {return;}
	var parent=elem.parentNode;
	var sibling=(f_AnyElem) ? parent.lastChild : parent.children [parent.children.length-1];
	return $(sibling);
}
jQuery.fn.nextSibling = function (f_AnyElem) {
	var elem=this [0];
	if (!elem) {return;}
	var sibling=(f_AnyElem) ? elem.nextSibling : elem.nextSibling; // TODO :  Finish this
	return $(sibling);
}
jQuery.fn.previousSibling = function (f_AnyElem) {
	var elem=this [0];
	if (!elem) {return;}
	var sibling=(f_AnyElem) ? elem.previousSibling : elem.previousSibling; // TODO :  Finish this
	return $(sibling);
}


/**
 * iif
 *
 * @description Evaluates a true/false value
 * @returns jQuery object if b_Value is True, NULL otherwise
 */
jQuery.fn.iif = function (b_Value) {
	return (b_Value) ? this : $(); // return an 'empty' jQuery object if test fails
};
jQuery.fn.IIf = function (b_Value) {return this.iif (b_Value);} // ALT capitalization of iif()


/**
 * ifHasClass
 *
 * @description Chainable version of hasClass
 * @returns jQuery object if this.hasClass is True, NULL otherwise
 */
jQuery.fn.ifHasClass = function (s_Class) {
	return (this.hasClass (s_Class)) ? this : $(); // return an 'empty' jQuery object if test fails
};

/**
 * hasClass
 *
 * @description Check for 1 OR MORE classNames in element (Not case-sensitive)
 * @returns True if is valid object and ALL classNames are found - false otherwise
 */
jQuery.fn.hasClass = function (s_Class) {
	// check for 1 or more classNames - return true is ALL are found!
	var elem=this [0];
	if (s_Class==null || s_Class=="" || !elem) {return false;}
	if (typeof elem !="object" || typeof elem.className=="undefined") {return false;}

	s_Class=s_Class.toLowerCase()
	var arr_FindClasses=[s_Class]; // initialize array as a 'single className'
	var sep=[",",";","|"," "]; // all valid className separators ('spaces' LAST)
	for (var i=0; i < sep.length; i++) {
		if (s_Class.indexOf (sep[i]) >0) {arr_FindClasses=s_Class.split (sep[i]); break;}
	}
	$.each (arr_FindClasses, function (i) {arr_FindClasses [i]=$.trim (arr_FindClasses [i]);} );

	var arr_HasClasses=elem.className.toLowerCase().split (" ");
	$.each (arr_HasClasses, function (i) {arr_HasClasses [i]=$.trim (arr_HasClasses [i]);} );

	// if ANY class is not found, then must return false;
	var is_ClassFound;
	for (var f=0; f < arr_FindClasses.length; f++) {
		is_ClassFound=false; // reset before each loop
		for (var h=0; h < arr_HasClasses.length; h++) {
			if (arr_HasClasses [h]==arr_FindClasses [f].toLowerCase()) {
				is_ClassFound=true; break; // found it - find next class OR we are done (successfully)
			}
		}
		if (is_ClassFound==false) {break;} // didn't find the previous class, so no need to continue
	}
	return is_ClassFound;
}


/**
 * isTrue
 *
 * @description Parses property value to check for any valid string that represents 'true'
 * @returns True if property is 'true', 'yes', 'show' or any positive number
 */
jQuery.fn.isTrue = function (s_Property) {
	// evaluate criteria for all possible values that mean 'true'
	s_Property=s_Property.toLowerCase();
	var criteria=["1","true","yes","show"];
	for (var i=0; i <criteria.length; i++) {
		//if (this.is ("["+s_Property+"="+criteria[i]+"]"))  {return true;}
	}
	return ((s_Property*1) > 0) ? true : false;
}


/**
 * srcToggle
 *
 * @description Toggle an image by replacing the 'src' with alternate version
 * @returns jQuery object - Chainable method
 */
jQuery.fn.srcToggle = function (s_New, s_Old) {
	try {
		if (!s_Old) {
			if (s_New==1) {s_New="on";}
			if (s_New==0) {s_New="off";}
			switch (s_New) {
				case "on": s_Old="off"; break;
				case "off": s_Old="on"; break;
				case "up": s_Old="down"; break;
				case "down": s_Old="up"; break;
				case "left": s_Old="right"; break;
				case "right": s_Old="left"; break;
				default: return this;
			}
		}

		if (s_Old.substr (0,1) !="_" && s_Old.substr (0,1) !="-") {
			s_Old="_"+s_Old;
		}
		if (s_New.substr (0,1) !="_" && s_New.substr (0,1) !="-") {
			s_New="_"+sNew;
		}

		var src=this.src();
		var pos=src.indexOf (s_Old);
		if (pos >=0) {
			this.src (src.substr (0,pos) + s_New + src.substr (pos+s_Old.length+1));
		}

	}
	catch (e) {}
	finally {return this;}
}


/**
 * imgPreload
 *
 * @description Change Image Source - optionally using a 'transition effect'
 * @returns jQuery object - Chainable method
 */
jQuery.imgPreload = function (sa_Images, s_ImgFolder, sa_ImagesLoaded) {
	if (typeof sa_Images=="string") { // delimited string - convert to array
		var sep=[",",";","|"," "]; // all valid separators ('spaces' LAST)
		for (var i=0; i < sep.length; i++) {
			if (sa_Images.indexOf (sep[i]) >0) {sa_Images=sa_Images.split (sep[i]); break;}
		}
		if (typeof sa_Images=="string") sa_Images=[sa_Images]; // 'single image'
	}
	if (s_ImgFolder && s_ImgFolder.substr (s_ImgFolder.length-1) !="/") {
		s_ImgFolder+="/";
	}
	$.imgPreload_DoIt (sa_Images, 0, s_ImgFolder, sa_ImagesLoaded);
}

/**
 * imgPreload_DoIt
 *
 * @description Child process of imgPreload - calls self recursively to preload each image
 * @returns Nothing
 */
jQuery.imgPreload_DoIt = function (sa_Images, idx, s_ImgFolder, sa_ImagesLoaded) {
	var s_Img;
	while (idx < sa_Images.length && !s_Img) {
		s_Img=$.trim (sa_Images [idx]);
		idx++;
	}
	if (s_Img) {
		var o_Img=document.createElement ("img");
		if (sa_ImagesLoaded) {
			o_Img.onload=function() {
				$.imgPreload_DoIt (sa_Images, idx, s_ImgFolder, sa_ImagesLoaded);
				sa_ImagesLoaded [idx-1]=s_ImgFolder+s_Img; // recreate ORIGINAL ORDER of images
			}
		} else {
			o_Img.onload=function() {
				$.imgPreload_DoIt (sa_Images, idx, s_ImgFolder);
			}
		}
		o_Img.src=s_ImgFolder+s_Img;
	}
},


/**
 * imgChange
 *
 * @description Change Image Source - optionally using a 'transition effect'
 * @returns jQuery object - Chainable method
 */
jQuery.fn.imgChange = function (s_NewSrc, s_Filter, s_Speed) {
	//try {
		var img=this[0];
		if (!document.images || !img || img.tagName !="IMG") {return this;}

		var src=img.src;
		if (s_NewSrc.indexOf ("/")<0 && src.indexOf ("/")>=0) {
			// assume the images are in the same folder
			s_NewSrc=src.substr (0, src.lastIndexOf ("/")+1)+s_NewSrc;
		}

		if (s_NewSrc==src) {return this;} // ALREADY THIS IMAGE!

		if (s_Filter && img.style && img.filters) { // IE ONLY
			var x="blendTrans(duration="+s+")"; // default transition effect
			var t, s=(s_Speed >0) ? s_Speed : 1;
			s_Filter=$.trim (s_Filter.toLowerCase());
			while (s_Filter.indexOf ("_")>=0) {
				s_Filter=s_Filter.substr (0,s_Filter.indexOf ("_"))+s_Filter.substr (s_Filter.indexOf ("_")+1);
			}
			// check for short aliases
			switch (s_Filter) {
				case "wipe": s_Filter="WipeRight"; break;
				case "checkerboard": s_Filter="HorzCheckers"; break;
				case "circle": s_Filter="CircleOut"; break;
			}
			// START with 'blendTrans' filter
			switch (s_Filter) {
				case "fade": t="blendTrans"; break;
			}
			if (t) {x=t+"(duration="+s+")";}
			else { // now look for 'revealTrans' filters
				if (s_Filter>=0) {t=s_Filter;} // passed a 'transition number'
				else { // check for a 'keyword'
					switch (s_Filter) {
						case "boxin": t=0; break;
						case "boxout": t=1; break;
						case "circlein": t=2; break;
						case "circleout": t=3; break;
						case "wipeup": t=4; break;
						case "wipedown": t=5; break;
						case "wiperight": t=6; break;
						case "wipeleft": t=7; break;
						case "horzcheckers": t=10; break;
						case "vertcheckers": t=11; break;
						case "dissolve": t=12; break;
						case "barndoor": t=14; break;
						case "strips": t=20; break;
					}
				}
				if (t>=0) {x="revealTrans(transition="+t+",duration="+s+")";}
				else { // now look for 'transform' filters
					// REFERENCE: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/filter/reference/reference.asp
					// SAMPLES: http://msdn.microsoft.com/workshop/samples/author/dhtml/DXTidemo/DXTidemo.htm
					switch (s_Filter) {
						case "pixelate": t="Pixelate(duration="+s+")"; break;
						case "gradientwipe": t="gradientWipe(GradientSize=0.25,wipestyle=0,motion=forward,duration="+s+")"; break;
						case "radialwipe": t="RadialWipe(duration="+s+")"; break;
						case "spiral": t="Spiral(GridSizeX=205,GridSizeY=205,duration="+s+")"; break;
						case "wheel": t="Wheel(Spokes=10,duration="+s+")"; break;
						case "iris": t="Iris(IrisStyle=circle,motion=out,duration="+s+")"; break;
					}
					if (t) {x="progid:DXImageTransform.Microsoft."+t;}
				}
			}
			
			if (x) {
				img.style.filter=x;
				if (img.filters && img.filters[0]) { // make sure it took!
					img.filters[0].Apply();
					img.src=s_NewSrc;
					img.filters[0].Play();
					return this;
				}
			}

		} // END: if (s_Filter.length && img.style && img.style.filters)

		// *NON-IE* SWAP-IMAGE - FILTERS *NOT* AVAILABLE
		if (s_Filter && s_Speed >0.25) { // skip simulated effect IF is supposed to be "very fast"
			if (s_Filter=="wipeup" || s_Filter=="wipedown") {
				// use simulated 'wipe effect' (slide up/down)
				this.slideUp ("fast", function () {
					$(this).src (s_NewSrc).slideDown ("fast");
					
				});
			}
			else { 
				this.fadeTo ("fast", 0, function () {
					// use simulated 'dissolve effect' (fade out/in) for everything else
					$(this).src (s_NewSrc).fadeTo ("normal", 1);
				});
			}
		}
		else {
			// just change the img!
			this.src (s_NewSrc);
		}
/*
	}
	catch (e) {}
	finally {return this;}
*/
}


/*

jQuery.fn.slideLeftOpen = function (speed,callback) {
	return this.animate ({width: 'show'}, speed, callback);
	//this.animate ({width: 100%}, speed, callback);
	//return this.animate({ width: 'show' }, "slow", callback);
	// this.animate({ height: 'toggle', opacity: 'toggle' }, "slow");
},

jQuery.fn.slideLeftClose = function (speed,callback) {
	return this.animate ({width: 0}, speed, callback);
	//return this.animate ({width: 'hide'}, speed, callback);
},

jQuery.fn.slideLeftToggle = function (speed,callback) {
	return this.each(function(){
		var state = jQuery (this).is (":hidden") ? "show" : "hide";
		jQuery (this).animate ({width: state}, speed, callback);
	});
},


jQuery.fn.slideRightOpen = function (speed,callback) {
	return this.animate ({width: "show"}, speed, callback);
},

jQuery.fn.slideRightClose = function (speed,callback) {
	return this.animate ({width: "hide"}, speed, callback);
},

jQuery.fn.slideRightToggle = function (speed,callback) {
	return this.each (function(){
		var state = jQuery (this).is (":hidden") ? "show" : "hide";
		jQuery (this).animate ({width: state}, speed, callback);
	});
},

*/

