/// photo_rotator.js
///  Handles the photo rotator operations on the
///  community home page.

/// joe was here
/// you can tell because the hilarious comments will not be present in this block of code
/// endjoe

function playVideo(video_url, x)
{
	clearTimeout(PhotoRotator.timeout);
	
	if (video_url.substring(video_url.length-3).toLowerCase() == 'flv') {
		video_url = 'rtmp'+video_url.substring(4, video_url.length-4);
		var flash_div = document.createElement('div');
		flash_div.id = 'flv_embed';
		PhotoRotator.image_container.innerHTML ='';
		PhotoRotator.image_container.appendChild(flash_div);
		
		var flashvars = {};
		flashvars.width = '320';
		flashvars.height = '240';
		flashvars.source = video_url;
		var params = {};
		params.wmode = "transparent";
		params.allowscriptaccess = "always";
		var attributes = {};
		swfobject.embedSWF(fs_url+"flash/videoplayers/flvplayer.swf", "flv_embed", flashvars.width , flashvars.height, "9.0.0", fs_url+"scriptaculous/expressInstall.swf", flashvars, params, attributes);
	}
	else {
		PhotoRotator.image_container.innerHTML='<embed style="margin-bottom: 4px;" height="240" width="320" src="'+video_url+'"></embed>'+
			'<div class="image_info">' +
				'<span>Viewing ' +(PhotoRotator.current_photo+1)+ ' of ' + PhotoRotator.num_photos + '</span><br />' +
				photo_list[x].cutline +
			'</div>';
	}
}

///
/// endjoe
///

///
/// Photo class
///
function Photo (url, thumb_url, cutline, link) {
	if (!url)
		return false;
	
	this.url = url;
	this.thumb_url = thumb_url;
	this.cutline = (cutline == '') ? '&nbsp;' : cutline;
	this.link = (link == '') ? 'javascript:;' : link;
	
	// rudimentary preloading
	var big_preload = new Image();
	big_preload.src = this.url;
	var thumb_preload = new Image();
	thumb_preload.src = this.thumb_url;
}

