/*
 - Description
	jQuery Dynamic Billboard Plugin
 - Author
	Trevor Hughes
 - Licence
	GNU Lesser General Public License
 - Concept from Yusuf Najmuddin (ynzi.com)
 - Usage
	$("#slideshow").dynamicSlideshow();

	OR

	$("#slideshow").dynamicSlideshow({duration: 5000});

 - Date 6/30/2009
 - Version 1.0

*/

/* `Dynamic Loading
----------------------------------------------------------------------------------------------------*/


var timer="";
var curr = 1;
var img = [];
var href = [];
var capt = [];
var alt = [];
var container = "";
var clickedVideo = "false";

if (typeof duration === 'undefined') { //check if duration was define outside this script -> if not, define it now
    var duration = 8000;
}

function showImage(container, num, img, href, capt, alt){
	var rw = $(".rotator").width();
	
	//we need to find out what type of media we are working with
	var fc = img[num].substr(0, 1);
	if(img[num].substr(0,1) == '#'){
		//rotating a div in
		var video = $(img[num]).html();
		$(container).append(video);
		$('.rotator .bb').wrapAll("<a href='" + href[num]  + "'></a>");
		
		$(".rotator .hidden").show();

		$(container).find('*:first').css({'z-index': 1});
		$(container).find('.billboardplayer:last').css({position:'absolute', opacity: 0.0, 'z-index': 2}).animate({opacity: 1.0}, 500, function() {
			if($(".rotator > *").size() > 1){
				$(container).find('*:first').remove();
			}
		});		
	}else{
		//rotating an image in
		var j = new Image();
		//var src = img[num] + "?" + new Date().getTime(); //add query string to stop cacheing
		var src = img[num];
		
		$(j).attr({src: src, alt: alt[num]}).css({position:'absolute', opacity: 0.0,'z-index':2}).addClass('fix').load(function(){
			
			$('#caption').html(capt[num]);
			$(container).append(this);
			if(href[num] != '' && href[num] != undefined){
				$(this).wrapAll("<a href='" + href[num]  + "'></a>");
			}
			
			if($(".rotator > *").size() != 1){
				$(container).find('*:first').css({'z-index': 1});
				
				$(this).animate({opacity: 1.0}, 500, function() {
					$(container).find('*:first').remove();
				});
			}else{
				$(this).animate({opacity: 1.0}, 1000);

			}
		});
	}	
}

function initSlider() {
	timer = setInterval( function(){
		if (curr == img.length) {
			curr = 0;
		}
		showImage(container, curr, img, href, capt, alt);
		
		$('.indicatorselected').toggleClass("indicatorselected");
		$(".indicator:eq(" + curr + ")").toggleClass("indicatorselected");
		
		curr++;
	}, duration );
};

$(document).ready(function() {
	$(".rotator").after("<div id='indicator_bar'></div>");
	$("#indicator_bar").append("<div id='indicators'></div>");
	$("#indicator_bar").append("<div id='caption'></div>");

	$(".rotator").each(function(){
		container = this;
		
		//go collect all the data
		var tmp_alt;
		$(this).find("a").each(function(){
			img.push($(this).attr("id"));		
			href.push($(this).attr("href"));
			capt.push($(this).attr("title"));
			
			tmp_alt = $(this).attr("name");
			if(typeof tmp_alt !== 'undefined'){
				alt.push(tmp_alt.replace(/_/g, " "));
			}
		});
		
		
		//put the indicators on the page
		var n = img.length;
		for(var i=0; i<n; i++){
			$("#indicators").append("<div class='indicator'></div>");
		}
		$('.indicator:first').addClass("indicatorselected");

		//put the first one one the page
		$(container).empty();
		var num = 0;
		//put a place holder on the page - IE fix
		showImage(container, num, img, href, capt, alt);
		//put the first image on the page
		showImage(container, num, img, href, capt, alt);

		
		$('.indicator').click(function(){
			clearInterval(timer);
			clickedVideo = "false";
			
			//find current element
			$('.indicator').each(function(index){
				if($(this).is(".indicatorselected")) {
					currentElement = parseInt(index);
				}
			});
		
			//move indicator
			$('.indicatorselected').removeClass("indicatorselected");
			$(this).addClass("indicatorselected");
			 
			//find selected element  					
			$('.indicator').each(function(index){
				if($(this).is(".indicatorselected")) {
					clickedElement = parseInt(index);
				}
			});
		
			//display selected element
			showImage(container, clickedElement, img, href, capt, alt);
						
			//add alt text of image to indicator bar
		 	$('#caption').html($('.billboard:eq(' + clickedElement + ')').attr("alt"));
		 	
		 	curr = clickedElement+1;

			initSlider();
		});
		
		//initSlider(container, img, href, capt);
		initSlider();
	});


	$(".rotator").hover(
		function(){
			clearInterval(timer);
		},
		function(){
			if(clickedVideo == "false"){
				initSlider();
			}
		}
	);
	
	
	$(".rotator .billboardplayer").live('click', function(event) {
		clearInterval(timer);
		/*
		var tpvalue = '/rotator/?mid=' + mid;
		_gaq.push(['_trackPageview', tpvalue]);
		
		tpvalue = 'http://multimedia.law.wfu.edu/?mid=' + mid;
		_gaq.push(['_trackPageview', tpvalue]);
		*/
		clickedVideo = 'true'; //set movie variable to true
	});
	
});
