jQuery.noConflict();

document.observe('dom:loaded', function() {
	
	startSlideShow(3000); //wait 3 seconds before beginning the slide show
	
	var splashImages = $$('div.splashButton');
	splashImages.each(function(name){
		name.observe('click', stopSlideshow.bindAsEventListener(this));
	});
	
load_youtube();
	//adding and removing default text from header search input
});

/*slide show of images is based off of id of slideshow div. format of id is splashImage#Slide */
/*assuming count starts at 1*/

var slideShowTimer; /*needed to find and stop timer*/

function startSlideShow(delay){
	if($('splashImage1Slide')!=null) //dpn't start unless at least first slide exists
	{
		slideShowTimer=setTimeout(nextSlide(1, delay), delay);
	}
}

function nextSlide(currentSlideNumber, delay){
	return (function(){
		Effect.Fade('splashImage'+currentSlideNumber+'Slide');
		Effect.Fade('splashImage'+currentSlideNumber+'Text');
		var nextNumber = currentSlideNumber+1;
				
		if($("splashImage"+nextNumber+'Slide')==null)
		{
			nextNumber=1;
		}

		setTimeout("Effect.Appear('splashImage"+nextNumber+"Slide')", 900); //how fast you want the image to appear
		setTimeout("Effect.Appear('splashImage"+nextNumber+"Text')", 900);
		slideShowTimer=setTimeout(nextSlide(nextNumber, delay), delay+2000); //how long the image should remain
		
	});
}

var previousSplash;
function stopSlideshow(mouseEvent){
	var currentElement = Event.element(mouseEvent);
	if(previousSplash != null && previousSplash.id != currentElement.id){
		previousSplash.setOpacity(currentElement.getOpacity());
		previousSplash.removeClassName("splashButtonHighlighted");
	}
	currentElement.setOpacity("0.99");
	currentElement.addClassName("splashButtonHighlighted");
	previousSplash = currentElement;
	
	clearTimeout(slideShowTimer);
	
	$$('div.splashImage').invoke('hide');
	$$('div.splashText').invoke('hide');
	
	Effect.Appear(currentElement.id+'Slide');//again basing id of slide on id of button.
	Effect.Appear(currentElement.id+'Text');//again basing id of slide on id of button.
}


/* Pulls up the latest two videos from youtube and put them in predefined divs */
var youtube_url = "http://gdata.youtube.com/feeds/users/AlbertaLiberalCaucus/uploads?orderby=published&alt=json&max-results=2&callback=?";

function load_youtube() {
	var data;
    jQuery.getJSON(youtube_url, function(data) {
        var x = 0;
        jQuery.each(data.feed.entry, function(i, item) {
            x++;
            var updated = item.updated;
            var url = item['media$group']['media$content'][0]['url'];
            //var thumb = item['media$group']['media$thumbnail'][0]['url'];
            // I'm not sure when this line got added, but it broke this function...
            //var numViews = item['yt$statistics']['viewCount'];
 
            var html = "<object width=\"280\" height=\"275\" class=\"video\">";
            html += "<param name=\"movie\" value=\"" + url + "\"></param>";
            html += "<param name=\"wmode\" value=\"opaque\"></param>";
            html += "<param name=\"allowFullScreen\" value=\"true\"></param>";
            html += "<param name=\"allowscriptaccess\" value=\"always\"></param>";
            html += "<embed src=\"" + url + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"280\" height=\"275\" wmode=\"opaque\"></embed></object>";
            jQuery("#youtube_latest"+x+"div").html(html);
        });
    });
};
