/*!
 * slideViewer 1.2
 * Examples and documentation at: 
 * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/imagestrip/imageslide-plugin.html
 * 2007-2010 Gian Carlo Mingati
 * Version: 1.2.3 (9-JULY-2010)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Requires:
 * jQuery v1.4.1 or later, jquery.easing.1.2
 * 
 */

var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
	settings = jQuery.extend({
		easeFunc: "easeOutExpo",
		easeTime: 750,
		uiBefore: false,
		toolTip: false,
		ttOpacity: 0.9,
		paddingRight: 20,
		paddingLeft: 20
	}, settings);
	return this.each(function(){
		var container = jQuery(this);
		container.find("img.ldrgif").remove();
		$('#slideshow_loading_overlay').fadeOut('fast');
		container.removeClass("svw").addClass("stripViewer");		
		var pictWidth = container.find("img").width();
		var pictHeight = container.find("img").height();
		var pictEls = container.find("li").size();
		var stripViewerWidth = (pictWidth + settings.paddingRight + settings.paddingLeft)*pictEls;

		container.each(function(i) {
    		(!settings.uiBefore) ? jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>") : jQuery(this).before("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");			
		
			// if we have already created one slideshow, we need to now take into account all slides in that one (or ones) for our button ID counter
			var currentItemCounter = 1;
			$('.stripTransmitter').each(function(){
				listItems = $(this).children('ul').children('li');
				listCount = listItems.length;
				currentItemCounter += listCount;
			});
			
			jQuery(this).find("li").each(function(n) {
				var sectionNumber = $(this).attr('class'); // added by AR.  Each list item has a class of
				jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#' id='imgBtn_" + currentItemCounter + "' rel='.portfolio_link" + sectionNumber + "'>"+(n+1)+"<\/a><\/li>");												
				currentItemCounter++;
			});
			jQuery("div#stripTransmitter" + j + " a").each(function(z) {
				jQuery(this).bind("click", function(){
					// click functions for our hidden buttons, triggered by our right-hand nav
				
					thisElementID = $(this).attr("id");
					slideshowListItem = container.find('li[rel="#'+thisElementID+'"]');
				
					newItemCategory = slideshowListItem.find('input.itemCategory').attr('value');
					newItemDetail = slideshowListItem.find('input.itemDetail').attr('value');
					newItemDescription = slideshowListItem.find('input.itemDescription').attr('value');
					
					$('#item_info_box').fadeOut('fast', function() {
						$('#item_category').text(newItemCategory);
						$('#item_detail').text(newItemDetail);
						$('#item_description').text(newItemDescription);
						$('#item_info_box').fadeIn('fast', function() {
						});
					});
		
					jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
					var cnt = -((pictWidth+settings.paddingRight+settings.paddingLeft)*z);
					container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				
					var elementToHighlight = jQuery(this).attr('rel'); //added by AR
				
					$('.portfolio_link').each(function(){
						$(this).removeClass("current");
					});
				
					$('#portfolio_nav').find('ul.current_list').find(elementToHighlight).addClass("current");
				
					return false;
				});
			});
			
			
			container.bind("click", function(e){
				/*
				var ui = (!settings.uiBefore) ? jQuery(this).next().find("a.current") : jQuery(this).prev().find("a.current");
				var bTotal = parseFloat(jQuery(this).css('borderLeftWidth').replace("px", "")) +  parseFloat(jQuery(this).css('borderRightWidth').replace("px", ""));
				var dOs = jQuery(this).offset();
				var zeroLeft = (bTotal/2 + pictWidth) - (e.pageX - dOs.left);
				if(zeroLeft >= pictWidth/2) { 
					var uiprev = ui.parent().prev().find("a");	
					(jQuery(uiprev).length != 0)? uiprev.trigger("click") : ui.parent().parent().find("a:last").trigger("click");							
				} 
				else {
					var uinext = ui.parent().next().find("a");
				  	(jQuery(uinext).length != 0)? uinext.trigger("click") : ui.parent().parent().find("a:first").trigger("click");
				}
				*/
			});

			jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
			jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
			jQuery('body').append('<div class="tooltip" style="display:none;"><\/div>');
			
	
			if(settings.toolTip){
				var aref = jQuery("div.stripTransmitter" + j + " a");
				aref.live('mousemove', function(e) {
				var att = jQuery(this).attr('title');
				posX=e.pageX+10;
				posY=e.pageY+10;
				jQuery('.tooltip').html(att).css({'position': 'absolute', 'top': posY+'px', 'left': posX+'px', 'display': 'block', 'opacity': settings.ttOpacity});
			});
			aref.live('mouseout', function() {
				jQuery('.tooltip').hide();
			});				
			}
			});
		j++;
	});	
};
