// campaign class
var Campaign = function(){
	
	var LawPlayer = "http://media.lawschool.cornell.edu/flashcom/clswebvid/wwwhomepage/LawPlayer.swf";
	var width = "568";
	var height = "198";
	var layer = new Array();
	layer[0] = "home-video";
	layer[1] = "home-video-fader";
	var layerindex = 0;
	var mapID = "homemap";
	var swfID = "home-swf";
	var timer = 500;  // Used to "next"
	var flashTimer = 1500; //used to fade out flash slide
	var isFading = true;
	var _index;
	
	return {
		nextSlide: function(){
			// don't allow rotating when mid rotation
			if (isFading) return false;
			isFading = true;
			
			// build pointers
			var m = 0;
			var n = 1;
			var bottomlayer = $('#'+layer[m]);
			var toplayer = $('#'+layer[n]);
			
			_index = (_index + 1) % src.length;
			
			// build top layer
			var topimg = new Image();
			var toplayerimg = $(topimg);
			topimg.src = src[_index];
			toplayer.empty();
			toplayer.append(topimg);
			
			//fade in the top layer
			toplayer.show();
			toplayerimg.fadeOut(0);
			toplayerimg.fadeIn(timer);
			toplayer.fadeIn(timer, function(){
				// remove flash if it exists, copy the top to the bottom, then hide the top
				$('#'+swfID).remove();
				bottomlayer.html(toplayer.html());
				toplayer.fadeOut(0);
				toplayer.hide();
				
				//load the next image before enabling rotating again
				var _i = (_index+1) % src.length;
				var img = new Image();
				$(img).load( function(){ isFading = false; });
				img.src = src[_i];
			} );
			
			// make the text changes
			$("#campaign-caption").animate({opacity: 0}, timer/3, function(){
				$("#campaign-caption").html(cap[_index]);
				$("#campaign-caption").animate({opacity:1}, timer/3);
			});
			
			$("#campaign-caption-extra").animate({opacity: 0}, timer/3, function(){
				$("#campaign-caption-extra").html(cax[_index]);
				$("#campaign-caption-extra").animate({opacity:1}, timer/3);
			});
			
			$("#campaign-read-more").animate({opacity: 0}, timer/3, function(){
				// build "read more" link
				var txt = "";
				if (lnk[_index].length > 0){
					txt += '<a href="' + lnk[_index] + '" title="' + alt_text[_index] + '"';
					
					if (lnk_newwindow[_index] == 1)
						txt += ' target="_new"';
						
					if (lnk_text[_index] == '')
						txt += '>Read More</a></p>';
					else
						txt += '>' + lnk_text[_index] + '</a>';
					
					// build the "image map"
					var mt = $("#titlehomepage-top");
					var mb = $("#titlehomepage-bottom");
					
					mb.unbind('click');
					mt.unbind('click');
					$("#campaign-read-more").unbind('click');
					mt.show();
					mb.show();
					mt.attr("href", lnk[_index]);
					mb.attr("href", lnk[_index]);
					mt.attr("title", alt_text[_index]);
					mb.attr("title", alt_text[_index]);
					if (lnk_newwindow[_index] == 1) {
						mt.attr("target", "_new");
						mb.attr("target", "_new");
					} else {
						mt.attr("target", "");
						mb.attr("target", "");
					}
					
					$("#campaign-read-more").html(txt);
					$("#campaign-read-more").animate({opacity:1}, timer/3);
					linkSWF(_index);
				}
				else{
					// hide "image map"
					$("#titlehomepage-top").hide();
					$("#titlehomepage-bottom").hide();
					
					$("#campaign-read-more").html("");
					$("#campaign-read-more").animate({opacity:1}, timer/3);
				}
			});
			
			return false;
		},
		
		enable: function(first){ 
			
			_index=first;
					
			$('<div id="home-video-fader"><img src="' + src[_index] +'" /></div>').insertAfter("#home-video");
			$('#home-video-fader').fadeOut(0);
			$('#home-video-fader').hide(); 
			$('#home-video').css("zIndex",-1);
			$('#home-video-fader').css("zIndex",0);
		
			//*TVT suppress the revolver but load the current image in the base layer
			if (src.length > 1) {
				var obj = $('<div id="campaign-image-revolver"><p class="next-image"><a href="#" onclick="return Campaign.nextSlide();">next</a></p></div>');
				obj.animate({opacity:0}, 0);
				obj.insertAfter("#wrap_homemap");
				obj.animate({opacity:1}, timer/2);
			}
			
			// load the next image before displaying the rotator link
			var _i = (_index+1) % src.length;
			var img = new Image();
			$(img).load(function(){
				isFading=false;		 
			});
			img.src = decodeURI(src[_i]);
			
			return true;
		},
		
		playVideo: function(){
			if (isFading) return false;
			isFading = true;
			
			$('#'+layer[0]).html('<div style="background-color: #fff" id="' + swfID + '"></div>');
			$('#' + swfID).html('<object width="'+width+'" height="'+height+'"><param name="wmode" value="transparent"><param name="movie" value="'+LawPlayer+'?loadflv='+flv[_index]+'"><param name="flashvars" value="'+flv[_index]+'"><embed src="'+LawPlayer + "?loadflv=" + flv[_index]+'" width="'+width+'" height="'+height+'" wmode="transparent" flashvars="'+flv[_index]+'"></embed></object>');
			
			//fade out current slide
			var toplayer = $('#'+layer[1]);
			var topimg = new Image();
			var toplayerimg = $(topimg);
			topimg.src = src[_index];
			toplayer.empty();
			toplayer.append(topimg);
			toplayer.show();
			toplayer.fadeIn(0);
			toplayerimg.fadeIn(0);
			
			//use this method once we get the correct flvs
			toplayer.fadeOut(flashTimer);
			toplayerimg.fadeOut(flashTimer, function(){ isFading=false; toplayer.hide(); });
			
			$("#titlehomepage-top").hide();
			$("#titlehomepage-bottom").hide();
			
			return false;
		}
		
	};
	
}();

