﻿
var insert = function(e, before) {
	if (before) {
		_insert(before, "insert-before-name", "insert-before-item");
	} else {
		_insert(before, "insert-after-name", "insert-after-item");
	}

	if (imageIndices.length > 6)
		carousel.scrollTo(imageIndices.length);
}

/**
 * Illustrates how to insert before or after
 */
var _insert = function() {

	var newName = name ? name : 'unnamed';

	if (newName === "") {
		//YAHOO.util.Dom.get("status").innerHTML = 
		$("status").innerHTML = 
				'<span style="color:red;">Please enter a name<\/span>'
		return;
	}

	var refIdx    = parseInt(imageList.length, 10);
	var newImgUrl = thumb;

	carousel.insertAfter(refIdx, fmtItem(newImgUrl, "#", newName));
	imageIndices.push(atid);

	$("status").innerHTML = '<span style="font-weight: bold; color: green">Item added</span>';
};

/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/

var fmtItem = function(imgUrl, url, title) {
	//url = "#ok";
  	var innerHTML = 
  		'<a href="' + 
  		url + 
  		'"><img src="' + 
  		imgUrl +
		'" width="' +
		75 +
		'" height="' +
		75+
		'"/>' + 
  		title + 
  		'<\/a>';
	return innerHTML;
};

/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 */
var loadInitialItems = function(type, args) {

	var start = args[0];
	var last = args[1]; 

	load(this, start, last);	
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 */
var loadNextItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];

	if(!alreadyCached) {
		load(this, start, last);
	}
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 */
var loadPrevItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 */
var handlePrevButtonState = function(type, args) {
	var enabling = args[0];
	var leftImage = args[1];
	if(enabling) {
		leftImage.src = "/images/carousel/left-enabled.gif";		
	} else {
		leftImage.src = "/images/carousel/left-disabled.gif";	
	}
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the next button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: nextButtonStateHandler
 */
var handleNextButtonState = function(type, args) {
	var enabling = args[0];
	var rightImage = args[1];
	if(enabling) {
		rightImage.src = "/images/carousel/right-enabled.gif";	
	} else {
		rightImage.src = "/images/carousel/right-disabled.gif";
	}
};

/**
 * To simulate out of synch loading, we load these items backwards.
 * This exercises the ability to load items out of sequence.
 */
var load = function(carousel, start, last) {
	for(var i=last; i>=start; i--) {
		if(!carousel.isItemLoaded(i)) {
			var url = "/p/" + pid + "/" + slideIndices[i-1];
			carousel.addItem(i, 
						fmtItem(imageList[i-1], url,
						prefix + imageNames[i-1]));
		}
	}
};

var carousel; // for ease of debugging; globals generally not a good idea
var prefix = "";
var pageLoad = function() 
{
	var size = imageIndices.length;// > 6 ? 6 : imageIndices.length;
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:      9,
			animationSpeed:  0.4,
			animationMethod: YAHOO.util.Easing.easeBoth,
			scrollInc:       6,
			navMargin:       50,
			size:            size,
			firstVisible:	 sid && size > 5 ? slideIndices.indexOf(parseInt(sid, 10)) : 1,
			loadInitHandler: loadInitialItems,
			prevElement:     "prev-arrow",
			nextElement:     "next-arrow",
			loadNextHandler: loadNextItems,
			loadPrevHandler: loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);
	//carousel.hide();
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);


/*
 * initialize the slider, add onclick event and adds the item into our presentation
*/
var setSlider = function (el) {
	if ($(el) == null)
		return;

	var mySlide = new Fx.Slide(el);
	/*
	if (imageIndices.length)
		mySlide.hide();
	*/
	$('hide').addEvent('click', function(e){
		e = new Event(e);
		$('status').innerHTML = '';
		mySlide.hide();
		e.stop();
	});
	
	$('add').addEvent('click', function(e) {

		if (imageIndices.indexOf(atid) != -1) {
			//alert('This item already exists in your presentation!');
			$("status").innerHTML = '<span style="color: red">This item already exists in your presentation!</span>';
			mySlide.show();
			return;
		}
		e = new Event(e);
		
		var myXhr = new XHR({method: 'post', onSuccess: function() {
					var r = myXhr.response;
					if (r && r.text) {
						var status = Json.evaluate(r.text);
						if ( "ok" == status.status) {
							if ( imageIndices.length > 0 ) {
								mySlide.slideIn();
								setTimeout("insert()", 500);
							} 
							else {
								insert();
							}
						}
						else if (status.status.match(/not auth/)) {
							$("status").innerHTML 
								= '<span style="color: red">You are not authenticated!</span>';
							return;
						}
					}
				} // end onSuccess
			});
		myXhr.send('/add_slide', "id=" + atid);

		//return;
		e.stop();
	});
}