///
/// Rotator Routines
///
var PhotoRotator = {
	container 		: null,
	image_container : null,
	thumb_container : null,
	timeout			: null,

	num_photos		: 0,
	current_photo	: -1,
	
	leftmost		: 0,

	rotate : function ()
	{
		if(PhotoRotator.current_photo==PhotoRotator.num_photos-1){
			PhotoRotator.current_photo=0;
		}
		else {
			PhotoRotator.current_photo+=1;
		}
		PhotoRotator.showImage(PhotoRotator.current_photo);
		PhotoRotator.timeout=setTimeout('PhotoRotator.rotate()',5000);
	},

	///
	/// Init function, sets up local vars and container
	/// 	objects.
	init : function (targ) {
		// Outer Container (contains big image, controls, and thumbnail list)
		PhotoRotator.container = document.getElementById(targ);
		if (!PhotoRotator.container)
			return false;
		
		// Inner Image & Thumb Containers
		var _temp = PhotoRotator.container.getElementsByTagName("div");
		for (var i=0; i<_temp.length; i++) {
			// Checking for Inner Image Container
			if (_temp[i].className == 'image_container')
				PhotoRotator.image_container = _temp[i];
			
			// Checking for Inner Thumbnail Container
			if (_temp[i].className == 'thumb_container')
				PhotoRotator.thumb_container = _temp[i];
		}
		if (!PhotoRotator.image_container || !PhotoRotator.thumb_container)
			return false;
		
		// Store number of photos locally.
		PhotoRotator.num_photos = photo_list.length;
		if (PhotoRotator.num_photos == 0)
			return false;
		
		// Give yourself a pat on the back, you've passed all my tests.
		var ret = PhotoRotator.showImage(0);
		return true;
	},
	
	///
	/// This function handles showing an image and
	///		it also calls the function used to update
	///		the thumbnail container.
	showImage : function (x) {
		// If for some reason the index arg is screwed up
		if ((x < 0) || isNaN(x))
			x = 0;
		if (x >= PhotoRotator.num_photos)
			x = PhotoRotator.num_photos - 1;
			
		// Return if we're trying to re-access the current photo
		//if (x == PhotoRotator.current_photo)
		//	return false;

		// Keep track of the current photo index
		PhotoRotator.current_photo = x;
		
		// Output the big image and its info into
		// the image container.
		// TODO: create parent elements and append to image container
		// 		 rather than outputting a huge string into innerHTML.
		var output = 	'<a href="'+photo_list[x].link+'"><img id="rotator_image" style="opacity:0;filter:alpha(opacity=0);" src="'+photo_list[x].url+'" /></a><br />' +
						'<div class="image_info">' +
							'<span>Viewing ' +(PhotoRotator.current_photo+1)+ ' of ' + PhotoRotator.num_photos + '</span><br />' +
							photo_list[x].cutline +
						'</div>';
						
		PhotoRotator.image_container.innerHTML = output;
		
		// Fade In Effect
		PhotoRotator.fadeImage();
		
		// Update the thumbnail list & Return true!
		PhotoRotator.updateThumbnails(PhotoRotator.current_photo);
		return true;
	},
	
	/// This was reduced to its own function in case
	/// of re-use.
	fadeImage : function () {
		Effect.Appear('rotator_image', {duration: 0.4, queue: 'end'});
	},
	
	///
	/// Updates the thumbnail container.
	///		(Shuffles thumbnails based on currently active one.)
	updateThumbnails : function (c) {
		//alert(c);
		//alert(PhotoRotator.thumbnail_inc);
		// Checking for how many thumbnails to show on either
		// side of the current thumbnail, if applicable.

		var start = 0;
		var end = 0;


		/*
		if (PhotoRotator.num_photos <= 7) {
			start = 0;
			end = PhotoRotator.num_photos;
		}
		else {
			if ((c + 4) > PhotoRotator.num_photos)
				end = PhotoRotator.num_photos;
			else
				end = c + 4;
				
			if ((c - 3) < 0)
				start = 0;
			else 
				start = c - 3;
	
			if (c < 4) {
				end += 3-c;
			}
			
			if (c > PhotoRotator.num_photos-4) {
				start -= 4-(PhotoRotator.num_photos-c);
			}
		}
		*/
		if ((c == PhotoRotator.leftmost) && (PhotoRotator.leftmost > 0)) {
			PhotoRotator.leftmost -= 1;
			start = PhotoRotator.leftmost;
			if (start + 5 < PhotoRotator.num_photos)
				end = start + 5;
			else
				end = PhotoRotator.num_photos;
		}
		else if ((c == PhotoRotator.leftmost+4) && (PhotoRotator.leftmost+5 < PhotoRotator.num_photos)) {
			PhotoRotator.leftmost += 1;
			start = PhotoRotator.leftmost;
			end = PhotoRotator.leftmost + 5;
		}
		else {
			start = PhotoRotator.leftmost;
			end = PhotoRotator.leftmost + 5;
		}
		
		if (end > PhotoRotator.num_photos)
			end = PhotoRotator.num_photos;
	

		

		// Output the thumbnail markup to the thumbnail container.
		// TODO: Contemplate: appending children in the for-loop rather
		//		 than outputting a huge string into innerHTML; meaning
		//		 of life; pancakes or sausage; etc.
		var output = 	'<center>' +
						'<img class="arrow_left" src="'+fs_url+'community/img/photo_rotator/prev_arrow.gif" width="13" height="25" onclick="PhotoRotator.showImage('+(PhotoRotator.current_photo-1)+'); clearTimeout(PhotoRotator.timeout);" />';
		for (i=start; i<end; i++) {
			if (i == PhotoRotator.current_photo) {
				var style = ' class="current"';
				var _onclick = '';
			}
			else {
				var style = '';
				var _onclick = 'onclick="PhotoRotator.showImage('+i+'); clearTimeout(PhotoRotator.timeout);" ';
			}
			// Create a thumbnail and put it into the container
			output += '<img'+style+' src="'+photo_list[i].thumb_url+'" '+_onclick+'/>';
		}
		output += 	'<img class="arrow_right" onclick="PhotoRotator.showImage('+(PhotoRotator.current_photo+1)+'); clearTimeout(PhotoRotator.timeout);" src="'+fs_url+'community/img/photo_rotator/next_arrow.gif" width="13" height="25" />' +
					'</center>';
		PhotoRotator.thumb_container.innerHTML = output;
	}
};