/**
 * Creates a new function that, when called, itself calls this function in
 * the context of the provided this value, with a given sequence of arguments
 * preceding any provided when the new function was called.
 *
 * developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
 */
if(!Function.prototype.bind) {
	Function.prototype.bind = function(oThis) {
		// closest thing possible to the ECMAScript 5 internal IsCallable function
		if(typeof this !== "function") {
			throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");
		}
		var aArgs = Array.prototype.slice.call(arguments, 1),
			fToBind = this,
			fNOP = function () {},
			fBound = function () {
				return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));
			};
		fNOP.prototype = this.prototype;
		fBound.prototype = new fNOP();
		return fBound;
	};
}

/**
 * Window logging function for pretty and safe logging calls
 *
 * usage: log('inside coolFunc', this, arguments);
 * paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
 */
window.log = function() {
	log.history = log.history || [];
	log.history.push(arguments);
	arguments.callee = arguments.callee.caller;
	if(this.console) {
		console.log(Array.prototype.slice.call(arguments));
	}
};

// Upgrade MSIE5.5-8 to be compatible with modern
// browsers. We're only going have this apply to
// MSIE7-8 though due to the IE6 chrome frame prompt
// http://code.google.com/p/ie7-js/
yepnope({
	load:'ielt9!http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js',
	callback:function(url,result,key){if(!window.IE7) yepnope(dir+'/js/ie9.js');}
});

// A fast & lightweight polyfill for min/max-width
// CSS3 Media Queries (for IE 6-8, and more)
// Visit j.mp/respondjs for project information
yepnope({
	load:dir+'/js/respond.js'
});

