$(function() {
		setupGallery();
		findOpenRows();
		buzzSetup();
		littleWindowSetup();
		/*startAwards();*/
	});

var curBuzzH2;
var stillAnimating = false;

function littleWindowSetup(){
	$("ul.researchInfo li h3").each(function(){
		var isShown = 0;
		$(this).click(function(){
			if(isShown == 0){
				$(this).css({'background' : 'url("/discoverypark/images/icons/minus_small.gif") no-repeat'});
				$(this).next("div.moreInfo").slideDown(300);
				isShown = 1;
			}else{
				$(this).css({'background' : 'url("/discoverypark/images/icons/plus_small.gif") no-repeat'});
				$(this).next("div.moreInfo").slideUp(300);
				isShown = 0;
			}
		});
	});
}
/*function showInfo() {
	var display = $('div.moreInfo:first').css("display");
	if(display == 'none') {
		$("this div.moreInfo").show(1000);
}	*/

/* Sliders */
	function findOpenRows(){
		var temp;
		var openRows = new Array();
		openRows[0] = 1;
		$('.pageIndex').each(function() {
			temp = this.id;
			openRows = temp.split(",");
		});
		setupSliders(openRows);
	}
	function setupSliders(index) {
		var sliderNum=0;
		//show first sub item of each slider
		$('.slider').each(function() {
			$(this).children('ul').hide();
			if(index[sliderNum]>0){
				$(this).children('h3:eq('+(index[sliderNum]-1)+')').next('ul').show();
				$(this).children('h3:eq('+(index[sliderNum]-1)+')').addClass('open');
			}
			sliderNum++;	
		});
		//check for items with no sub items
		$(".slider h3").each(function() {
			if(!$(this).next().is("ul")){
				$(this).addClass('none'); //add class for no icon
			}		
		});
		//click function
		$(".slider h3").click(function(){
			var tabOpen = false;
			if($(this).hasClass("open")){
				tabOpen = true;
			}else{
				tabOpen = false;
			}
			$('.slider').each(function() {
				$(this).children('h3').removeClass("open");
				$(this).children('ul').slideUp("slow");
			});
			if(tabOpen==true){
				$(this).removeClass("open");
			}else{
				$(this).addClass("open");
				$(this).next("ul").slideToggle(300).siblings("ul").slideUp("slow");
			}
			$(this).siblings("h3").removeClass("open"); 
		});
	}
	
/* Featured Gallery */	
	function setupGallery() {
		$('#myGallery').show();
		$('#myGallery div').each(function() {
			title = $(this).children('h3').text();
			text = $(this).children('p').text();
			$(this).children('h3').remove();
			$(this).children('p').remove();
			if(title != "" && text != "") {
				$(this).append('<div class=\'slideInfoZone\'><h2>'+title+'</h2><p>'+text+'</p></div>');
			}
			//if only one feature, slide up its content, else show the slideshow controls
			if($(this).is(':only-child')){
				onAfter();
			}else{
				$(this).append('<a class=\'left\'/>');
				$(this).append('<a class=\'right\'/>');
				$(this).append('<a class=\'pause\'/>');
			}
		});
		$('#myGallery').cycle({ 
			fx:     'fade', 
			speed:   700, 
			timeout: 9000, 
			next: '.right',
			prev: '.left',
			before: onBefore,
			after: onAfter
		});
		//click events for pause/play button
		$('#myGallery a').click(function() { 
			if($(this).hasClass('pause')){										
				$('#myGallery .pause').addClass('play');			
				$('#myGallery .pause').removeClass('pause');
				$('#myGallery').cycle('pause'); 
			}else if($(this).hasClass('play')){		
				$('#myGallery .play').addClass('pause');			
				$('#myGallery .play').removeClass('play');
				$('#myGallery').cycle('resume'); 
			}
		});			
	}
	function onBefore(curr, next, opts) {
		$('.slideInfoZone').stop().animate({bottom:'-100px', opacity: 0.0}, 300);
	}
	function onAfter(curr, next, opts) {
		$('.slideInfoZone').stop().animate({bottom:'0px', opacity: 0.8}, 700);
	}

