//JQuery Gallery v1.1
function Gallery(params){
	
	//wrapper, xmlPath, slideInterval, layout, looping, animSpeed, animTransition, imagesClickable, labelDelimiter, nodesAlign
	
	var $this;
	if(params.wrapper == null){ alert("Error, wrapper class not specified"); }
	else { $this = "." + params.wrapper; }
	
	if(params.xmlPath == null){ alert("Error, xmlPath not specified"); }
	if(params.slideInterval == null){ params.slideInterval = 7000; }
	if(params.layout == null){ params.layout = "minimal"; }
	if(params.looping == null){ params.looping = true; }
	if(params.animSpeed == null){ params.animSpeed = 500; }
	if(params.animTransition == null){ params.animTransition = "easeOutExpo"; }
	if(params.imagesClickable == null){ params.imagesClickable = false; }
	if(params.labelDelimiter == null){ params.labelDelimiter = "/"; }
	if(params.nodesAlign == null){ params.nodesAlign = "center"; }
	if(params.thumbsAlign == null){ params.thumbsAlign = "center"; }
	
	var numImages = 0;
	var currentNum = 0;
	var slideTimer;
	var imageWidth;
	var imageHeight;
	var moverX = 0;
	var linkArray = new Array();
	var titleArray = new Array();
	var nodesOn = true;
	var thumbsOn = false;
	var linkDivOn = false;
	var arrowsOn = true;
	
	
	$($this).empty();
	
	///////////////////////////////LAYOUTS///////////////////////////////
	var htmlArray = new Array();
	//MAIN START - this part normally goes first
	htmlArray.push('<div class="galleryContainer"><div class="mover" style="width: 20000px"></div>');						/*0 */
	//MAIN START END - this part ends galleryContainer, things after this normally float over it
	htmlArray.push('</div>');																								/*1 */
	//nav button wrapper open
	htmlArray.push('<div class="navBtns">');																				/*2 */
	//nav button wrapper close
	htmlArray.push('</div>');																								/*3 */
	//nav button right
	htmlArray.push('<div class="rightBtn"><a href="#"></a></div>');															/*4 */
	//nav button left
	htmlArray.push('<div class="leftBtn"><a href="#"></a></div>');															/*5 */
	//link div for nav button wrapper
	htmlArray.push('<div class="linkDiv"></div>');																			/*6 */
	//nav bar open
	htmlArray.push('<div class="navBar">');																					/*7 */
	//nav bar close
	htmlArray.push('</div>');																								/*8 */
	//text label
	htmlArray.push('<div class="textLabel"><p></p></div>');																	/*9 */
	//number label
	htmlArray.push('<div class="numberLabel"><p></p></div>');																/*10*/
	//nav nodes
	htmlArray.push('<div class="navNodesWrapper"><ul class="navNodes"></ul><div style="clear:both;"></div></div>');			/*11*/
	//thumbnails
	htmlArray.push('<div class="thumbsWrapper"><ul class="thumbnails"></ul><div style="clear:both;"></div></div>');			/*12*/
	//clearing div
	htmlArray.push('<div style="clear:both;"></div>');																		/*13*/
	//nav left section open
	htmlArray.push('<div class="leftSection">');																			/*14*/
	//nav left section close
	htmlArray.push('</div>');																								/*15*/
	//nav right section open	
	htmlArray.push('<div class="rightSection">');																			/*16*/
	//nav right section close
	htmlArray.push('</div>');																								/*17*/
	
	var layoutArray = new Array();
	//minimal
	layoutArray.push(new Array(0,2,4,5,6,3,1,7,11,8));
	//titleOverlay
	layoutArray.push(new Array(0,1,7,4,5,9,11,8));
	//thumbOverlayLeft
	layoutArray.push(new Array(0,1,7,14,4,5,10,15,16,12,17,8));
	//thumbOverlayRight
	layoutArray.push(new Array(0,1,7,14,12,15,16,4,5,10,17,8));
	//nodesOnly
	layoutArray.push(new Array(0,1,7,11,8));
	//arrowsOnly
	layoutArray.push(new Array(0,2,4,5,6,3,1));
	//arrowsOnlyTitleAbove
	layoutArray.push(new Array(9,0,2,4,5,6,3,1));
	/////////////////////////////END LAYOUTS/////////////////////////////

	//Fill out layout html
	var layoutIndex = 0;
	switch(params.layout){
		case "titleOverlay":
			layoutIndex = 1;
			break;
		case "thumbOverlayLeft":
			layoutIndex = 2;
			nodesOn = false;
			thumbsOn = true;
			break;
		case "thumbOverlayRight":
			layoutIndex = 3;
			nodesOn = false;
			thumbsOn = true;
			break;
		case "nodesOnly":
			layoutIndex = 4;
			arrowsOn = false;
			break;
		case "arrowsOnly":
			layoutIndex = 5;
			nodesOn = false;
			break;
		case "arrowsOnlyTitleAbove":
			layoutIndex = 6;
			nodesOn = false;
			break;
	}
	
	var mainHTML = '';
	
	var i = 0;
	$.each(layoutArray[layoutIndex], function(){
		mainHTML += htmlArray[layoutArray[layoutIndex][i]];
		if(layoutArray[layoutIndex][i] == 6)
		{
			linkDivOn = true;
		}
		i++;					  
	});
	
	$($this).append(mainHTML);
						   
	if(arrowsOn)
	{
		$($this + " .rightBtn a").click(function(event){
		 event.preventDefault();
		 getImage(-2);
	   });
	   
	   $($this + " .leftBtn a").click(function(event){
		 event.preventDefault();
		 getImage(-1);
	   });
	}
						   
	/*imageWidth = $($this).width();
	imageHeight = $($this).height();
	add above back in if things break
	*/
	imageWidth = $($this + ' .galleryContainer').width();
	imageHeight = $($this + ' .galleryContainer').height();
	
	
	$.ajax({
        type: "GET",
		url: params.xmlPath,
		dataType: "xml",
		success: function(xml) {
			$(xml).find('xml').each(function(){
											 
				numImages = $(this).find('item').length;
				
				var i = 0;
				
				$(this).find('item').each(function(){
					var imagePath = encodeURI($(this).find('file').text());
					var linkURL = encodeURI($(this).find('link').text());
					linkArray.push(linkURL);
					titleArray.push($(this).find('title').text());
					
					var imageHTML = "";
					
					if(params.imagesClickable)
					{
						imageHTML += '<a href="' + linkURL + '">';
					}					
					imageHTML += '<img src="' + imagePath + '" width="' + imageWidth + '" height="' + imageHeight + '" />';
					if(params.imagesClickable)
					{
						imageHTML += '</a>';
					}
					
					$($this + " .mover").append(imageHTML);
					
					
					if(nodesOn)
					{
						$($this + " .navNodes").append('<li><div class="node"><a href="#" style="display:block; width:100%; height:100%;"><div class="fadeThis"><p>h</p><span class="hover"></span></div></a></div></li>');
					
						$($this + " .fadeThis:last").each(function () {
							var $span = $($this + " > span.hover", this).css('opacity', 0);
						});
						
						var imageNum = i;
						
						$($this + " .node:last a").click(function(event){
							 event.preventDefault();
							 getImage(imageNum);
						});
					}
					
					if(thumbsOn)
					{
						$($this + " .thumbnails").append('<li><div class="thumb"><a href="#" style="display:block; width:100%; height:100%;"><div class="thumbFadeThis"><p>h</p><span class="hover"></span></div><img src="' + imagePath + '" /></a></div>');
					
						$($this + " .thumbFadeThis:last").each(function () {
							var $span = $($this + " > span.hover", this).css('opacity', 0);
						});
						
						var imageNum = i;
						
						$($this + " .thumb:last a").click(function(event){
							 event.preventDefault();
							 getImage(imageNum);
						});
					}
					
					i++;
				});
			});
			
			
/*			$('.navBar').mouseenter(function() {
				$(this).stop().animate(
					{
						marginTop: 200,
						height: 130
					},
					500,
					easeOutExpo'
				);
			});
			
			$('.navBar').mouseleave(function() {
				$(this).stop().animate(
					{
						marginTop: 282,
						height: 48
					},
					500,
					easeInOutSine'
				);
			});
*/			
		  
		 
			numImages = $(xml).find('item').length;
			
			if(linkDivOn)
			{
				var totalString = new String();
				for(var m = 0; m < linkArray.length; m++)
				{
					totalString += linkArray[m];
				}
				
				if(totalString != "")
				{
					$($this + " .linkDiv").click(function(event){
						event.preventDefault();
						getLink();
					});
				}
			}
			
			if(nodesOn)
			{
				if(params.nodesAlign != "left")
				{
					var liWidth = parseInt($($this + " .navNodes li").css('width'));
					liWidth += parseInt($($this + " .navNodes li").css('marginLeft'));
					liWidth += parseInt($($this + " .navNodes li").css('marginRight'));
					var newWidth = liWidth * numImages;
					var parentWidth = parseInt($($this + " .navNodesWrapper").css('width'));
					
					if(params.nodesAlign == "right")
					{
						$($this + " .navNodes").css('marginLeft', (parentWidth - newWidth));
					}
					else
					{
						$($this + " .navNodes").css('marginLeft', (parentWidth - newWidth) / 2);
					}
				}
			}
			
			if(thumbsOn)
			{
				if(params.thumbsAlign != "left")
				{
					var liWidth = parseInt($($this + " .thumbnails li").css('width'));
					liWidth += parseInt($($this + " .thumbnails li").css('marginLeft'));
					liWidth += parseInt($($this + " .thumbnails li").css('marginRight'));
					var newWidth = liWidth * numImages;
					var parentWidth = parseInt($($this + " .thumbsWrapper").css('width'));
					
					if(params.nodesAlign == "right")
					{
						$($this + " .thumbnails").css('marginLeft', (parentWidth - newWidth));
					}
					else
					{
						$($this + " .thumbnails").css('marginLeft', (parentWidth - newWidth) / 2);
					}
				}
			}
			
			if(numImages == 1)
			{
				$($this + " .navBtns").empty();
				$($this + " .navBar").empty();
			}
			getImage(0);
		},
		error:function (xhr, ajaxOptions, thrownError){
			alert('Could not load XML "' + params.xmlPath + '", thrown error: ' + thrownError);
		} 
		
		
	}); 
	
	var getImage = function(num)
	{
		
		
		if(num == -1)
		{//left
			num = currentNum - 1;
		}
		else if(num == -2)
		{//right
			num = currentNum + 1;	
		}
		
		if(params.looping)
		{
			if(params.slideInterval > 0)
			{
				clearTimeout(slideTimer)
				slideTimer = setTimeout(nextImage, params.slideInterval);
			}
		}
		else if (num < numImages - 1)
		{
			if(params.slideInterval > 0)
			{
				clearTimeout(slideTimer)
				slideTimer = setTimeout(nextImage, params.slideInterval);
			}
		}		
		
		if(num < 0)
		{
			num = numImages - 1;
		}
		else if (num > numImages - 1)
		{
			num = 0;
		}
		
		currentNum = num;
	
		$($this + " div.mover").stop().animate(
			{marginLeft: -(imageWidth * num)},
			{duration: params.animSpeed,
			easing: params.animTransition});
		
		$($this + " .textLabel p").empty();
		$($this + " .textLabel p").append(titleArray[num]);
		
		$($this + " .numberLabel p").empty();
		$($this + " .numberLabel p").append((num+1) + params.labelDelimiter + numImages);
		
		$($this + " .node .fadeThis span.hover").stop().fadeTo(200, 0);
		$($this + " .thumb .thumbFadeThis span.hover").stop().fadeTo(200, 0);
		num++;
		$($this + " .navNodes li:nth-child(" + num + ") .fadeThis span.hover").stop().fadeTo(200, 1);
		$($this + " .thumbnails li:nth-child(" + num + ") .thumbFadeThis span.hover").stop().fadeTo(200, 1);
		
	}
	
	var nextImage = function()
	{
		getImage(-2);
	}
	
	var getLink = function()
	{
		window.location = linkArray[currentNum];
	}
	
}