/*

	PacifiCorp Global Javascript
	ISITE Design

*/

// prototype with jquery. $ = prototype, $j = jquery.
// can still use $ for jQuery within the jQuery document ready function
var $j = jQuery.noConflict();

// prevent ie6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

// sIFR 3
if(typeof sIFR != "undefined") {
	var gillsans = { src: '/etc/designs/pacificorp/flash/GillSans_sIFR3.swf' }; 
	sIFR.activate(gillsans);
	
	sIFR.replace(gillsans, {
		selector: '#lead h1',
		css: '.sIFR-root { background-color: transparent; color: #ffffff; }',
		wmode: 'transparent',
		repaintOnResize: true
	});
	
	sIFR.replace(gillsans, {
		selector: '.sifr',
		css: '.sIFR-root { background-color: transparent; color: #000000; }',
		wmode: 'transparent',
		repaintOnResize: true
	});
	
	// sIFR 3 with border
	var gillsansborder = { src: '/etc/designs/pacificorp/flash/GillSans_sIFR3_modified.swf' }; // adds borders around text
	sIFR.activate(gillsansborder);
	
	sIFR.replace(gillsansborder, {
		selector: '#primary h1',
		css: '.sIFR-root { background-color: transparent; color: #85855c; }',
		wmode: 'transparent',
		repaintOnResize: true
	});
}


// Dom loaded / jQuery
jQuery(function($) {

	// Pull the label, make lowercase, set as default value and hide it
	$("#sitesearch input").inputSetter(1);

	// ie6 special cases
	if($.browser.msie && parseInt($.browser.version) < 7){
		
		// add class to drop downs and buttons
	    $("#nav li, button, .image-btn a").hover(
			function() { var c = $(this).attr("class").split(" ")[1] || ""; $(this).addClass("over over-"+c); },
			function() { var c = $(this).attr("class").split(" ")[1] || ""; $(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("/etc/designs/pacificorp/js/jquery.bgiframe.min.js", callback);
		
	}// if ie6
	
	// show drops on focus
	$("#nav a").focus(function(){ $(this).parents("li").addClass("over"); })
				.blur(function(){ $(this).parents("li").removeClass("over"); });
	$("#nav>li").mouseover(function() { $("#nav>li").not($(this)).removeClass("over"); });
	
	// expand breadcrumb list
	$("#breadcrumb-expand a").click(function(){	
		$(this).parents("ul").find("li.hide").show().end().end().parent().remove();		
		return false;											 
	});
	
	// sitemape expand/collapse
	enableTree(".sitemap ul");
	
	// external window
	$("#header a.external, #primary a.external, #secondary a.external, #footer a.external").bind("click",openExternal);

	// if use has browser configured to not display colors or images, reset to fix collision with image replacement
	resetBackgrounds();
	
});// document ready


//if use has browser configured to not display colors or images, reset to fix collision with image replacement
//if use has browser configured to not display colors or images, reset to fix collision with image replacement
var resetBackgrounds = function() {
    if (!$j('#accessibility-reset-styles').length && // not already inserted
           ((!$j('body.popup').length && $j('body').css("background-image") == "none" ) || // popups don't have a bg image by default. this is always false on popups by body class check
            ($j('body.popup').length && $j('.btn').css("background-image") == "none" )) // if popup, should have a close button. check its background-image property
       )
    {
    	$j('head').append('<link id="accessibility-reset-styles" rel="stylesheet" type="text/css" media="all" href="/etc/designs/pacificorp/css/accessibility-reset.css" />');           
    }              
};

// create open/close tree. receives ul.
var enableTree = function(el) {
	$j(el).addClass("expander").find("li li ul").hide();
	
	$j("li li",el).each(function(){							 
		if($j("li",this).length>0) {
			$j(this).prepend('<a class="btn" href="#">+</a>')
			$j("a.btn",this).click(function(){										   
				var textinsert = $j(this).text() == "+" ? "-" : "+";
				$j(this).text(textinsert).toggleClass("open").parent("li").find("ul:first").slideToggle("fast");				
				return false;			
			});
		}	
	});	
};

// external link
// ref: http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
var openExternal = function(){
	var extwin = window.open($j(this).attr("href"), "_blank");
	if (extwin) {
		if (extwin.focus) extwin.focus();
		return false;
	}
	extwin = null;
	return false;
}

// 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();	
		$input.val(labeltext);		
		$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
			  .blur(function() { if (!this.value.length) { this.value = labeltext; } });		
	});
};