/* Buzz controls */
function buzzSetup() {
	if($('#buzz').length > 0) {
		container = document.getElementById("buzz"); 
		$("#buzz .buzzElement").each(function() {
				$(this).css({'display' : 'none'});
		});
		
		var nextButton = document.createElement('a');
		var prevButton = document.createElement('a');
		
		nextButton.onclick = function(){return buzzNext();};
		prevButton.onclick = function(){return buzzPrev();};
		
		nextButton.id = 'next';
		prevButton.id = 'prev';
		
		container.insertBefore(prevButton, container.firstChild);
		container.insertBefore(nextButton, container.firstChild);
		
		var firstElement = $('#buzz .buzzElement:first');
		firstElement.css({'display' : 'block'});
		firstElement.css({'marginLeft' : '0px'});
		$('#buzz .buzzElement:first .obHider').css({'visibility' : 'visible'});
		curBuzzH2 = $('#buzz .buzzElement:first h2').html();
	}
}

function buzzNext() {
	if(!stillAnimating){
		var tempBuzz;
		var tempOb;
		stillAnimating = true;
		
		$("#buzz .buzzElement").each(function() {
			if($(this).children('h2').html()==curBuzzH2){
				if(($(this).next().children('h2').html()==null)||($(this).next().children('h2').html()=="")){
					tempBuzz = $('#buzz .buzzElement:first h2').html();
					//IE Audio Fix
						$(this).children('.obHider').css({'visibility' : 'hidden'});
						tempOb = $(this).children('.obHider').html();
						$(this).children('.obHider').html('<div style="width:320px;height:265px"></div>');
					//
					$(this).children('.obHider').html(tempOb);
					$(this).animate({marginLeft: '-450px'}, 'slow', function() {
							$(this).css({'display' : 'none'});
						});
					$('#buzz .buzzElement:first').css({'display' : 'block', 'marginLeft' : '450px'});
					$('#buzz .buzzElement:first').animate({marginLeft: '0px'}, 'slow', function() {
							$(this).children('.obHider').css({'visibility' : 'visible'});
							stillAnimating = false;
					});
				}else{
					tempBuzz = $(this).next().children('h2').html();
					$(this).children('.obHider').css({'visibility' : 'hidden'});
					//IE audio fix
						tempOb = $(this).children('.obHider').html();
						$(this).children('.obHider').html('<div style="width:320px;height:265px"></div>');
						$(this).children('.obHider').html(tempOb);
					//
					$(this).animate({marginLeft: '-450px'}, 'slow', function() {
							$(this).css({'display' : 'none'});
						});
					$(this).next().css({'display' : 'block', 'marginLeft' : '450px'});
					$(this).next().animate({marginLeft: '0px'}, 'slow', function() {
							$(this).children('.obHider').css({'visibility' : 'visible'});
							stillAnimating = false;
					});
				}
			}
		});
		curBuzzH2 = tempBuzz;
	}
}
function buzzPrev() {
	if(!stillAnimating){
		var tempBuzz;
		var tempOb;
		stillAnimating = true;
		
		$("#buzz .buzzElement").each(function() {
			if($(this).children('h2').html()==curBuzzH2){
				if(($(this).prev().children('h2').html()==null)||($(this).prev().children('h2').html()=="")) {
					tempBuzz = $('#buzz .buzzElement:last h2').html();
					$(this).children('.obHider').css({'visibility' : 'hidden'});
					//IE Audio Fix
						tempOb = $(this).children('.obHider').html();
						$(this).children('.obHider').html('<div style="width:320px;height:265px"></div>');
						$(this).children('.obHider').html(tempOb);
					//
					$(this).animate({marginLeft: '450px'}, 'slow', function() {
							$(this).css({'display' : 'none'});
						});
					$('#buzz .buzzElement:last').css({'display' : 'block', 'marginLeft' : '-450px'});
					$('#buzz .buzzElement:last').animate({marginLeft: '0px'}, 'slow', function() {
							$(this).children('.obHider').css({'visibility' : 'visible'});
							stillAnimating = false;
					});
				} else {
					tempBuzz = $(this).prev().children('h2').html();
					$(this).children('.obHider').css({'visibility' : 'hidden'});
					//IE Audio Fix
						tempOb = $(this).children('.obHider').html();
						$(this).children('.obHider').html('<div style="width:320px;height:265px"></div>');
						$(this).children('.obHider').html(tempOb);
					//
					$(this).animate({marginLeft: '450px'}, 'slow', function() {
							$(this).css({'display' : 'none'});
					});
					$(this).prev().css({'display' : 'block', 'marginLeft' : '-450px'});
					$(this).prev().animate({marginLeft: '0px'}, 'slow', function() {
							$(this).children('.obHider').css({'visibility' : 'visible'});
							stillAnimating = false;
					});
					
				}
			}
		});
		curBuzzH2 = tempBuzz;
	}
}

