/*
	
	City of Cambridge Global Javascript Functions
	ISITE Design

*/

// jquery no conflict. use $j or jQuery outside of ready function
var $j = jQuery.noConflict(); 

// jQuery document ready

$j(document).ready(function( ) {
							
	//select box links
    $j(".jumpmenu").change(function(e) { window.location.href = $j(this).val(); });							

	//faqs 
	$j('.answer').hide();
	$j('.faq h2').toggle(
	function() {
	   	$j(this).next('.answer').fadeIn();
		$j(this).addClass('close');
	},
	function() {
		  $j(this).next('.answer').fadeOut();
			$j(this).removeClass('close');
	 }
	); // end faq
	
	//lists - add .first & .last to all lists
    $j("table thead tr").find("th:first").addClass('first');
	$j("table thead tr").find("th:last").addClass('last');
	$j("div.col ul").find("li:first").addClass('first');

	$j("a[rel=external]").click(function() { window.open(this.href); return false; });
});


jQuery(function($) {
	
	//add class "even" to every other product list item for proper wrapping
	$j('table.sortable tbody tr:even').addClass('even');
	// Pull the label, make lowercase, set as default value and hide it
	$j("#search-form input").inputSetter(1);
	
	// Add an event handler for the search box's blur event to add the Google watermark if there's no text in it
	$j("#search-form input").blur(function(){if($('#search-form input').val() == '') $('#search-form input').addClass('google-watermark');});
	
	// Add an event handler for the search box's focus event to remove the Google watermark and select any text in it
    $j("#search-form input").focus(function(){$('#search-form input').removeClass('google-watermark'); this.select();});
	
	// Reset all form elements to default, clear the fields 
	$j("#clear").click(function(){
		$j("form").each(function() {
			jQuery('input[type=text],input[type=password],textarea',this).val('');
			jQuery("select", this).each(function() { this.selectedIndex = 0; });
		});
		return false;
	});

	// setup ul.tabs to work as tabs for each div directly under div.panes
	$j("ul.tabs").tabs("div.tabs-container > div");
	
	//lists - add .first & .last to all lists
    //$("ul, ol").find("li:last").addClass('last');
	
	// JS enabled
	$j('html').addClass('js');
	
	// ie6 special cases
	if($.browser.msie && parseInt($.browser.version) < 7){
		
		// add class to drop downs and buttons
	    $j("#nav li, button, .image-btn a").hover(
			function() { var c = $(this).attr("class").split(" ")[1] || ""; $j(this).addClass("over over-"+c); },
			function() { var c = $(this).attr("class").split(" ")[1] || ""; $j(this).removeClass("over over-"+c); }
	    );		
		
		// fix elements over selects z-index issue
		// load script without ajax to avoid ActiveX requirement
		function loadScript(src, callback) {
			var script = document.createElement("script");		
			if(script.attachEvent) {
				script.attachEvent("onreadystatechange",
				function() { loadScript.callbackIE(callback); });
			}
			script.src = src;
			document.getElementsByTagName("head")[0].appendChild(script);
		}
		loadScript.callbackIE = function(callback) {
			var target = window.event.srcElement;
			if(target.readyState == "loaded")
			callback.call(target);
		};		
		callback = function() { $("#nav ul").bgiframe(); };
		
		loadScript("resources/js/jquery.bgiframe.min.js", callback);
		
	}// if ie6
	
	// show drops on focus
	$j("#nav a").focus(function(){ $j(this).parents("li").addClass("over"); })
				.blur(function(){ $j(this).parents("li").removeClass("over"); });
	$j("#nav>li").mouseover(function() { $j("#nav>li").not($j(this)).removeClass("over"); });

	$j('#flickr a[href^="http://"]')
  		.attr("target", "_blank");

});// document ready

// pull label and insert as field. clear with click
// optional lower param if == 1, string toLowerCase
jQuery.fn.inputSetter = function(lower) {	
	return this.each(function() {
		var $input = jQuery(this);
		var $label = jQuery("label[for='"+$input.attr("id")+"']");
		var labeltext = lower && lower==1 ? $label.text().toLowerCase() : 
		$label.text();
		$label.hide();
		if (jQuery.trim(labeltext).length) {
			$input.val(labeltext);		
			$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
				  .blur(function() { if (!this.value.length) { this.value = labeltext; } });		
		}
	});
};
