// PRELOAD ROLLOVER IMAGES
// -----------------------
if (document.images) {
	img1 = new Image();
	img1.src = "images/home/welcome_button.jpg";
	img2 = new Image();
	img2.src = "images/home/welcome_button-on.jpg";
	img3 = new Image();
	img3.src = "images/home/ethos_button-on.jpg";
	img4 = new Image();
	img4.src = "images/home/search_button-over.gif";
	img5 = new Image();
	img5.src = "images/home/ethos_button-on.jpg";
	img6 = new Image();
	img6.src = "images/news/top_nav_back-over.gif";
	img7 = new Image();
	img7.src = "images/news/top_nav_next-over.gif";
	img8 = new Image();
	img8.src = "images/news/bottom_nav_back-over.gif";
	img9 = new Image();
	img9.src = "images/search_page/search_button-over.gif";
	img9 = new Image();
	img9.src = "images/home/parents_button-over.gif";
	img10 = new Image();
	img10.src = "images/video_button-over.gif";
	img11 = new Image();
	img11.src = "images/photos_button-over.gif";
	img11 = new Image();
	img11.src = "images/green_issues-over.gif";
}


// FADING PHOTOS
// -------------
function fading_photos(container_id, total_images, img_path) {
	var img_extension = ".jpg";
	var i = 1;
	
	// insert first image
	$('<img src="' + img_path + i + img_extension + '" class="pic' + i + '" />').appendTo(container_id).show();
	i++;
	
	// every 5 seconds - run the change_photo function
	setInterval(change_photo, 6000);

	function change_photo() {
		if(i > total_images) {
			i = 1;
			current_img = 1;
		}
		else current_img = i;
		
		if(current_img == 1) last_img = total_images;
		else last_img = (current_img - 1);
		
		// fade in the new image
		$('<img src="' + img_path + current_img + img_extension + '" class="pic' + current_img + '" />').appendTo(container_id).fadeIn(1500);
		// fade out + remove the existing image
		$(container_id + ' img.pic' + last_img).fadeOut(1500, function() {
			$(container_id + ' img.pic' + last_img).remove();
		});
		
		i++;
	}
}


// CHANGE SEARCH RESULTS PAGE
// --------------------------
function change_search_results(div_class) {
	
	// create array of <div> tags within #search_results	
	var mydivs = document.getElementById('search_results').getElementsByTagName('div');
	// foreach <div>
	for(var i = 0; i < mydivs.length; i++) {
		// if div_class provided and matches current <div> - make <div> visible
		if (div_class != null & mydivs[i].className == div_class) mydivs[i].style.display = 'block';
		else if (mydivs[i].className == 'nav') mydivs[i].style.display = 'block';
		else mydivs[i].style.display = 'none';		// else - hide it
	}
	
	window.scroll(0,0);
	
	return false;
}



$(document).ready(function() {


	

	
	// VIDEO PAGES
	// -----------
	
	// add 'play' buttons to the video thumbs
	$("<span></span>").appendTo("table.video_thumbs tr td a");
	
	// animate the 'play' buttons
	$("table.video_thumbs tr td a").hover(function() {
			$("span", this).stop().animate({"opacity": 1});
		}, function() {
			$("span", this).stop().animate({"opacity": .65});
		}
	);
	// -----------
	
	
	// VIDEO pop up VIEWER
	// --------------------------------
	$("a.video").click(function(e) {
		
		// get movie dimensions
		var mov_dimensions_arr = $(this).children("span").attr("class").split("x");
		var mov_height = parseInt(mov_dimensions_arr[1]);
		var mov_width = parseInt(mov_dimensions_arr[0]);
		
		$("#movie_viewer_background").css({"opacity" : "0.7"}).fadeIn("slow");
		$("#movie_viewer").css({
			top:'50%',
			left:'50%',
			height: + (mov_height+45) + 'px',
			width: + (mov_width+20) + 'px',
			margin:'-'+(mov_height / 2)+'px 0 0 -'+(mov_width / 2)+'px'
		});
		
		$("#movie_viewer").prepend("<embed src='"+ this.href +"' width='"+ mov_width +"' height='"+ mov_height +"' loop='false' pluginspage='http://www.apple.com/quicktime' autoplay='false' controller='true'></embed>").fadeIn("slow");
		
		return false;
	});
	
	
	// if click on background - close the overlay
	$("#movie_viewer_background").click(function() {
		close_movie_viewer();
	});
	
});



function close_movie_viewer() {
	$("#movie_viewer embed").remove();
	$("#movie_viewer").fadeOut("slow");
	$("#movie_viewer_background").fadeOut("slow");
}










function createWindow(page, w, h) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'width='+w+',height='+h+',top='+wint+',left='+winl+',scrollbars=no';
	newwindow = window.open(page,'name',winprops);
	return false;
}

function treeclick(a) {
    var curr_status = a.parentNode.className;
    
    // create array of <li> classes
	var mylis = document.getElementById('side_nav').getElementsByTagName('li');
	// hide all <li>s
	// for(var i = 0; i < mylis.length; i++) {
		// if(mylis[i].className = 'open') mylis[i].className = 'closed';
	// }
	
    if(curr_status == 'open') a.parentNode.className = 'closed';
    else a.parentNode.className = 'open';
    return false;
}
