//=====================================//
// Earth Hour 2010 signup & takeaction //
//=====================================//
// Feb 10, 2010

var localPath = "";
var earthHourId = 0;
if (typeof(debug)=='undefined') var debug = 0;

$('#content-bg .header').css("background-image","none");

/* ----- Count down clock ----- */

var eh_runClock_CountStepper = -1;
var eh_runClock_LeadingZero = true;
var eh_runClock_TargetDate = "03/26/2011 8:30 PM";
var eh_runClock_DisplayFormat = "%%D%%:%%H%%:%%M%%:%%S%%";
var eh_runClock_Sec = 0;
var eh_runClock_Interval = 0;

function eh_runClock(act) {
	if (act==0) {
		var dnow = new Date();
		var dthen = new Date(eh_runClock_TargetDate);
		var tz = dthen.getTimezoneOffset() - dnow.getTimezoneOffset();
		var ddiff = new Date(dthen-dnow);
		ddiff.setMinutes(ddiff.getMinutes()-tz);
		eh_runClock_Sec = Math.floor(ddiff.valueOf()/1000);
		eh_runClock_Interval = setInterval("eh_runClock(1)", 1000);
	} else {
		if (eh_runClock_Sec < -3600) {
			html = "";
			clearInterval(eh_runClock_Interval);
		} else if (eh_runClock_Sec < 1) {
			html = "Earth Hour Is Now";
		} else {
			eh_runClock_Sec += eh_runClock_CountStepper;
			html = eh_runClock_DisplayFormat.replace(/%%D%%/g, calcage(eh_runClock_Sec,86400,100000));
			html = html.replace(/%%H%%/g, calcage(eh_runClock_Sec,3600,24));
			html = html.replace(/%%M%%/g, calcage(eh_runClock_Sec,60,60));
			html = html.replace(/%%S%%/g, calcage(eh_runClock_Sec,1,60));
		}
		$('#eh_bannertime').html(html);
	}
}
function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (eh_runClock_LeadingZero && s.length < 2)
    s = "0" + s;
  return "<b>" + s + "</b>";
}

/* ----- News ----- */

var eh_runNews_current = -1;
var eh_runNews_interval = 0;
var eh_runNews_active = false;
var eh_runNews_liheight = 0;

function eh_runNews(act) {

	// stop ticker on IE 7
	if ($.browser.msie) {
		if (parseInt($.browser.version.substr(0,3))<=7) {
			$('#eh_newsflip ul').css('position','static');
			act = 9;
		}
	}

	if (act == 0) {
		if ($('#eh_newsflip').length>0) {
			$('#eh_newsflip').append($('#eh_newsflip').html());
			eh_runNews_liheight = $('#eh_newsflip ul li').height();
			setTimeout("eh_runNews(1)", 1000); // time to first roll
			$('#eh_newsflip').mouseover(function(){
				clearInterval(eh_runNews_interval);
			});
			$('#eh_newsflip').mouseout(function(){
				eh_runNews_interval = setInterval("eh_runNews(2)", 5000); // roll interval
			});
		} else {
			$('#eh_bannercontent .row2').hide();
		}
	}
	if (act == 1) {
		clearInterval(eh_runNews_interval);
		eh_runNews_interval = setInterval("eh_runNews(2)", 5000); // roll interval
		eh_runNews(2);
	}
	if (act == 2) {
		if (eh_runNews_active == false) {
			eh_runNews_active = true;

			eh_runNews_current++;
			var c = $('#eh_newsflip ul').attr('rel');
			if (eh_runNews_current>c) {
				eh_runNews_current = 1;
				$('#eh_newsflip ul').css('top',0);
			}

			$('#eh_newsflip ul').animate(
				{
					top: -(eh_runNews_current*eh_runNews_liheight)
				}, 500, function() {
					eh_runNews_active = false;
			});
		}
	}

}

/* ----- Header Banner ----- */

function eh_drawHeader(swffile,swfdata) {

	//$('#content-bg .header').css("height", $('#eh_bannercontent').css('height'));
	$('#content-bg .header').css("height", "auto");
	$('#content-bg .header').html($('#eh_banner').html());
	$('#eh_banner').html('');

	var html = '';

	if (eh_hasFalsh(9,0,0)==true) { // if flash is available
		html  = '<object width="677" height="205">';
		html += '<param name="movie" value="'+swffile+'"></param>';
		html += '<param name="allowFullScreen" value="true"></param>';
		html += '<param name="allowscriptaccess" value="always"></param>';
		html += '<param name="wmode" value="transparent"></param>';
		html += '<param name="salign" value="tl"></param>';
		html += '<param name="flashvars" value="data='+swfdata+'"></param>';
		html += '<embed src="'+swffile+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" salign="tl"';
		html += ' flashvars="data='+swfdata+'"';
		html += ' width="677" height="205"></embed>';
		html += '</object>';
	} else {
		html += '<img src="'+localUrl+'img/banner/noflash.jpg" width=677 height=205 />';
	}

	$('#eh_banner_swf').html(html);

    eh_runClock(0,0);
}

function eh_hasFalsh() {
	// look for a flag in the query string to bypass flash detection
	if(/hasFlash=true/.test(location)) return true;
	if(/hasFlash=false/.test(location)) return false;

	var v = "";
	// ie
	try {
		try {
			// avoid fp6 minor version lookup issues
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try { axo.AllowScriptAccess = 'always'; } catch(e) { v = '6,0,0'; }
		} catch(e) {}
		if (v=="") {
			v = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
			// other browsers
		}
	} catch(e) {
		try {
			if (v=="") {
				if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
					v = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
				}
			}
		} catch(e) {}
	}
	if (v=="") v = '0,0,0';

//alert(v);
	var pv = v.match(/\d+/g);
//alert(pv);
	var rv = String([arguments[0], arguments[1], arguments[2]]).match(/\d+/g);
	for(var i = 0; i < 3; i++) {
		if (typeof(pv[i])!="undefined") {
			pv[i] = parseInt(pv[i] || 0);
			rv[i] = parseInt(rv[i] || 0);
			// player is less than required
			if(pv[i] < rv[i]) return false;
			// player is greater than required
			if(pv[i] > rv[i]) return true;
		}
	}
	// major version, minor version and revision match exactly
	return true;


};