if(ga) {
	// More consistent way of loading the google analytics
	// tracking code but please be sure to change the above
	// @ga var to the correct site ID
	window._gaq=[['_setAccount',ga],['_trackPageview'],['_trackPageLoadTime']];
	yepnope({
		load:('https:'==location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js'
	});
}

// Prompt IE 6 users to install Chrome Frame.
yepnope({
	load:'ielt7!http://ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js',
	callback:function(url,result,key){CFInstall.check({mode:'overlay'});}
});


// Grab Google CDN's jQuery, with a protocol relative
// URL; fall back to local if necessary
// The plugin files will start downloading as soon as
// the line is executed, but if the jQuery CDN was down
// it won't be executed until after the local version
// was loaded.
yepnope([{
	load:'http://ajax.googleapis.com/ajax/libs/jquery/'+ (window.jqv ? window.jqv : '1.6.2') +'/jquery.min.js',
	callback:function(url,result,key){
		if(!window.jQuery){
			//if(console.log) console.log('jQuery CDN wasn\'t loaded successfully');
			yepnope({
				load: dir+'/js/jquery-1.6.2.js',
				callback:function(){
					if(log) log('jQuery library was loaded from local');
				}
			});
		} else {
			if(log) log('jQuery CDN was loaded');
		}
	}
}, {
	load:dir+'/js/plugins-jquery.js',
	complete: function() {
		if(log) log('Default jQuery plugins were successfully loaded... Begin loading user defined scripts');
	

/**
 * All of this project's meat scripts are loaded via the yepnope javascript
 * library. This script file contains a common procedure for loading all other
 * required plugins (including those specified within the main page template
 * and the common jQuery plugins used to make life easy.
 *
 * All code processes herein are licenced soley to Click Creative copyright
 * 2011 to author Matthew Kingston <matthewkingston@me.com>
 */

 /* ==|== primary scripts =====================================================
   Author:
   Date: 
   ========================================================================== */

/* <-- INSERT YOUR PROJECT SCRIPT PROCESSES BELOW --> */

$(function() {
	$('.selectAll').click(function(e) {
		e.preventDefault();
		var group = $(this).nextAll('.checkboxGroup').first();
		var checkboxes = group.find('input[type=checkbox]');
		
		if($(this).data('checked') == true) {
			checkboxes.each(function() { $(this).attr('checked', ''); });
			$(this).text('Select all');
			$(this).data('checked', false);
		} else {
			checkboxes.each(function() { $(this).attr('checked', 'checked'); });
			$(this).text('Deselect all');
			$(this).data('checked', true);
		}
	});
	
	$('.checkboxGroup input[type=checkbox]').change(function() {
		var group = $(this).parents('.checkboxGroup');
		var checkboxes = group.find('input[type=checkbox]');
		var selectAll = group.prevAll('.selectAll').first();
		var allChecked = true;
		checkboxes.each(function() { if(!$(this).attr('checked')) allChecked = false; });
		
		if(allChecked) {
			selectAll.text('Deselect all');
			selectAll.data('checked', true);
		} else {
			selectAll.text('Select all');
			selectAll.data('checked', false);
		}
	});
	
	
	$('.ph').each(function(index) {
		if($.browser.mobile) {
			var self = this;
			var repl = $("<a></a>");
			
			repl.html($(self).html());
			repl.attr('href', 'tel:' + $(self).data('phone'));
			
			$.each(this.attributes, function(index) {
				$(repl).attr(self.attributes[index].name, self.attributes[index].value);
			});
			
			$(self).after(repl).remove();
		}
	});
	
	$(['input.required', 'textarea.required'].join(',')).each(function() {
		$(this).keyup(function() {
			if($(this).val().length > 0) $(this).removeClass('required'); else $(this).addClass('required');
		});
	});
	
	$('.action').not('.nojs').each(function() {
		$(this).wrap('<span class="purple-button inline"></span>');
	});
	
	$('#ForgotPassword a').each(function() {
		$(this).addClass('purple-button');
		$(this).addClass('inline');
		$(this).wrapInner('<span></span>');
	});
});


yepnope({
	load: [
		dir+'/js/plugins/autocomplete/autocomplete.js',
		dir+'/js/plugins/autocomplete/autocomplete.css'
	],
	complete: function() {
		$(function() {
			var options, a;
			options = {
				serviceUrl:window.location.href+'ResidencyLookup'
			};
			a = $('input.autocompleter').autocomplete(options);
		});
	}
});


yepnope({
	load: dir+'/js/plugins/hintr.js',
	callback: function() {
		$('input[type="text"]').each(function(){
			if($(this).hasClass('showhint')) {
				$(this).attr('title', $(this).attr('value'));
				$(this).attr('value', '');
			}
			$(this).hintr();
		});
	}
});


yepnope({
	load: [
		dir+'/js/shadowbox/shadowbox.js',
		dir+'/js/shadowbox/css/shadowbox.css'
	],
	complete: function() {
		$(function() {
			Shadowbox.init({
				enableKeys: false,
				handleOversize: "none"
			});
		});
	}
});


yepnope({
	load: [
		dir+'/js/plugins/mappr.js',
		dir+'/js/plugins/infobubble.js'
	],
	complete: function() {
		$(function() {
			$('.mappr').mappr({});
		});
	}
});


yepnope([{
	load: dir+'/js/cufon/cufon-1.09i.js',
}, {
	load: dir+'/js/cufon/cufon-Arial.js',
	callback: function() {
		if(!$.browser.msie && !$.browser.mobile) {
			Cufon.replace('#header nav li, #tabbr nav li, h1, h2, h3, h4, h5, h6', { fontFamily: 'Arial', hover: true });
			Cufon.replace('#quickLinks li', { fontFamily: 'Arial', hover: true, textShadow: '1px 1px 0 #e1ed3c' });
			Cufon.replace('#feature h1, #feature p', { fontFamily: 'Arial', textShadow: '0 1px 1px rgba(0,0,0,.75)' });
			Cufon.now();
		}
	}
}]);


yepnope({
	load: 'ielt9!'+dir+'/js/dd_belatedpng-0.0.8a.js',
	callback: function() {
		$(function() {
			DD_belatedPNG.fix("img, .png");
		});
	}
});


yepnope({
	load: 'ielt9!'+dir+'/js/dd_roundies-0.0.2a.js',
	callback: function() {
		$(function() {
			DD_roundies.addRule('.roundify', '999px');
		});
	}
});


/** End user defined scripts **/
/* <!-- DO NOT CHANGE THE BELOW FUNCTION & OBJECT CLOSURES --> */

}
}]);

