/*
 *
 * Siteworx jQuery Plugins Library
 *
 * Note: $swx() is used as the jQuery namespace to ensure there are no
 *  conflicts with other javascript libraries.
 *
 *  eg: var $swx = jQuery.noConflict();
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


// Prevent conflicts
var $swx = jQuery.noConflict();


/*
 *
 * Input Toggle Plugin
 *
 * This plugin handles text input blur/focus auto remove/addition
 *  of default text.
 *
 * On focus - default text is removed
 * On blur  - default text is added
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
$swx.fn.inputFocusToggle = 

	function(check_term) {
		//if ($swx(this).length) { // Make sure element exists
			$swx(this).each( function() {
			
				// Cache "this"
				var input_field = $swx(this);
				
				// Grab default text
				var default_text = (check_term)? check_term : input_field.val();
		
				// Check if default text exists (this is used to skip over input fields in forms)
				if (default_text != ""){
				
					// Removes text on focus
					input_field.focus( function() {
						if (input_field.val() == default_text){
							input_field.val("");
						}
					});
					
					// Inserts default text on blur
					input_field.blur( function() {
						if (input_field.val() == ""){
							input_field.val(default_text);
						}	
					});
				}
			});
		//}
	} // end jQuery.fn.inputToggle
	
	
	

/*
 *
 * Horizontal Slider Plugin
 *
 * This plugin creates a horizontal slider based on the following markup
 *
 *
 * <div id="slider">
 *		<button class="prev" />
 *		<div class="slide_wrap">
 *			<ul>
 *				<li> slide 1 content  </li>
 *				<li> slide 2 content  </li>
 *				<li> slide 3 content  </li>
 *				....
 *				<li> slide n content  </li>
 *			</ul>
 *		</div>
 *		<button class="next" />
 *	</div>
 *
 * 
 * 
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
$swx.fn.horizontalSlider = 

	function() {

			$swx(this).each( function() {
			
				var slider_parent  	= $swx(this);
				var slider      	= slider_parent.find("ul");

				setSlides(slider);
				
				$swx(".slides_wrap").cycle({ 
				    fx:     "scrollHorz", 
				    prev:   ".prev", 
				    next:   ".next", 
				    timeout: 0
				});
			});
		
	} // end jQuery.fn.horizontalSlider
	
/* 
	The purpose of this function is get over the hurdle of not having set sizes for each slide item (logo).
	When dealing with items in the slider that are different sizes, there was a noticeable overlap between 
	the overflow and the visible
	
	The goal is to ensure each transition shows the correct amount of items based on their width
	and the max width in the viewing area.
	 
	I break out all these items into newly created UL's that all have the same width, so when it comes
	time for the transition, I am dealing with a single UL instead of multiple LI's.

*/	
	
	function setSlides (slider) {
		var width = 0;
		var i_width = 0;
		var count = 0;
		var max_width 	= 760;
		var itemArr   	=[]; // temp array
		var items 		= slider.find("li");
		var item 		="";
		var wrapper		= jQuery("<div class='slides_wrap'></div>");

		// Go through the entire list of logo's
		for (var i=0;i<items.length;i++) {
			
			
			// Keep a running tally of the widths (adding each li's width)
			//  hold the li's in a temp array
			item 	= jQuery(items[i]);
			i_width = item.outerWidth();
			
			// If the running width is larger than the max width
			if( (width+i_width) > max_width ) {

				// Create a new UL and append all the items that were in the temp array (itemArr)
				var ul = jQuery("<ul class='item_slide'></ul>");
			
				for(var x=0;x<itemArr.length;x++){

					ul.append(itemArr[x]);
				}
				
				// Append the new UL and append it to the new wrapper, reset all counts
				wrapper.append(ul);
				itemArr = [];
				width = count = 0;

			}
			
			// Running width was not larger, add item to the array and add the width to the running total
			itemArr[count] = item;
			count++;
			width += i_width;
		
		}
		
		jQuery(".slide_wrap").find("ul").remove();
		jQuery(".slide_wrap").append(wrapper);
		jQuery(".slide_wrap .item_slide:eq(0)").addClass("active"); 
		
		
		
		// Check if there are items left in the itemArr (which is very likely)
		// If there get the remaining width and go back through the list and append
		// as many items as possible before running out of what width was left.
		// this ensures that there are no "empty" looking slides, with only one or two items that do not fill the space	

		if (itemArr.length >= 0) {
		
			var n_max_width = max_width - width;
			items = jQuery(".slide_wrap li");
			width = 0;
			
			var itemArrIdx = itemArr.length;
			
			for (var i=0; i<items.length;i++){
				
				item 	= jQuery(items[i]);
				i_width = item.outerWidth();
				
				if( (width +i_width) < n_max_width) {
					itemArr[itemArrIdx] = item.clone();
					itemArrIdx++;
					width += i_width;
				} else {
					break;
				}
				
			}
			

			var ul = jQuery("<ul class='item_slide'></ul>");
			
			for(var i=0;i<itemArr.length;i++) {
				ul.append(itemArr[i]);
			}
			
			wrapper.append(ul);
			
			jQuery(".slide_wrap .item_slide li:first-child").addClass("start");
		}
		

	}
	
