﻿/**
,Tom McLeod - PageLime LLC
* @copywrite - 2010
**/

//Variables


var effects= "on"
var galleryPath = "cms-assets/image-galleries/";
var xmlPath = "cms-assets/image-galleries/"+ xmlID +".xml";
var imageList = [];
var imageThumbList = [];
var width = [];
var height = [];
var desc =[];
var i =0;
var galleryName = "fgf";

//Document Ready Funtion
$(document).ready(function() {
						   startLoad();  
						   if(galleryType==("grid")){
							   //Loads the ShadowBox script if a GRID style gallery is selected.
							   Shadowbox.init();
							 } 
							 });

//Load the XML file based on the path defined in the XMLPath Variable above.
function startLoad(){
$.ajax({
				type: "GET",
				url: xmlPath,
				dataType: "xml",
				success: parseXML
				});
};



//Parse the XML file
function parseXML(xml){
	$(xml).find("Gallery").each(function() {
		var galleryID = $(this).attr('ID');
		galleryName = $(this).attr('Name');
		
		$(this).find("Images").each(function(){
		
			$(this).find("ImageGalleryEntry").each(function(){
				 var fileName = $(this).attr("FileName");
				 var imWidth = $(this).attr("Width");
				 var imHeight = $(this).attr("Height");
				 var imDesc = $(this).attr("Description");
				 var path = galleryPath + galleryID +'/images/' ;
				 var thumbPath = galleryPath + galleryID +'/thumbnails/' ;
				  width.push(imWidth);
				  height.push(imHeight);
				  imageList.push(path+fileName);
				  imageThumbList.push(thumbPath + fileName);
				  desc.push(imDesc);
				  });
			});
		});
	if(galleryType==("grid")){
		gridLaunch();
	} if(galleryType==("rotator")){
		preloader();
	}
};


//Image Preloader this is optional. This preloads images into a hidden div with the class .preload that has to be created. e.g <div class="preload" style="height:0; width:0; position:absolute;"></div>
function preloader() {

     for(var g=0; g<=imageList.length-1; g++) 
     {
          $(".preload").append("<img src=" + imageList[g]  + "  width='0' height='0'	 />");	
		  
     }
	
	 var initialWidth = width[0] + "px";
	 $(imageSectionID).css("width",initialWidth);
     $(imageSectionID).fadeIn(1000, function(){plrotate()} );
	


} ;

//If using the grid version of the plugin this creates the grid and the lightbox and even adds some jQuery effects.
function gridLaunch() {
	
     for(var j=0; j<=imageList.length-1; j++) 
     {
          $(imageSectionID).prepend("<span class='smallPic' style='display:none;'><a href= ' "+ imageList[j] +" '  rel='lightbox[ " + galleryName + " ]' title=' " + desc[j] + " ' ><img src=" + imageThumbList[j]  + "  /></a></span>");	
		  if(effects==("on")){
		  $(".smallPic").fadeIn(1000);
		  
		  $(".smallPic").mouseenter(function(){ $(this).fadeTo(50, 0.60);})
		  
		  $(".smallPic").mouseleave(function(){ $(this).fadeTo(50, 1);})
		  }
     }
	$(imageSectionID).prepend("<h2> " + galleryName  + "</h2>");	
} ;




function boxAnimate(){
	
	if(width[i] >height[i] ){
	$(imageSectionID).animate({
						  height: height[i],
						  width: width[i],
						  paddingBottom: "10px",
						  left:"380px"
						  
						  },{duration: 'slow'});
	
							} else {
			$(imageSectionID).animate({
						  height: height[i],
						  width: width[i],
						  paddingBottom: "10px",
						  left:"480px"
						  
						  },{duration: 'slow'});
			
			};
			imageStart(i);
};

function imageStart(){
  	$("#description").html("");
	$("#description").append(desc[i]);
	$(imageClass).fadeIn(1000);
	var t=setTimeout("imageFade()",5000);
};
	

	
function imageFade(){
	$(imageClass).fadeOut(1000, function(){imageNext(i)});
};	
	
function imageNext(){
i++;
if(i<=(imageList.length-1)){
			plrotate(i);
		} else {
			i=0;
			plrotate(0);
		};
};	

function plrotate(){
	$(imageClass).attr({ src: imageList[i] });	
	boxAnimate();
	
	
};
					
