// Custom Discovery Calls

		// Global Variable to keep track of gallery keys
		var keys = new Array();
	
		function handleDiscoveryErrors(responseBatch) {
					var s = '';
					for (var i = 0; i < responseBatch.Messages.length; i++) {
							s = s + '<p>' + responseBatch.Messages[i].Message + '</p>';
					}
					$('errors').className = "errorsPresent";
					$('errors').innerHTML = s;
			}



			// === Recent Photo Galleries ==============================================================================
			function showRecentPhotoGalleries(ItemLimit,QueryTags) {
				
				if (!ItemLimit) ItemLimit = 5;
				if (!QueryTags) QueryTags = "All";

				var searchSections = new Array();
					searchSections[0] = new Section("All");
				var searchCategories = new Array();
					searchCategories[0] = new Category(QueryTags);
				var limitToContributorsDisco = new Array();
					limitToContributorsDisco[0] = new UserTier("All");
				var activityDisco = new Activity("Recent");
				var contentType = new ContentType("PublicPhotoGallery");
				var age = 15;
				var maximumNumberOfDiscoveries = ItemLimit;
			
				var requestBatch = new RequestBatch();
				var discoveryAction = new DiscoverContentAction(
							searchSections, 
							searchCategories, 
							limitToContributorsDisco, 
							activityDisco, 
							contentType, 
							age, 
							maximumNumberOfDiscoveries);
				requestBatch.AddToRequest(discoveryAction);
				requestBatch.BeginRequest(serverUrl, DiscoveryPhotoGalleriesCallback);
				return;
			}

			function DiscoveryPhotoGalleriesCallback(responseBatch){
				if (responseBatch.Responses.length == 0) {
				if (responseBatch.Messages.length == 1 && responseBatch.Messages[0].Message == 'ok') return;
					handleDiscoveryErrors(repsonseBatch);
					return;
				}
				var discoverRecentGalleries = responseBatch.Responses[0].DiscoverContentAction;
				
				var PhotoHTML = "<ul class='RecentPhotoGalContainer'>";		
				for (var i = 0; i < discoverRecentGalleries.DiscoveredContent.length; i++) {
					var info = discoverRecentGalleries.DiscoveredContent[i];
					PhotoHTML += "<li>";
						PhotoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/photogallery/index.html?plckGalleryID=" +info.GalleryKey.Key+ "'><img src='" + info.GalleryPromo.Image.Medium + "'/></a></div>";
						PhotoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/photogallery/index.html?plckGalleryID=" +info.GalleryKey.Key+ "'>" + info.Title +"</a></div>";
						PhotoHTML += "<div class='RecentMediaDescription'>"+info.Description+"</div>";
						PhotoHTML += "<div class='RecentMediaCreated'> "+ info.CreatedOn +" </div>";
					PhotoHTML += "</li>";
				}
				PhotoHTML += "</ul>";
				$("#PhotoGalleryDiscovery").append(PhotoHTML);
		}



			// === Show Photo Gallery By ID ==============================================================================
			function showPhotoGalleryByID(key) {
				var requestBatch = new RequestBatch();
				requestBatch.AddToRequest(new GalleryKey(key));
    	  requestBatch.BeginRequest(serverUrl, DiscoverySpecificPhotoGalleryCallback);
			}

			function DiscoverySpecificPhotoGalleryCallback(responseBatch){
        var gallery = responseBatch.Responses[0].Gallery;
				var PhotoHTML = "<ul class='SpecificPhotoGalContainer'>";		
					PhotoHTML += "<li>";
						PhotoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/photogallery/index.html?plckGalleryID=" +gallery.GalleryKey.Key+ "'><img src='" + gallery.GalleryPromo.Image.Medium + "' alt='" + gallery.Title + "' /></a></div>";
						PhotoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/photogallery/index.html?plckGalleryID=" +gallery.GalleryKey.Key+ "'>" + gallery.Title +"</a></div>";
						PhotoHTML += "<div class='RecentMediaDescription'>" + gallery.Description +"</div>";
						PhotoHTML += "<div class='RecentMediaCreated'> "+ gallery.CreatedOn +" </div>";
					PhotoHTML += "</li>";
				PhotoHTML += "</ul>";
				$("#SpecificPhotoGallery").html(PhotoHTML);
			}



