(function($){
	/**
	 *  Clear input fields on FOCUS event, if value equals defaultValue.
	 *  Return original value on BLUR event, if empty.
	 *  
	 *  @example $('input:text').clearInput();
	 */
	 
	$.fn.clearInput = function(){
		this.focus(function(){
			if(this.value == this.defaultValue) this.value = "";
		}).blur(function(){
			if(this.value == "") this.value = this.defaultValue;
		});
	};
	
	// ===== GLOBAL: Cached Version of $.getScript() =====
	$.getCachedScript = function(url, callback, cache){
		$.ajax({ type: "GET", url: url, success: callback, dataType: "script", cache: cache || true });
	};
	
	// USAGE: $(['img1.jpg','img2.jpg','img3.jpg']).preload();
	$.fn.preload = function() {
		this.each(function(){
			$('<img/>')[0].src = this;
		});
	};
	
})(this.jQuery);

jQuery.extend( jQuery.easing,{
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	}
});

var deviceAgent = navigator.userAgent.toLowerCase();
var isMobile = deviceAgent.match(/(iphone|ipod|ipad)/);
var isPhone = deviceAgent.match(/(iphone|ipod)/);


window.log = function(){
	log.history = log.history || [];   
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

(function(doc){
	var write = doc.write;
	doc.write = function(q){ 
		log('document.write(): ',arguments); 
		if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
	};
})(document);

// ~~~~~ BACKGROUND SLIDESHOW ~~~~~
var slideshowSpeed = 6000,
	activeContainer = 1,	
	currentImg = 0,
	animating = false,
	currentZindex = -1,
	interval;

var navigate = function(direction) {
	// Check if no animation is running. If it is, prevent the action
	if(animating) return;
	
	// Check which current image we need to show
	if(direction == "next") {
		currentImg++;
		if(currentImg == photos.length + 1) currentImg = 1;
	} else {
		currentImg--;
		if(currentImg == 0) currentImg = photos.length;
	};
	if($('#carousel_controls').length && $('#carousel_controls').is(':visible')){
		var active = $('#carousel_controls').find('li.active');
		var bkgd = $(active).find('a').css('background-position').split(' ');
		$(active).find('a').css('background-position','0 '+bkgd[1]);
		
		var next = $('#carousel_controls').find('li').eq(currentImg-1);
		var bkgd = $(next).find('a').css('background-position').split(' ');
		$(next).find('a').css('background-position','right '+bkgd[1]);
		
		$('#carousel_controls').find('li').removeClass('active');
		$('#carousel_controls').find('li').eq(currentImg-1).addClass('active');
	};
	
	// Check which container we need to use
	var currentContainer = activeContainer;
	activeContainer = (activeContainer == 1)? 2 : 1;
	showImage(photos[currentImg - 1], currentContainer, activeContainer);
	
};
var showImage = function(photoObject, currentContainer, activeContainer) {
	animating = true;
	
	
	
	currentZindex--;
	// Set the background image of the new active container
	$("#headerimg" + activeContainer).css({
		"background-image" : "url(" + photoObject.image + ")",
		"display" : "block",
		"z-index" : currentZindex
	});
	
	// Fade out the current container
	$("#headerimg" + currentContainer).fadeOut(function() {
		setTimeout(function() {
			$("#headertxt").css({"display" : "block"});
			animating = false;
		}, 500);
	})
	$("#carousel-btn").bind('click',function(){
		window.location.href = photoObject.url;
	}).css('cursor','pointer');
};
