/*
	
	Dreams Indeed Global Javascript Functions
	By Will Moore - ISITE Design

*/



// jQuery document ready
jQuery(function($) {

	// JS enabled
	$('html').addClass('js');
	
	// Few setups for IE6
	if(document.all){
		//add class to drop downs and buttons 
	    $('#nav li, button').hover(
			function() { $(this).addClass('over'); },
			function() { $(this).removeClass('over'); }
	    );
	}// if document.all

	// let's make tabs.
	$('.tabs').idTabs();
	
	// grab the label, put it in the input
	$('#header form input, #footer input[type=text], .cform input[type=text]').inputSetter();
	
	// add the class of last to the sub nav so we can remove the lower divider
	$('#nav-sub ul').find('li:last').addClass("last");
	
	/*

		// featured content slider on home page
		$('.nav-scrollerXXX a').click(function(){
			
			//send what was clicked and what it's href is. href could be pulled out in actual slider() function.
			var $this = $(this);
			var target = $this.attr("href");

			slider($this, target);		
			return false;	
		});*/
	
	
	var $shareThis = $('.share-this');
	$('h1').before($shareThis);
	
	// photogallery extending
	photoGallery();
	
	// fix for the IEs
	if ($('#footer #usermessage2a').text() == "") {$('#footer #usermessage2a').hide();};
	
	
});// document ready

//make sure the addthis widget doesn't break the page layout (http://www.addthis.com/forum/viewtopic.php?f=4&t=23037)
var addthis_config = {
  data_use_flash: false
}

// Extends the default wordpress photo gallery
//wordpress wants to link off to another page for each image. this grabs the href and shows it on the same page, ala jeremy keith.
function photoGallery(){
	if ($('div.gallery').length){
		var $gal = $('div.gallery');
		var $first = $gal.find('.gallery-item:eq(0)');
		var firstImage = $first.find('a').attr("href");
		var firstCaption = $first.find('.gallery-caption').text();
		//we need a default image
		$gal.prepend('<div id="gal-image-wrap"><img id="gal-image" src=" '+ firstImage + ' " /></div>');
		
		$gal.append('<p id="gal-caption">'+ firstCaption +'</p>');

		//each click replaces the source of the default image with the image clicked.
		$('.gallery-icon a').click(function(){
			var newImage = $(this).attr('href');
			var caption = $(this).parents('dl').find('.gallery-caption').text();
			
			$('#gal-image').attr('src', newImage);
			$('#gal-caption').text(caption);
			
			$gal.find('a.active').removeClass('active');
			$(this).addClass('active');
			
			return false;
		});
		
		// design calls for the h1 to be after the gallery... we're going to fake it because doing it via wp will be a fail.
		// since we know there's a gallery in here, let's move it.
		$('.gallery').prependTo('#lead');
		$('h1').insertAfter('#lead');	
		
	}
}


/*
	Simple HomePage slider. 
	Tied to preset sizes. Target is passed in on click. To extend, add 740px and add another condition for each..
*/
function slider($this, target) {


	if 		(target == "#scr-2") { scrollTo = "-1200px" ;}
	else if (target == "#scr-3") { scrollTo = "-2400px";}
	else if (target == "#scr-4") { scrollTo = "-3600px";}
	else if (target == "#scr-5") { scrollTo = "-4800px";}
 	else 	{scrollTo = "0";}
	
	// stuff to set the 'on' state of the link.
	$this.parent().parent().find('.active').removeClass('active');
	$this.parent('li').addClass('active');
	
	// make it so.
	$('.scrollers').animate({
		left: scrollTo
	});
	

	
}



// jQuery plugins

// attach to an input, grab its label, insert the label into the input, hide the label.  
jQuery.fn.inputSetter = function(lower) {	
	return this.each(function() {
		var $input = $(this);
		var $label = $("label[for='"+$input.attr("id")+"']");
		var labeltext = lower && lower==1 ? $label.text().toLowerCase() : $label.text();
		$label.hide();	
		$input.val(labeltext);		
		$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
			  .blur(function() { if (!this.value.length) { this.value = labeltext; } });		
	});
};

// generic tab builder
jQuery.fn.idTabs = function() {	
	return this.each(function() {
		var $container = jQuery(this), 
			tabs = $("a", this),
	 		panes = new Array();
		
		tabs.each(function () {
			var $this = jQuery(this);
						
			// using the href to make the collection of panes			
			var pane = $this.attr('href');
			if (pane.indexOf("#") != 0) {
				return true;
			};
		 	panes.push(pane);
		
			
			$this.bind("click", function(){
				//build the jq selector. cheap.
				$(panes.join(",")).hide();
				
				//do some class switching
				$container.find('.active').removeClass('active');
				$this.parent("li").addClass("active");

				$(pane).show();
				return false;
			});
		});
		
		var $default = jQuery('a.default',$container).length 
						? jQuery('a.default',$container) 
						: jQuery('a:first',$container);
	
		$default.click();
		
		
		
		
	});
};


// etc

// clean console.log
function cl(){ if(window.console&&window.console.firebug) { var args = [].splice.call(arguments,0); console.log(args.join(" ")); } }//cl()
// example: cl("If you use cl() instead of console.log(), it won't break IE when you forget to take it out.");





// IE6 fixes
// make sure IE has the abbr and acronym tag
if(document.all){
	document.createElement("abbr");
	document.createElement("acronym");
}
// prevent IE6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}