/*
 *
 * Animate Background on Hover plugin
 *
 * This plugin animates the background color of the specified element
 *
 * hoverAnimateBackground ( "Mouse In Color", 
 *							"Mouse Out Color", 
 *							"Mouse In animation time (in milliseconds)" , 
 *							"Mouse Out animation time (in milliseconds)" <= opt);
 *
 * Defaults:
 *
 *	Mouse In Color  : Black 
 *	Mouse Out Color : Elements current background color ( failsafe is white) 
 *	Mouse In Time   : 500 (milliseconds) 
 *	Mouse Out Time  : Mouse In time 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
$swx.fn.hoverAnimateBackground = 

	function (obj) {
		
		$swx(this).each ( function() {
		
		var elem = $swx(this);
		
		// Setting defaults
		var in_c  = "#000";
		var out_c = "transparent";
		var in_t  = 500;
		var out_t = 500;
		
		// If object exists
		if (obj) {
		
			// Error checking
			in_c  = (obj.in_color  && !isDigit(obj.in_color) ) ? obj.in_color  : in_c;
			out_c = (obj.out_color && !isDigit(obj.out_color)) ? obj.out_color : out_c;	
			in_t  = (obj.in_time   && isDigit (obj.in_time)  ) ? obj.in_time   : in_t;	
			out_t = (obj.out_time  && isDigit (obj.out_time) ) ? obj.out_time  : out_t;	
		}
		
		// Attach hover events	
		elem.hover( 
			function() { // Mouse In
				$swx(this).animate({"backgroundColor" : in_c}, in_t);
			},
			function() { // Mouse Out
				if (out_c == "transparent") {
					$swx(this).css({ "background-color": "transparent" });
				} else {
					$swx(this).animate({"backgroundColor" : out_c}, out_t);
				}
			}
		);
		
	});
	}// end jQuery.fn.hoverAnimateBackground
	

/*
 *
 * Initiate Modal Background
 *
 * This plugin inintiates the modal background for modal dialogs
 *
 * The Modal container must be a child of the parent of the element
 *  clicked to initiate the dialog.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

$swx.fn.initModal =

	function() {
	
		$swx(this).click ( function() {
		
			// Find modal - fade in 
			var parent = $swx(this).parent();
			var modal  = parent.find(".modal");
			modal.fadeIn( "fast", function() {
				modal.find(".modal_wrap").fadeIn();
			});
			
			
			
			// Attach click event to close link
			modal.find(".close").click( function() {
				modal.fadeOut();
					
				return false; //prevent default action
			});
			
			
			return false; // prevent default action
		});
	
	}




/** Utility Functions for plugins **/

// Returns true if 'i' is a digit
function isDigit (i) {
	
	var re = /^[1-9][0-9]*$/;
	
	return re.test(i);
}
/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $swx(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $swx('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($swx(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($swx(this).attr("src")!="undefined") {
	  	
	  	var strHref = $swx(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($swx(this).attr("href")!="undefined") {
	  	
	  	var strHref = $swx(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});
	

