/*
//notvena
instructions
1. inside the list that contains content that should scroll define width and height for li element, in the css
2. if imgThumbs is set for the gallery - thumb images should be in the same folder where the main image is with the suffix _thumb
for example 01.jpg should have 01_thumb.jpg in the same folder
*/

/*
added:
1. width of the container is the width of the list that containes the scrolling content, not width/height of the image. That way any content, not just image can be added to the li tag
2. OPTIONS: imgThumbs option in settings - creates the links with image thumbs 
($("div#gallery1").slideView({imgThumbs: true});)

*/

var $thumbListWidth = -1;
var $nomoreNext = false;
var $nomorePrev = true;

//alert($nomoreNext + " : "+ $nomorePrev);
var $thumbListWidthbool = false;

jQuery(function()
	{
	jQuery("div.svw").prepend("<img src='http://www.gcmingati.net/wordpress/wp-content/uploads/svwloader.gif' class='ldrgif' alt='loading...'/ >"); 
	});

var j = 0;



jQuery.fn.slideView = function(settings) 
{
settings = jQuery.extend({
	easeFunc: "easeInOutExpo", /* <-- easing function names changed in jquery.easing.1.2.js */
    easeTime: 750,
    toolTip: false
  	}, settings);
return this.each(function()
	{
	var container = jQuery(this);
	container.find("img.ldrgif").remove(); // removes the preloader gif
	container.removeClass("svw").addClass("stripViewer");		
	var pictWidth = container.find("li").find("img").width();
	var pictHeight = container.find("li").find("img").height();
	// notvena - container width added
	var containerWidth = container.find("li").width();
	var containerHeight = container.find("li").height();	


	var pictEls = container.find("li").size();
	//var stripViewerWidth = pictWidth*pictEls; // old line

	// notvena
	/* use container width not image*/
	var stripViewerWidth = containerWidth*pictEls;		 

	// notvena 
	/*
	set the width and height of the container as width/height of the list
	*/
	container.css("width" , containerWidth);	
	container.css("height" , containerHeight);			

	container.find("ul").css("width" , stripViewerWidth); //assegnamo la larghezza alla lista UL	
	container.each(function(i) {
								
    var insertBeforeThumbs = '<div id="thumbgallery">'+
    	'<a href="#" id="prev" class="nomore">Prev</a>' +
        '<div class="frame">';	
	var insertAfterThumbs = '</div>' +  '<a href="#" id="next">Next</a>' +
           '</div>';
		   
		   
	   
								
	jQuery(this).after(insertBeforeThumbs+"<div class='stripTransmitter' id='stripTransmitter" + j + "'><ul><\/ul><\/div>"+insertAfterThumbs);
	jQuery(this).find("li").each(function(n) 
		{

		// notvena
		/* if imgThumb is true set image thumbs instead of just numbers */
		if (settings.imgThumbs)
			{
			var currentSrc  = jQuery(this).find("img.bigimg").attr("src");
			if (currentSrc!=null)
				var thumbSrc = currentSrc.substring(0,currentSrc.length-4)+ "_thumb" + currentSrc.substring(currentSrc.length-4);
			else 
				var thumbSrc = url + "images/mov-noimage.gif";

			var imgThumb = '<img height="54" src="'+thumbSrc+'" alt="' + jQuery(this).find("img").attr("alt") + '"/>'
			jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+imgThumb+"<\/a><\/li>");						
			}
		else
			jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");	
			
					
		});


	
	jQuery("div#stripTransmitter" + j + " a").each(function(z) 
		{
		jQuery(this).bind("click", function()
			{
	  		jQuery(this).parent().parent('ul').children('li').removeClass("current");	
			jQuery(this).parent().addClass("current");
			// notvena
			/* cnt is not calculated by image but container width */
			//var cnt = - (pictWidth*z); //old line
			//var cntold = - (pictWidth*z); 			
			var cnt = - (containerWidth*z);
			jQuery(this).parents("#thumbgallery").prev().find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);		
			return false;
			});
		});
			
		jQuery("div#stripTransmitter" + j).css("width" , containerWidth);			
		// notvena - put current on list not a
		jQuery("div#stripTransmitter" + j + " li:eq(0)").addClass("current");			
	
		// notvena
		setSlider();

	
		if(settings.toolTip)
			{
			container.next(".stripTransmitter ul").find("a").Tooltip({
				track: true,
				delay: 0,
				showURL: false,
				showBody: false
				});
			}
	}); // end each
	j++;
  });	// end settings extend 
}; // end jQuery.fn.slideView