/*  Awards Controls */

function startAwards(){
	$("#awardsContainer").each(function(){
		var i;
		var xmlDoc = loadXMLDoc("/dp2/main/includes/awardsXML.xml");
		var numAwards = 0;
		var atAwardsEnd = false;
		var tempAward;
		var randomNum;
		var tempArray = new Array();
		var awardBox = document.getElementById("awardsContainer");
		var tempStor;
		var awardOrder = new Array();
		var curAward = 0;
		var justStarting = true;
		var timer = 0;
		var dirValue = 1;
		var fxDone = true;
		
		function loadXMLDoc(docName){
			try{ // IE
				docLoader=new ActiveXObject("Microsoft.XMLDOM");
				docLoader.async="false";
				docLoader.load(docName);
				return docLoader;
			}catch(e){  // Everything else
				docLoader = new window.XMLHttpRequest();
				docLoader.open("GET",docName,false);
				docLoader.send("");
				return docLoader.responseXML;
			}
			alert("Error loading document");
			return null;
		}
		
		while(atAwardsEnd==false){
			try{
				tempAward = xmlDoc.getElementsByTagName("title")[numAwards].childNodes[0].nodeValue;
				numAwards++;
			}catch(e){
				atAwardsEnd = true;
			}
		}
		for(i=0; i<(numAwards); i++){
			var newDiv = document.createElement("div");
			$(newDiv).addClass("awardElement");
			$(newDiv).attr("id","aw"+i);
			
			var newTitle = document.createElement("span");
			$(newTitle).html(xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue);
			$(newTitle).addClass("awardElementSpan");
			newDiv.appendChild(newTitle);
	
			var newImg = document.createElement("img");
			newImg.setAttribute("src",xmlDoc.getElementsByTagName("image")[i].childNodes[0].nodeValue);
			$(newImg).addClass("awardElementImg");
			newDiv.appendChild(newImg);
			
			var newDesc = document.createElement("p");
			$(newDesc).html(xmlDoc.getElementsByTagName("description")[i].childNodes[0].nodeValue);
			newDiv.appendChild(newDesc);
			
			awardBox.appendChild(newDiv);
			
			tempArray[i] = i;
		}
		while(tempArray.length>0){
			randomNum=Math.floor(Math.random()*tempArray.length);
			awardOrder.push(tempArray[randomNum]);
			tempArray.splice(randomNum,1);
		}
		
		var nextButton = document.createElement('a');
		$(nextButton).addClass("awardElementNext");
		awardBox.appendChild(nextButton);
		var prevButton = document.createElement('a');
		$(prevButton).addClass("awardElementPrev");
		awardBox.appendChild(prevButton);
		
		nextButton.onclick = function(){return awardNext();};
		prevButton.onclick = function(){return awardPrev();};
		
		rotateAwards();
		
		function rotateAwards(direction){
			if(fxDone){
				fxDone=false;
				if(!justStarting){
					$("#aw"+awardOrder[curAward]).fadeOut(600, function() {
						curAward+=direction;
						if((curAward==numAwards)&&(direction>0)){
							curAward=0;
						}else if(curAward<0){
							curAward=(numAwards-1);
						}
						$("#aw"+awardOrder[curAward]).fadeIn(600, function() {
							timer=0;
							fxDone=true;
							timeCheck();
						});
					});
				}else{
					$("#aw"+awardOrder[curAward]).css({"display" : "block"});
					$("#aw"+awardOrder[curAward]).css({"visibilty" : "visible"});
					justStarting = false;
					timer=0;
					fxDone=true;
					timeCheck();
				}
			}
		}
		function timeCheck(){
			var z;
			if(fxDone==false){
				return 0;
			}
			if(timer>=5000){
				rotateAwards(dirValue);
				return 0;
			}
			timer+=1000;
			z = setTimeout(timeCheck, 1000);
		}
		function awardNext(){
			timer=6000;
			rotateAwards(1);
		}
		function awardPrev(){
			timer=6000;
			rotateAwards(-1);
		}
	});
}