/**
 * @package WordPress
 * @subpackage SolPowered
 * @since SolPowered 1.0
 */

/* besure to reference "jquery.js" prior to referencing this file (main.js) */

jQuery(document).ready(function($) {

	$("#access a[href=#]").click(function(e){
		return false;
	});

	$("#fontsize").html('<p>Type Size <a href="javascript:void(0);" tabindex="0" onclick="fontminus()" title="reduce font size">[-]</a> <a href="javascript:void(0);" tabindex="0" onclick="fontplus()" title="increase font size">[+]</a> <a href="javascript:void(0);" tabindex="0" onclick="fontreset()" title="reset font size">[reset]</a></p>');
	
	$(".js_hide").removeClass("js_hide");
	
	if(location.pathname.indexOf("environmental.html")==-1) {
		//$("div.collapser").hide();
		$(".expander").prepend('<img src="'+img_dir+'/expand.gif" class="expand" />')
	}	else {
		$("#open_popup strong").prepend('Open ');
	}
	
	
	// test for CSS
	var css = test_css();

	$("#access").removeClass('no_js');
	
	$("#access a + .sub-menu").prev("a").bind("mouseenter focus", function(e){
		$(this).next(".sub-menu").fadeIn("fast");
	});
	$("#access a + .sub-menu").parent("li").bind("mouseleave", function(e){
		$(this).children("ul").fadeOut("fast");
	});
	$("#access a + .sub-menu li:last-child a").bind("blur", function(e){
		$(this).parent("li").parent("ul").fadeOut("fast");
	});
	
	
	$("#content .collapser, #content .expander").css("cursor","pointer");


	// fp_boxes mouse over/out functions
	$("#fp_boxes .fp").hover(
		function () {
		// hover over
			// hide all
			$("#fp_boxes .fp").children(".hidden").fadeOut(200);
			// show current
			$(this).children(".hidden").fadeIn(200);
		}, 
		function () {
		// hover out
			// hide current
			$(this).children(".hidden").fadeOut(200);
		}
	).click(
		function () {
			window.location=$(this).children(".fp_text").children("a").attr("href");
		}
	);
	
	
	// fp_boxes blur/focus functions
	$("#fp_boxes .fp a").bind("focus", function(e){
		$("#fp_boxes .fp").children(".hidden").fadeOut(200);
		$(this).parent(".fp_text").next(".hidden").fadeIn(200);
	}).bind("blur", function(e){
		$(this).parent(".fp_text").next(".hidden").fadeOut(200);
	});


	if($("#env_resp").length) {
		var popup_before = '\r\n\
				<div id="popupContainer">\r\n\
					<div id="popup">\r\n\
';
		var popup_close = '<div id="popup_close"><a href="#chart"><img src="'+img_dir+'/close_pop_up.gif" alt="close" title="close" /></a></div>\r\n\
';
		var popup_after = '			</div>\r\n\
		</div>\r\n\
';
		var popup_text = $("#chart").next("div.popup").html();
		
		if(css){
			$("#chart").next("div.popup").html(popup_before + popup_close + popup_text + popup_after);
		} else {
			$("#chart").next("div.popup").html(popup_before + '<img src="'+img_dir+'/env_resp_pop_up.png" alt="chart of CO2 Emissions for Coal, Oil/Gas, Biomass, Nuclear, Wind, Solar, and Hydro" />' + popup_after);
		}
	}

	
	
	$(".expander").hover(
      function () {
        $(this).children(".expand").attr("src",img_dir+"/expand_o.gif");
				$(this).children(".collapse").attr("src",img_dir+"/collapse_o.gif");
      }, 
      function () {
        $(this).children(".expand").attr("src",img_dir+"/expand.gif");
				$(this).children(".collapse").attr("src",img_dir+"/collapse.gif");
      }
    ).click(function() {
		//$(this).toggleClass("open").next("div.collapser").slideFadeToggle("fast");
		$(this).toggleClass("open").next("div.collapser").animate({opacity: 'toggle', height: 'toggle'}, "fast");
		if($(this).hasClass("open")) {
			$(this).children(".expand").addClass("collapse").removeClass("expand");
			$(this).children(".collapse").attr("src",img_dir+"/collapse_o.gif");
		} else {
			$(this).children(".collapse").addClass("expand").removeClass("collapse");
			$(this).children(".expand").attr("src",img_dir+"/expand_o.gif");
		}
	});
	
	
	$(".collapser").click(function() {
		$(this).prev(".expander").click().children(".expand").attr("src",img_dir+"/expand.gif");
	});

	// force external links to open in a new window/tab
	$(".links a").click(function(e) {
		window.open(this.href);
		return false;
	});
		
	$("#open_popup").click(function(e) {
		// foward thinking popup

		$("#popupContainer").css({'margin-top': "-100px"}).fadeIn("normal");
		return false;
	});
	
	$("#forward").click(function(e) {
		$("#open_popup").click()
	});
	


	$("#forward_thinking").bind("mouseenter focus", function(e) {
		$(this).children("a").fadeIn(200);
	}).bind("mouseleave", function(e) {
		$(this).children("a").fadeOut(200);
	}).children("a").hide().children("img").attr("src",img_dir+"/foward_thinking_o.gif");

	$("#popup_close a").click(function(e) {
		$("#popupContainer").fadeOut("normal");
		return false;
	});
	
	
	
	if(location.pathname.indexOf("environmental.html")!=-1 && window.location.hash=="#chart") { setTimeout('$("#open_popup").click();', 250); } 
	
	

	// slideFadeToggle function
	(function(jQuery) {
		jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
			return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
		};
	})(jQuery);

});


function test_css() {
	// markup:			<div id="css_test"></div>
	// stylesheet:	#css_test { position:absolute; top:-1px; left:-1px; }
	
	if($("#css_test").length) {
	var css_test = $("#css_test");
	var css_test_pos = css_test.position();
	if(css_test_pos.left==-1 && css_test_pos.top==-1){ return 1; } else { return 0; }
	} else {
		return -1;
	}
}