//////////////////////////////////////////////////////////////////////////////////////////
        function CustomProfilePage(key, elementID){
						var requestBatch = new RequestBatch();
						requestBatch.AddToRequest(new GalleryKey(key));
            
            // save this so we can get back into our object.
            var This = this;
            var cb = function(response){
                // debug when firebug is enabled                
                try {
                    TestCallBack(response.Responses[0].Gallery, elementID);
                } 
                catch (e) {
                    alert('something broke');
                }
                
            }
            requestBatch.BeginRequest(serverUrl, cb);
        }

				function TestCallBack(responseBatch,ElementID){				
						var gallery = responseBatch;
						return;
				}
//////////////////////////////////////////////////////////////////////////////////////////



			// === Recent Photos by Tag ==============================================================================
			function showRecentPhotos(elementID,ItemLimit,QueryTags) {
				if (!ItemLimit) ItemLimit = 5;
				if (!QueryTags) QueryTags = "All";

				var searchSections = new Array();
					searchSections[0] = new Section("All");
				var searchCategories = new Array();
					var searchTag=QueryTags.split(",");
						searchCategories[0] = new Category(searchTag[0]);
						searchCategories[1] = new Category(searchTag[1]);
						searchCategories[2] = new Category(searchTag[2]);
						searchCategories[3] = new Category(searchTag[3]);
						searchCategories[4] = new Category(searchTag[4]);
				var limitToContributorsDisco = new Array();
					limitToContributorsDisco[0] = new UserTier("All");
				var activityDisco = new Activity("Recent");
				var contentType = new ContentType("PublicPhoto");
				var age = 15;
				var maximumNumberOfDiscoveries = ItemLimit;
			
				var requestBatch = new RequestBatch();
				var discoveryAction = new DiscoverContentAction(
							searchSections, 
							searchCategories, 
							limitToContributorsDisco, 
							activityDisco, 
							contentType, 
							age, 
							maximumNumberOfDiscoveries);
				requestBatch.AddToRequest(discoveryAction);

				// save this so we can get back into our object.
				var This = this;
				var cb = function(responseBatch){
						try { DiscoveryPhotoCallback(responseBatch.Responses[0].DiscoverContentAction,elementID,QueryTags); }
						catch (e) {
//							alert('something broke with showRecentPhotos');
						}
				}

				requestBatch.BeginRequest(serverUrl, cb);
				return;
			}

			function DiscoveryPhotoCallback(responseBatch,elementID,QueryTags){
/* trying this a new way
				if (responseBatch.Responses.length == 0) {
				if (responseBatch.Messages.length == 1 && responseBatch.Messages[0].Message == 'ok') return;
					handleDiscoveryErrors(repsonseBatch);
					return;
				}
				var discoverRecentGalleries = responseBatch.Responses[0].DiscoverContentAction;
*/
				var discoverRecentGalleries = responseBatch;
				var SetTitle = "<div class='RecentPhotosHeader'>Photos Tagged: "+QueryTags+"</div>";
				
				//This catch is written to show grayed out arrow button if there is nothing to scroll
				var CarNexDisabled = "";
				if (discoverRecentGalleries.DiscoveredContent.length<=4){
					CarNexDisabled = "disabled"
				}
				
				var Carousel = "<div class='CarouselNav'><img class='car prev disabled' src='http://media.suntimes.com/images/cds/preps/season/designelements/left_arrow_gray.gif'/><img class='car next "+CarNexDisabled+"' src='http://media.suntimes.com/images/cds/preps/season/designelements/right_arrow_gray.gif'/></div>";

				var PhotoHTML = "<ul class='RecentPhotosContainer'>";

				for (var i = 0; i < discoverRecentGalleries.DiscoveredContent.length; i++) {
					var info = discoverRecentGalleries.DiscoveredContent[i];
					keys[i] = info.GalleryKey.Key;
					PhotoHTML += "<li>";
						PhotoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/photo/index.html?plckPhotoID="+ info.PhotoKey.Key +"&plckGalleryID="+ info.GalleryKey.Key +"'><img src='"+ info.Image.Small +"' alt='"+ info.Title +"' /></a></div>";
						PhotoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/photo/index.html?plckPhotoID="+ info.PhotoKey.Key +"&plckGalleryID="+ info.GalleryKey.Key +"'>"+ info.Title +"</a></div>";
						PhotoHTML += "<div class='RecentMediaDescription'>"+ info.Description +"</div>";
						PhotoHTML += "<div class='RecentMediaBy'>by: "+ info.Author.DisplayName +" </div>";
						PhotoHTML += "<div class='RecentMediaCreated'>added: "+ info.CreatedOn +" </div>";
						PhotoHTML += "<div class='GallerySetTrail'></div>";
					PhotoHTML += "</li>";
				}
				PhotoHTML += "</ul>";
				
/* Trying New
				if ( $('#PhotoDiscovery').length ){
					$("#PhotoDiscovery").append(Carousel+"<div class='"+info.PhotoKey.Key+"'>"+PhotoHTML+"</div>");
					LoadJCarouselGallery("#PhotoDiscovery ."+info.PhotoKey.Key+"");
				}
*/
					$("#"+elementID+"").html(Carousel+SetTitle+"<div class='RecentMediaSet'>"+PhotoHTML+"</div>");
			}
			



			// === Photo Gallery With Image and Next/Previous ==========================================================
				var globalImageInSet = 1;
         function showPhotosInGalleryByID(key,RenderPage) {
					 	globalImageInSet = RenderPage;
            var galleryKey = new GalleryKey(key);
//            var photoPage = new PhotoPage(galleryKey, 10, 1);
            var photoPage = new PhotoPage(galleryKey, 1, RenderPage, new MediaType('Photo'));
            var requestBatch = new RequestBatch();
		        requestBatch.AddToRequest(photoPage);
		        requestBatch.AddToRequest(galleryKey);
            requestBatch.BeginRequest(serverUrl, showPhotosInGalCallback);
         }
         
         function showPhotosInGalCallback(responseBatch) {
							if (responseBatch.Responses.length == 0) {
								//alert('Gallery not found');
							}
							var gallery = responseBatch.Responses[0].PhotoPage;
							var GeneralGallery = responseBatch.Responses[1].Gallery;
							
								// Pagination
							var lastGalleryPage = Math.ceil(gallery.NumberOfPhotos/gallery.NumberPerPage);
							var Carousel = "<div class='CarouselNav'>";
								if (lastGalleryPage!= 1){
									if (1<globalImageInSet){
											var PreviousPage = globalImageInSet-1
											Carousel += "<img class='car' src='http://media.suntimes.com/images/cds/preps/season/designelements/left_arrow_gray.gif' onclick='showPhotosInGalleryByID(\""+ gallery.GalleryKey.Key +"\","+ PreviousPage +")'/>";
									} else {
											Carousel += "<img class='car disabled' src='http://media.suntimes.com/images/cds/preps/season/designelements/left_arrow_gray.gif'/>";
									}
									if (globalImageInSet<lastGalleryPage){
											var NextPage = globalImageInSet+1
											Carousel += "<img class='car' src='http://media.suntimes.com/images/cds/preps/season/designelements/right_arrow_gray.gif' onclick='showPhotosInGalleryByID(\""+ gallery.GalleryKey.Key +"\","+ NextPage +")'/>";
									} else {
											Carousel += "<img class='car disabled' src='http://media.suntimes.com/images/cds/preps/season/designelements/right_arrow_gray.gif'/>";
									}
							}
							Carousel += "</div>"; //end Pagination
							
							var SetTitle = "<div class='PhotoViewerTitle'><a href='/community/galleries/photogallery/index.html?plckGalleryID="+GeneralGallery.GalleryKey.Key+"'>"+GeneralGallery.Title+"</a></div>";

							var html = "";
							if (GeneralGallery.GalleryViewState=="Open" && userKey!=null && userKey!= ""){
								html += "<div class='AddPhotos'><a href='/community/upload/index.html?plckGalleryID="+gallery.GalleryKey.Key+"&UID="+userKey+"&GalTitle="+GeneralGallery.Title+"&video=false&TB_iframe=true&height=450&width=680&modal=true' class='thickbox' title='Add a photo to "+GeneralGallery.Title+"'>[+] Add your photo to this gallery</a></div>";
							}
	             for(var i=0; i < gallery.Photos.length; i++) {
  	              var photo = gallery.Photos[i];
    	            html += "<div class='GalleryImages'><a href='/community/galleries/photo/index.html?plckPhotoID="+photo.PhotoKey.Key+"&plckGalleryID="+gallery.GalleryKey.Key+"'><img src='" + photo.Image.Medium + "'></a></div>";
      	       }
							 							 
								$("#PhotoGalleryView").html(Carousel+SetTitle+html);
								tb_init('a.thickbox, area.thickbox, input.thickbox');						
         }




			// === Recent Video Galleries ==============================================================================
			function showRecentVideoGalleries(ItemLimit,QueryTags) {
				
				if (!ItemLimit) ItemLimit = 5;
				if (!QueryTags) QueryTags = "All";

				var searchSections = new Array();
					searchSections[0] = new Section("All");
				var searchCategories = new Array();
					searchCategories[0] = new Category(QueryTags);
				var limitToContributorsDisco = new Array();
					limitToContributorsDisco[0] = new UserTier("All");
				var activityDisco = new Activity("Recent");
				var contentType = new ContentType("PublicVideoGallery");
				var age = 15;
				var maximumNumberOfDiscoveries = ItemLimit;
			
				var requestBatch = new RequestBatch();
				var discoveryAction = new DiscoverContentAction(
							searchSections, 
							searchCategories, 
							limitToContributorsDisco, 
							activityDisco, 
							contentType, 
							age, 
							maximumNumberOfDiscoveries);
				requestBatch.AddToRequest(discoveryAction);
				requestBatch.BeginRequest(serverUrl, DiscoveryVideoGalleriesCallback);
				return;
			}

			function DiscoveryVideoGalleriesCallback(responseBatch){
				if (responseBatch.Responses.length == 0) {
				if (responseBatch.Messages.length == 1 && responseBatch.Messages[0].Message == 'ok') return;
					handleDiscoveryErrors(repsonseBatch);
					return;
				}
				var discoverRecentGalleries = responseBatch.Responses[0].DiscoverContentAction;
				
				var PhotoHTML = "<ul class='RecentVideosContainer'>";		
				for (var i = 0; i < discoverRecentGalleries.DiscoveredContent.length; i++) {
					var info = discoverRecentGalleries.DiscoveredContent[i];
					PhotoHTML += "<li>";
						PhotoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/videogallery/index.html?plckGalleryID=" +info.GalleryKey.Key+ "'><img src='" + info.GalleryPromo.Image.Medium + "'/></a></div>";
						PhotoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/videogallery/index.html?plckGalleryID=" +info.GalleryKey.Key+ "'>" + info.Title +"</a></div>";
						PhotoHTML += "<div class='RecentMediaDescription'>"+info.Description+"</div>";
						PhotoHTML += "<div class='RecentMediaCreated'> "+ info.CreatedOn +" </div>";
					PhotoHTML += "</li>";
				}
				PhotoHTML += "</ul>";
				$("#VideoGalleryDiscovery").html(PhotoHTML);
			}



			// === Show Video Gallery By ID ==============================================================================
			function showVideoGalleryByID(key) {
				var requestBatch = new RequestBatch();
			requestBatch.AddToRequest(new GalleryKey(key));
      requestBatch.BeginRequest(serverUrl, DiscoverySpecificVideoGalleryCallback);
		}

			function DiscoverySpecificVideoGalleryCallback(responseBatch){
        var gallery = responseBatch.Responses[0].Gallery;
				var PhotoHTML = "<ul>";
					PhotoHTML += "<li>";
						PhotoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/videogallery/index.html?plckGalleryID=" +gallery.GalleryKey.Key+ "'><img src='" + gallery.GalleryPromo.Image.Medium + "' alt='" + gallery.Title + "' /></a></div>";
						PhotoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/videogallery/index.html?plckGalleryID=" +gallery.GalleryKey.Key+ "'>" + gallery.Title +"</a></div>";
						PhotoHTML += "<div class='RecentMediaDescription'>" + gallery.Description +"</div>";
						PhotoHTML += "<div class='RecentMediaCreated'> "+ gallery.CreatedOn +" </div>";
					PhotoHTML += "</li>";
				PhotoHTML += "</ul>";
				$("#SpecificVideoGallery").html(PhotoHTML);
			}


			// === Recent Videos by Tag ==============================================================================
			function showRecentVideos(ItemLimit,QueryTags) {
				if (!ItemLimit) ItemLimit = 5;
				if (!QueryTags) QueryTags = "All";

				var searchSections = new Array();
					searchSections[0] = new Section("All");
				var searchCategories = new Array();
					var searchTag=QueryTags.split(",");
						searchCategories[0] = new Category(searchTag[0]);
						searchCategories[1] = new Category(searchTag[1]);
						searchCategories[2] = new Category(searchTag[2]);
						searchCategories[3] = new Category(searchTag[3]);
						searchCategories[4] = new Category(searchTag[4]);
				var limitToContributorsDisco = new Array();
					limitToContributorsDisco[0] = new UserTier("All");
				var activityDisco = new Activity("Recent");
				var contentType = new ContentType("PublicVideo");
				var age = 15;
				var maximumNumberOfDiscoveries = ItemLimit;
			
				var requestBatch = new RequestBatch();
				var discoveryAction = new DiscoverContentAction(
							searchSections, 
							searchCategories,
							limitToContributorsDisco, 
							activityDisco, 
							contentType, 
							age, 
							maximumNumberOfDiscoveries);
				requestBatch.AddToRequest(discoveryAction);
				requestBatch.BeginRequest(serverUrl, DiscoveryVideoCallback);
			}
    
			function DiscoveryVideoCallback(responseBatch){
				if (responseBatch.Responses.length == 0) {
				if (responseBatch.Messages.length == 1 && responseBatch.Messages[0].Message == 'ok') return;
					handleDiscoveryErrors(repsonseBatch);
					return;
				}
				var discoverRecentGalleries = responseBatch.Responses[0].DiscoverContentAction;
				var VideoHTML = "<ul class='RecentVideosContainer'>";
				for (var i = 0; i < discoverRecentGalleries.DiscoveredContent.length; i++) {
					var video = discoverRecentGalleries.DiscoveredContent[i];
					keys[i] = video.GalleryKey.Key;
					VideoHTML += "<li>";
						VideoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/videogallery/index.html?videoKey=" +video.VideoKey.Key+ "&plckGalleryID=" +video.GalleryKey.Key+ "'><img src='" + video.VideoThumbnail + "' alt='" + video.Title + "' /></a></div>";
						VideoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/videogallery/index.html?videoKey=" +video.VideoKey.Key+ "&plckGalleryID=" +video.GalleryKey.Key+ "'>" + video.Title +"</a></div>";
						VideoHTML += "<div class='RecentMediaBy'>by "+ video.Author.DisplayName +" </div>";
						VideoHTML += "<div class='RecentMediaCreated'> "+ video.CreatedOn +" </div>";
					VideoHTML += "</li>";
				}
				VideoHTML += "</ul>";
				$("#VideoDiscovery").html(VideoHTML);
			}


			// === Recent Videos by Tag - In Player ==============================================================================
			var BigPlayerHeight;
			var BigPlayerWidth;
			function playRecentVideos(Height,Width,QueryTags) {
				if (!Height) Height = 375;
				BigPlayerHeight = Height;
				if (!Width) Width = 500;
				BigPlayerWidth = Width;
				if (!QueryTags) QueryTags = "All";

				var searchSections = new Array();
					searchSections[0] = new Section("All");
				var searchCategories = new Array();
					var searchTag=QueryTags.split(",");
						searchCategories[0] = new Category(searchTag[0]);
						searchCategories[1] = new Category(searchTag[1]);
						searchCategories[2] = new Category(searchTag[2]);
						searchCategories[3] = new Category(searchTag[3]);
						searchCategories[4] = new Category(searchTag[4]);
				var limitToContributorsDisco = new Array();
					limitToContributorsDisco[0] = new UserTier("All");
				var activityDisco = new Activity("Recent");
				var contentType = new ContentType("PublicVideo");
				var age = 15;
				var maximumNumberOfDiscoveries = 1;
			
				var requestBatch = new RequestBatch();
				var discoveryAction = new DiscoverContentAction(
							searchSections, 
							searchCategories,
							limitToContributorsDisco, 
							activityDisco, 
							contentType, 
							age, 
							maximumNumberOfDiscoveries);
				requestBatch.AddToRequest(discoveryAction);
				requestBatch.BeginRequest(serverUrl, DiscoveryVideoPlayback);
			}
    
			function DiscoveryVideoPlayback(responseBatch){
				if (responseBatch.Responses.length == 0) {
				if (responseBatch.Messages.length == 1 && responseBatch.Messages[0].Message == 'ok') return;
					handleDiscoveryErrors(repsonseBatch);
					return;
				}
				var discoverRecentGalleries = responseBatch.Responses[0].DiscoverContentAction;
				var VideoHTML = "";
				for (var i = 0; i < discoverRecentGalleries.DiscoveredContent.length; i++) {
					var video = discoverRecentGalleries.DiscoveredContent[i];
					keys[i] = video.GalleryKey.Key;
//						VideoHTML += "<div class='RecentMediaThumb'><a href='/community/galleries/videogallery/index.html?videoKey=" +video.VideoKey.Key+ "&plckGalleryID=" +video.GalleryKey.Key+ "'><img src='" + video.VideoThumbnail + "' alt='" + video.Title + "' /></a></div>";
//						VideoHTML += "<div class='RecentMediaLink'><a href='/community/galleries/videogallery/index.html?videoKey=" +video.VideoKey.Key+ "&plckGalleryID=" +video.GalleryKey.Key+ "'>" + video.Title +"</a></div>";
					VideoHTML += "<div class='RecentMediaBigTitle'>"+video.Title+"</div>";
					VideoHTML += "<div class='VideoPlayer'><embed type=\"application/x-shockwave-flash\" src=\"http://sitelife.suntimes.com/ver1.0/content/swf/mediaplayer.swf\" style=\"\" id=\"video\" name=\"video\" bgcolor=\"#000000\" quality=\"high\" flashvars=\"file="+video.VideoUrl+"&amp;autostart=false&amp;width="+BigPlayerWidth+"&amp;height="+BigPlayerHeight+"&amp;allowfullscreen=true&amp;allowscriptaccess=always\" wmode=\"transparent\" height=\""+BigPlayerHeight+"\" width=\""+BigPlayerWidth+"\"></div>";
					VideoHTML += "<div class='RecentMediaDetails'>";
						VideoHTML += "<div class='RecentMediaBy'>by "+video.Author.DisplayName+"</div>";
						VideoHTML += "<div class='RecentMediaTitle'>Title: "+video.Title+"</div>";
						VideoHTML += "<div class='RecentMediaDescription'>Description: "+video.Description+"</div>";
						VideoHTML += "<div class='RecentMediaCreated'>"+ video.CreatedOn +"</div>";
					VideoHTML += "</div>";
					
				}
				$("#VideoDiscoveryPlayback").html(VideoHTML);
			}



			// === Load JQuery Carousels ==============================================================================
				function LoadJQCarousel4 (DivElement){
					$("#"+DivElement+"").jCarouselLite({
						btnNext: "#"+DivElement+" .next",
						btnPrev: "#"+DivElement+" .prev",
						circular: false,
						visible: 4
					});
				}
