var currentPhotoID;
var photoURL;
var url;

google.setOnLoadCallback(function() {

      $("#callFun").click(function ()
      {
        //e.preventDefault();
        //alert("id is "+'../images/goodlife_gallery/uploads/'+document.getElementById("callVal").value+'_Large.jpg');
        //var imageContent = '';
			 
				currentPhotoID = document.getElementById("callVal").value;
        $("#loader img").fadeOut("normal").remove();
        var largeImage = new Image();
        var imageSource = '../images/goodlife_gallery/uploads/'+currentPhotoID+'_Medium.jpg';
		$(largeImage).load(function()
		{
		    
		   $(this).hide();
			 
		   $("#loader").append(this);
			 
			 $("#loader").append('<a href="Panel-fullsize.html?photoID='+currentPhotoID+'" id="Fullsize_Link"></a>');
			 
			 photoURL = "http://www.goodliferecipe.com/goodlife-gallery/photos-new.aspx?photoId="+currentPhotoID;
			 photoTitle = "Check out dog and cat pictures like this one at The Goodlife Recipe Photo Gallery";
			 
			 $("#Fullsize_Link").append(this);
				$('#Fullsize_Link').nyroModal({
					type:'ajax',
					width:448,
					height:372,
					closeButton:'', // turn off default close button
					css:{
						wrapper:{
							border:'none',
							background:'transparent'
						}
					}
				});
				
		   $(this).fadeIn("slow");              
		});    
		$(largeImage).attr("src", imageSource);
		$(largeImage).attr("width", 316);
		$(largeImage).attr("height", 237);
		
		
		$.getJSON('../goodlife-gallery/PhotoDetails.aspx?id='+document.getElementById("callVal").value,function(data) {
		                   
							/*// Update large photo
							$('#Photo_Full').attr({
								src:'../images/goodlife_gallery/uploads/Thumb_'+data.id+'.jpg',
								alt:data.name
							});*/
							
							// Update details
							// 
							// if data.name is empty, hide the element, otherwise
							// display it and set it
							if (data.name!='') 
							{
								$('#Photo_Detail_Caption').html(data.name);
								$('#Photo_Detail_Caption').show();
							}
							else
							{
								$('#Photo_Detail_Caption').hide();
							}
							
							if (data.bio!="") {
								$('#Details_Text').html(data.bio);
								$('#Photo_Detail_Extra').fadeIn('fast');
							} else {
								$('#Photo_Detail_Extra').fadeOut('fast');
							}
							
							$('#Details_Location').html(data.location);
							
							if (data.breed=='undefined') { data.breed = ''; }
							$('#Details_Breed').html(data.breed);
							
							url = "http://www.goodliferecipe.com/goodlife-gallery/share-photo.aspx?photoid="+data.id;
							
							return false;
						});
		
		
        return false;
        //imageContent += '<img src="../images/goodlife_gallery/uploads/'+document.getElementById("callVal").value+'_Large.jpg" alt="">';
        //$('#large_photo div').append(imageContent);
        //$('#Photo_Full img').attr('src','../images/goodlife_gallery/uploads/'+document.getElementById("callVal").value+'_Large.jpg');
        
        
        
      });


	var photoIndices = new Array();
	var currentPhotoPage = 0;
	var lastPhotoPage;
	var i = 0;
	
	// Initialize Gallery
	var initPhotoPage = function() {
		//$.get('photo_search_results.csv',function(data) { // Use this to pull from CSV file
		//photoIndices = data.split(','); // Use this to pull from hidden input value
		photoIndices = $('#Search_Results_CSV').val().split(','); // Use this to pull from hidden input value
		
		$('#Photo_Slider').scrollTo(0,0,{axis:'x'});
		lastPhotoPage = Math.ceil(photoIndices.length/6);
		$('#Total_Pages_Text').html(lastPhotoPage);
		nextPhotoPage();
		if(photoIndices=='')
		{
		    $("#Photo_Slider div").remove();
		}
		//}); // close AJAX CSV call
	}
	
	  
	    photoIndices = $('#Search_Results_CSV').val().split(',');

		// if on the url load it here
		if (getQueryVariable('photoId')!='') {

			myFunc(getQueryVariable('photoId'));
		}else{
			myFunc(photoIndices[0]);
		}
		/*
		$.getJSON('../goodlife-gallery/PhotoDetails.aspx?id='+document.getElementById("callVal").value,function(data) {
		
		                   
							// Update large photo
							$('#Photo_Full').attr({
								src:'../images/goodlife_gallery/uploads/Thumb_'+data.id+'.jpg',
								alt:data.name
							});
							
							// Update details
							$('#Photo_Detail_Caption').html(data.name);
							$('#Details_Text').html(data.bio);
							$('#Details_Location').html(data.location);
							$('#Details_Breed').html(data.breed);
							return false;
						});
						*/
	    

	
	// Go to next page in the gallery
	var nextPhotoPage = function() {
		
		if (currentPhotoPage<lastPhotoPage) { // Make sure user is not on the last page
			// Disable next/prev page functionality
			$('#Right_Arrow').unbind('click');
			$('#Left_Arrow').unbind('click');
			
			currentPhotoPage++;
			
			if ($('#Photos_Page_'+currentPhotoPage).attr('id')==null) { // Check if requested page has been created yet
				
				var firstPhotoIndex = (currentPhotoPage - 1) * 6;
				var newPageContent = '';
				
				// Build page markup (table)
				newPageContent += '<table id="Photos_Page_'+currentPhotoPage+'"><tr>';
				for (i=0;i<=2;i++) {
					if (i==1) { newPageContent += '<td class="Center">'; } else { newPageContent += '<td>'; }
					if (firstPhotoIndex+i<photoIndices.length) {
						newPageContent += '<img src="../images/goodlife_gallery/uploads/'+photoIndices[firstPhotoIndex+i]+'.jpg" alt="" style="width:84px;height:62px;" onclick="myFunc('+photoIndices[firstPhotoIndex+i]+');"></td>';
					} else {
						newPageContent += '</td>';
					}
				}
				newPageContent += '</tr><tr>';
				for (i=3;i<=5;i++) {
					if (i==4) { newPageContent += '<td class="Center">'; } else { newPageContent += '<td>'; }
					if (firstPhotoIndex+i<photoIndices.length) {
						newPageContent += '<img src="../images/goodlife_gallery/uploads/'+photoIndices[firstPhotoIndex+i]+'.jpg" alt="" style="width:84px;height:62px;" onclick="myFunc('+photoIndices[firstPhotoIndex+i]+');"></td>';
					} else {
						newPageContent += '</td>';
					}
				}
				newPageContent += '</tr></table>';
				
				$('#Photo_Slider div').append(newPageContent); // Append page markup to DOM
				
				
				
			}
			
			// Slide to next page
			$('#Photo_Slider').scrollTo($('#Photos_Page_'+currentPhotoPage),333,{axis:'x',onAfter:function() {
				// After sliding, restore next/prev page functionality
				$('#Right_Arrow').bind('click',nextPhotoPage);
				$('#Left_Arrow').bind('click',prevPhotoPage);
			}});
			
			$('#Current_Page_Text').html(currentPhotoPage); // Update page number
			
			// Update page arrow statuses
			if (currentPhotoPage==lastPhotoPage) { $('#Right_Arrow img').attr('src','../images/goodlife_gallery/photos_arrow_next_off.gif'); }
			if (currentPhotoPage==2) { $('#Left_Arrow img').attr('src','../images/goodlife_gallery/photos_arrow_prev_on.gif'); }
			
		}
		
	}
		
	// Go to previous page in the gallery
	var prevPhotoPage = function() {
		if (currentPhotoPage>1) { // Make sure user is not on the first page
			// Disable next/prev page functionality
			$('#Right_Arrow').unbind('click');
			$('#Left_Arrow').unbind('click');
			
			currentPhotoPage--;
			
			// Slide to previous page
			$('#Photo_Slider').scrollTo($('#Photos_Page_'+currentPhotoPage),333,{axis:'x',onAfter:function() {
				// After sliding, restore next/prev page functionality
				$('#Right_Arrow').bind('click',nextPhotoPage);
				$('#Left_Arrow').bind('click',prevPhotoPage);
			}});
			
			$('#Current_Page_Text').html(currentPhotoPage); // Update page number
			
			// Update page arrow statuses
			if (currentPhotoPage==(lastPhotoPage-1)) { $('#Right_Arrow img').attr('src','../images/goodlife_gallery/photos_arrow_next_on.gif'); }
			if (currentPhotoPage==1) { $('#Left_Arrow img').attr('src','../images/goodlife_gallery/photos_arrow_prev_off.gif'); }
		}
	}
	
	// Show more details
	var showMore = function() {
		$('#Photo_Detail_Extra').slideDown();
		$('#Details_Toggle_Link').unbind('click');
		$('#Details_Toggle_Link').bind('click',showLess);
		$('#Details_Toggle_Link').html('Show Less &minus;');
	}
	
	// Show less details
	var showLess = function() {
		$('#Photo_Detail_Extra').slideUp();
		$('#Details_Toggle_Link').unbind('click');
		$('#Details_Toggle_Link').bind('click',showMore);
		$('#Details_Toggle_Link').html('Show More +');
	}
			
	var omniTrack = function(pageName) {
		s=s_gi('pedigreegoodliferecipe');
		s.pageName=pageName;
		s.t()
	}
	
	// Show upload
	var showUpload = function() {
		omniTrack('Gallery Upload Clicked');
		$('#Comic_Gallery').hide();
		$('#Upload_Gallery').show();
	}
	
	// hide upload
	var showGallery = function() {
		$('#Comic_Gallery').show();
		$('#Upload_Gallery').hide();
	}
	
	// Event binding
	$('#Right_Arrow').bind('click',nextPhotoPage);
	$('#Left_Arrow').bind('click',prevPhotoPage);
	$('#Details_Toggle_Link').bind('click',showMore);
	$('#Gallery_Upload_Link').bind('click',showUpload);
	
	initPhotoPage(); // Showtime!
});

 
function getQueryVariable(variable) { 
  var query = window.location.search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++) { 
    var pair = vars[i].split("="); 
    if (pair[0] == variable) { 
      return pair[1]; 
    } 
  } 
  //alert('Query Variable ' + variable + ' not found');
  return '';
} 