/* notvena - added gallery slider */

$step = 128* 4;
$step = (102 + 20)* 4; /* 102 is li width plus 22 for li margin*/

jQuery.easing.def = "easeOutQuint";

function calculateWidth()
	{
	$('#thumbgallery .stripTransmitter ul li').each(function()
		{
		$thumbListWidth1 +=$(this).width(); 
				
		// for padding difference
		$thumbListWidth1 = $thumbListWidth1 + parseInt($(this).css("margin-right"), 10) + parseInt($(this).css("margin-left"), 10);
		});
	// remove margin of last eleemnt
	//$thumbListWidth1 -= parseInt($('#thumbgallery .stripTransmitter ul li').css("margin-right"), 10);
	$visiblePart = parseInt($('#thumbgallery .frame').css('width'), 10);
	//alert($thumbListWidth1 + " : " + $visiblePart);
	if ($thumbListWidth1 <= $visiblePart)
		{
		$nomoreNext = true;
		$('#next').addClass('nomore');
		}
	}

function setSlider()
	{
	$nomoreNext = false;
	$nomorePrev = true;		
	$thumbListWidth = -1;
	$thumbListWidthbool = false;

	$thumbListWidth1 = 0;
	setTimeout("calculateWidth();", 1000);


	$('.frame').css("overflow","hidden");
	$('#thumbgallery .stripTransmitter').css({'overflow':'hidden'});
	$('#next').bind('click', function(e) {
		e.preventDefault();
		slideNext();
        return false;
    	});
    $('#prev').bind('click', function(e) {
		e.preventDefault();
		slidePrev();
        return false;
		});		
	}




function slidePrev()
	{
	if (!$nomorePrev)
		{
		$newleft = parseInt($('#thumbgallery .stripTransmitter ul').css('left'), 10);

		if (($thumbListWidth - parseInt($('#thumbgallery .stripTransmitter ul').css('width'), 10)) < 0)
			{
			$('#next').removeClass('nomore');
			$nomoreNext = false;			
			}
	
		$newleft = parseInt($('#thumbgallery .stripTransmitter ul').css('left'), 10) + $step;
		$('#thumbgallery .stripTransmitter ul').animate({left:$newleft}, 1200, 'easeOutQuint');	
		
		
		if($newleft >= 0)	
			{
			$('#prev').addClass('nomore');
			$nomorePrev = true;						
			}
		}
	}



function slideNext()
	{
	if (!$nomoreNext)
		{
		if ($thumbListWidthbool == false)
			{
			$thumbListWidth = 0;
			$('#thumbgallery .stripTransmitter ul li').each(function(){
				$thumbListWidth +=$(this).width(); 
				
				// for padding difference
				$thumbListWidth = $thumbListWidth - parseInt($(this).css("margin-right"), 10);// + parseInt($(this).css("margin-left"), 10);
				});
			$thumbListWidthbool = true;
			}
		$newleft = parseInt($('#thumbgallery .stripTransmitter ul').css('left'), 10) - $step;
	
		$('#thumbgallery .stripTransmitter ul').animate({left:$newleft}, 1200, 'easeOutQuint');		
		
		if (($newleft) <= 0 )
			{
			$('#prev').removeClass('nomore');
			$nomorePrev = false;
			}
		else
			{
			$('#prev').addClass('nomore');
			$nomorePrev = true;		
			}

		var $diff = ($thumbListWidth + $newleft);
		//alert($diff);
		if ($diff < $step)
			{
			$('#next').addClass('nomore');
			$nomoreNext = true;
			}
		}
	}
