var photoitems = new Array();

$(document).ready(function(){
 
		subgallery( 'http://api.flickr.com/services/feeds/photoset.gne?set=72157623127171955&nsid=46727219@N07&lang=en-us&format=json&jsoncallback=?');
 
		setupprevnext()
		
 });
 
 var photoitems = new Array();
 
 function subgallery(jsonurl)
 {
  
	var photoarray = new Array();

	var index = 0;
	var limit = 4;
  
	$.getJSON( jsonurl, function(data){
    $.each(data.items, function(i,item){
  
			//while (index < limit-1) {
			if ( index < limit )
			photoarray[index] = item.media.m ; 
			index ++;
			//}
	});
 
	// Show Photos in reverse order
		
	var subindex = 0;
	var currentphoto;
		
	// begin revers loop
	for (i=photoarray.length-1;i>=0;i--) {
		
	 currentphoto = photoarray[i];
	
	 $("<img />").attr("src", trim_s( currentphoto) ).appendTo("#MainList")
        .wrap("<li><a href='" + trim_m( currentphoto )+ "'></a></li>");
    
	 $("<img />").attr("src", trim_m( currentphoto ) ).appendTo("#gallerypreload") ; //.wrap(""); 
	
       // add lightbox after due to callback
       //  $("#" + galleryname + "Gallery a").lightBox();
		  
		  //alert (currentphoto);
		  $("a[href=" + trim_m( currentphoto ) + "]").live("click", function() { 
				// alert (currentphoto) ; 
				//alert( $(this).html() );
				//swapimage( (trim_m( currentphoto) ) );
				
					
				var newimage = $(this).attr("href");

				swapimage ( (newimage) );
				return false;
			}); 
			
		// set the initial image if needed
		
		if  ($("#mainimage img").attr("src")  == "gallery_new/images/placeholder.png" )
		{
			swapimage( trim_m( currentphoto )  );
			// set previous as first image
		}

		//alert(i);
		
		photoitems[subindex] = trim_m(currentphoto);
	
		//alert( photoitems[subindex] );
		
			subindex++;
		// end reverse loop
		}
	
	});
 }
 
 function subgallery_old(jsonurl, galleryname)
 {
 
	$.getJSON( jsonurl, function(data){
    $.each(data.items, function(i,item){
  
      $("<img />").attr("src", trim_s( item.media.m ) ).appendTo("#" + galleryname + "List")
        .wrap("<li><a href='" + trim_m( item.media.m )+ "'></a></li>");
  
	/* $("<img />").attr("src", item.media.m ).appendTo("#" + galleryname + "Nav")
        .wrap("<li><a href='" + trim_m( item.media.m )+ "'></a></li>");
  
	*/
  
	 $("<img />").attr("src", trim_m( item.media.m ) ).appendTo("#gallerypreload") ; //.wrap(""); 
	
       // add lightbox after due to callback
       //  $("#" + galleryname + "Gallery a").lightBox();
	   
	  
		  $("a[href=" + trim_m( item.media.m ) + "]").bind("click", function() { 
				/* alert (item.media.m) ;*/ 
				swapimage( trim_m( item.media.m )  );
				return false; 	
			}) 
			
		// set the initial image if needed
		
		if  ($("#mainimage img").attr("src")  == "gallery_new/images/placeholder.png" )
		{
			
			swapimage( trim_m( item.media.m )  );
			// set previous as first image
		}

		photoitems[photoitems.length] = trim_m(item.media.m);
			
			  
	});
 
	});
 }
 
 function setupprevnext()
 {
 
	$("#prev").bind("click", function() { prevphoto(); return false; } );
	$("#next").bind("click", function() { nextphoto(); return false; } );
	
	$("#galleryleft").bind("click", function() { prevphoto(); return false; } );
	$("#galleryright").bind("click", function() { nextphoto(); return false; } );
 
 }
 
  function swapimage(newimage)
  {

	var oldImage = $("#mainimage img");
    var newImage = $('<img height=271 width=355 src="' + newimage + '">');
  
	newImage.hide();
	$("#mainimage").prepend(newImage);
	
	newImage.fadeIn(1000)
	oldImage.fadeOut(1000, function () {
			$(this).remove();
		});
	
  }
  
  function prevphoto()
  {
		for (i=0;i<photoitems.length;i++)
		{
			//alert(i);
			if ( photoitems[i] == $("#mainimage img").attr("src") )
			{
				if ( photoitems[i-1] ) { 
					swapimage(photoitems[i-1]); }
				break;
			}	
		}	
  }
  
  function nextphoto()
  {
		for (i=0;i<photoitems.length;i++)
		{
			if ( photoitems[i] == $("#mainimage img").attr("src") )
			{
				if ( photoitems[i+1] ) 
				 { swapimage(photoitems[i+1]) ; }
				break;
			}	
		}	
		return false;
  }
   
  function trim_m(src)
  {
		/* Original just removed the _m */
  		src =  src.substring(0, src.length - 6) ; 	// remove "_m.jpg";
  		src =  src + "_m.jpg";		// _m is the medium one
  		return src;
  }
  
  function trim_s(src)
  {
		/* Original just removed the _m */
  		src =  src.substring(0, src.length - 6) ; 	// remove "_m.jpg";
  		src =  src + "_s.jpg";		// _s is the small one
  		return src;
  }
 
 


