/*********************************
	custom jquery extensions
*********************************/

/*
	On Focus, clear text field of initial value
*/
(function($){
	$.fn.bindToEmpty = function(options) {
	
		var options = $.extend({}, options);
		
		$(this).each(function() {
			$(this).data('initValue', $(this).val());
		});
		
		$(this).focus(function() {
			if($(this).val() == $(this).data('initValue')) {$(this).val('');}
		}).blur(function() {
			if($(this).val() == '') {$(this).val($(this).data('initValue'));}
		});
		
	}//end $.fn
	
	$.fn.heros = function(options) {
	
		
		var options = $.extend({
			imagesSelect	:	'ul a',
			replaceSelect	:	'p img'
		}, options);
		
		if($(options.imagesSelect, $(this)).length == 0) {
			return; //Bail out if not hero images
		}
		
		$this = $(this);
		
		env = {
			images	:	$(options.imagesSelect, $this),
			replace	:	$(options.replaceSelect, $this)
		}
		
		//TO DO: preload?
		
		//Change image on click
		env.images.click(function() {
			env.replace.attr('src', $(this).attr('href'));
			return false;
		});
		
		//Load first image
		env.replace.attr('src', env.images.get(0).href);
	}
	
	$.fn.dropdown = function(options) {
		if($(this).length == 0) {
			return; //Bail out if no dropdowns
		}
		
		var options = $.extend({
			currentValSelect	:	'.contact_question_dropdown_label',
			onChange			:	function() {},
			init				:	function() {},
			trigger				:	true
		}, options);
		
		$this = $(this);
		
		//Handle onchange
		/*
		$('select', $this).change(function() {
			if($(this).val() != '') {
				var parts = $(this).val().split('|');
				var val = parts[0];
				if(parts[1]) {
					var href = parts[1];
				} else {
					var href = null;
				}
				$(this).siblings(options.currentValSelect).html(val);
				options.onChange(val, href, $(this));
			}
		}).trigger('change');//trigger onchange to handle selected="selected"
		*/
		var selects = $('select', $this);
		
		selects.change(function() {
			var parts = $(this).val().split('|');
			$(this).siblings(options.currentValSelect).html($(':selected', $(this)).text());
			options.onChange(parts, $(this));
		});
		selects.each(function() {
			$(this).siblings(options.currentValSelect).html($(':selected', $(this)).text());
		});
		
		options.init(selects);
	}


	$.googleDecode = function(options) {
		
		if(typeof GClientGeocoder == 'undefined')  {
			return false; //Don't load if google maps isn't present
		}
		
		var options = $.extend({
			zip		:	false,
			callBack:	function(pnt) {}
		}, options);
		
		if(!$.validZip(options.zip)) {
			return false; //invalid zip
		}
		
		var env = {
			geocoder:	new GClientGeocoder(),
			point	:	false
		}
		
		env.geocoder.getLatLng(options.zip, function(point) {
			if(!point) {
				return false; //Return false if no point found
			}
			env.point = point;
			options.callBack(point);
			//alert('lat: '+point.lat()+' long: '+point.lng());
		});
		
	}//end $.fn
	
	$.validZip = function(zip) {
		var valid = "0123456789";
		if(zip.length != 5) {
			return false;
		}
		for (var i=0; i < zip.length; i++) {
			var temp = "" + zip.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				return false;
			}
		}
		return true;
	}
	
	$.fn.players = function(options) {
		var options = $.extend({}, options);
		
		var $this = $(this);
		
		var env = {
			pager		:	$('#slide_controller', $this),
			backLink	:	$('#backarrow a', $this),
			nextLink	:	$('#nextarrow a', $this),
			previousURL	:	$('#backarrow a', $this).attr('href'),
			nextURL		:	$('#nextarrow a',  $this).attr('href'),
			linksEnabled:	true	//Don't spam my ajax
		};
		
		//Run once
		env.backLink.bind('click', hitBack);
		env.nextLink.bind('click', hitNext);
		
		
		function resetEnv() {		
			env.pager = $('#slide_controller', $this);
			env.backLink = $('#backarrow a', $this);
			env.nextLink = $('#nextarrow a', $this);
			env.previousURL = $('#backarrow a', $this).attr('href');
			env.nextURL = $('#nextarrow a', $this).attr('href');
			env.linksEnabled = true;
			
			env.backLink.bind('click', hitBack);
			env.nextLink.bind('click', hitNext);
		}
		
		function hitBack(e) {
			if(env.linksEnabled === true) {
				env.linksEnabled = false;
				$this.load(env.previousURL, function(responseText, textStatus, XMLHttpRequest) {
					Cufon.refresh('.gotlight, .gotlight *, .wysiwyg h3');
					resetEnv();
				});
			}
			return false;
		};

		function hitNext(e) {
			if(env.linksEnabled === true) {
				env.linksEnabled = false;
				$this.load(env.nextURL, function(responseText, textStatus, XMLHttpRequest) {
					Cufon.refresh('.gotlight, .gotlight *, .wysiwyg h3');
					resetEnv();
				});
			}
			return false;
		};
		
	}//end $.fn
	
	$.fn.shuffle = function() {
		
        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = $.map(allElems, function(){
                var random = getRandom(allElems.length),
                    randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });
 
        this.each(function(i){
            $(this).replaceWith($(shuffled[i]));
        });
 
        return $(shuffled);
 
    };//end $.fn
})(jQuery);