function pop_window(url, w, h) {
	myfocus = window.open(url, 'mywindow', 'menubar=no,scrollbars=no,width='+w+',height='+(h+30)); 
	setTimeout('myfocus.focus()',500);
}

function getObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId);
	} else {
		return false;
	}
}

function hide_it(id) {
	var myObj = getObject(id);
	if (myObj.style != false) myObj.style.display = 'none';
}

function show_it(id) {
	var myObj = getObject(id);
	if (myObj.style != false) myObj.style.display = 'block';
}

function populate_it(myField, myValue, myArrayName) {
	var selectedArray = eval(myArrayName + myValue);
	while (selectedArray.length < myField.options.length) {
		myField.options[(myField.options.length - 1)] = null;
	}
	for (var i=0; i < selectedArray.length; i++) {
		eval("myField.options[i]=" + "new Option" + selectedArray[i]);
	}
}

function select_it(myField, myValue) {
	var elmLength = myField.length;
	for (var i = 0; i < elmLength; i++) {
		if (myField.options[i].value == myValue) {
			myField.selectedIndex = i
		}
	}
}

function slide_show(imageName, imageArray, counter) {
	var slideShowSpeed = 6000; // slideShow speed
	var crossFadeDuration = 3; // duration of crossfade
	var t;
	imageArray = imageArray.toString();
	imageArray = imageArray.split(',');
	var arrLength = imageArray.length;
	//alert(counter);
	var preLoad = new Array();
	for (i = 0; i < arrLength; i++) {
		preLoad[i] = new Image();
		preLoad[i].src = imageArray[i];
	}
	$("#" + imageName).fadeOut(function () {
		$(this).attr('src', imageArray[counter]);
		$(this).fadeIn();
	});
	counter = counter + 1;
	if (counter >= (arrLength)) counter = 1;
	t = setTimeout('slide_show("'+imageName+'", "'+imageArray+'", '+counter+')', slideShowSpeed);
}









$(document).ready(function () {
	popup();
});
var popup = function () {
	var pop = $('#popup');
	var overlay = $('#pageOverlay');
	var html = '<div class="greenInitiativeWrapper secondary"><div class="greenInitiativeBox"><a href="" id="greenInitiative" alt="View our Green Initiative policy"><img src="/images/icons/green_2010.png" width="63" height="64" border="0" alt="View our Green Initiative policy" class="png" /></a></div></div>';
	if ($('.greenInitiativeWrapper').length == 0) $('body').append(html);
	var position = function () {
		var w = $('body').width();
		var h = $(document).height();
		var popW = pop.width();
		var popH = pop.height();
		pop.css({
			left: Math.floor($(window).scrollLeft() + ($(window).width() - popW) / 2) + 'px',
			top: Math.floor($(window).scrollTop() + ($(window).height() - popH) / 2) + 'px'
		});
		overlay.width(w);
		overlay.height(h);
	}
	var close = function () {
		overlay.fadeOut();
		pop.fadeOut(function () {
			pop.remove();
			overlay.remove();
		});
	}
	var open = function () {
		overlay.fadeIn();
		pop.fadeIn();
	}
	$('#greenInitiative').click(function (e) {
		e.preventDefault();
		var url = '/green_initiative.php';
		$.ajax({
			type: 'POST',
			url: url,
			data: 'json=1',
			error: function(xhr, status, error) {
				alert('Ajax error has occurred.');
			},
			success: function(data) {
				$('body').append(data);
				setupThings();
				position();
				open();
			}
		});
		open();
	});
	var setupThings = function () {
		pop = $('#popup');
		overlay = $('#pageOverlay');
		pop.find('.close').click(function (e) {
			e.preventDefault();
			close();
		});
	}
	$(window).bind('resize scroll', function () {
		position();
	});
};