$(document).ready(function(){

	
	//  Begin next/prev rollovers
	$("#nextImgBtn").hover(
      function () {
		$(this).css ({ border:"1px solid #d1e1e6" });
        $("#nextImg").css({ display:"block" });
      }, 
      function () {
	   $(this).css ({ border:"1px solid #FFFFFF" });
       $("#nextImg").css({ display:"none" });
      }
    );
	
	
	$("#prevImgBtn").hover(
      function () {
		$(this).css ({ border:"1px solid #d1e1e6" });
        $("#prevImg").css({ display:"block" });
      }, 
      function () {
	   $(this).css ({ border:"1px solid #FFFFFF" });
       $("#prevImg").css({ display:"none" });
      }
    );
	// End next/prev rollovers

	//$('form.rating').rating();

	if (typeof loadMedia == "function") {
		loadMedia();
	}

  });


var photoFound=false;

var galleryHtml= new Array();

galleryHtml[0]='';
galleryHtml[1]='';
galleryHtml[2]='';

var photoId='';
var lastPhotoKey='';
var lastPhotoTitle='';
var lastPhotoThumb='';
var lastPhotoReversePage='';
var prevPagePhotoKey='';
var prevPageReversePage='';

var photoContentId='';
var prevPhotoKey='';
var prevPhotoLink='';
var prevPhotoContentId='';
var prevPhotoTitle='';
var prevPhotoReversePage='';
var nextPhotoKey='';
var nextPhotoLink='';
var nextPhotoContentId='';
var nextPhotoTitle='';
var nextPhotoReversePage='';
var bookmarkUrl='';

var pageSize=8;
var fullPageSize=16;

function updatePhoto(photo) {

	if ((photoId!='' && photo.PhotoKey.Key==photoId) || photoId=='') {

		resetRatingDivs();

		if (document.getElementById('photoTitle')!=null) {
			document.getElementById('photoTitle').innerHTML=photo.Title;
		}

		if (document.getElementById('photoCredit')!=null) {
			document.getElementById('photoCredit').innerHTML='By ' + photo.Author.DisplayName;
		}

		if (document.getElementById('photoImage')!=null) {
			document.getElementById('photoImage').src=photo.Image.Large;
			document.getElementById('photoImage').alt=photo.Title;
		}

		if (document.getElementById('photoDesc')!=null) {
			document.getElementById('photoDesc').innerHTML=photo.Description;
		}
		
		retrieveCreatePhotoContent(photo);
		
		s.tl(this,'o','photoload'+photo.PhotoKey.Key);
	}	
}

function retrieveCreatePhotoContent(photo) {

    var myDate=new Date();

    var url = '/content/addPhotoContentCall.do?galleryContentId='+galleryContentId+'&photoId='+photo.PhotoKey.Key+'&photoTitle='+escape(photo.Title) + '&photoDesc='+escape(photo.Description)+'&date='+photo.CreatedOn+'&byLine='+escape(photo.Author.DisplayName)+'&image='+photo.Image.Large + '&reversePage='+rpage+'&dt='+escape(myDate);
    
    $.getJSON(url,
        function(data){
            
            	var returnUrl=contentUrl;
		
		if (photoId!='') {
			returnUrl=returnUrl + '?photoId='+photoId
		}

		if (bookmarkUrl!='') {
			returnUrl=bookmarkUrl;
		}

		photoContentId=data.contentData.contentId;

		$("#moduleComments_long").load('/content/commentsCall.do?commentContentId='+photoContentId);

		populateRating(photoContentId,returnUrl,data.contentData.averageRating,data.contentData.userRating, data.contentData.votes);
        });
    
}

function updateRelatedContent() {

	var prev='';
	var next='';

	if (prevPhotoKey!='') {
		prev=contentUrl+'?photoId='+prevPhotoKey + '&rp='+prevPhotoReversePage;
	}
	if (nextPhotoKey!='') {
		next=contentUrl+'?photoId='+nextPhotoKey + '&rp='+nextPhotoReversePage;
	}

	if (prev!='' && prevPhotoLink!=prev) {
		insertPrevContent(prev)

	}
	else if (next!='' && nextPhotoLink!=next) {
		insertNextContent(next)
	}

}

function insertPrevContent(prev) {

	var url = '/content/insertLinkContentCall.do?link='+escape(prev)+'&title='+escape(prevPhotoTitle);
	
	$.getJSON(url,
	        function(data){
	            
			prevPhotoContentId=data.linkData.contentId;

			var next='';
			if (nextPhotoKey!='') {
				next=contentUrl+'?photoId='+nextPhotoKey;
			}

			if (next!='' && nextPhotoLink!=next) {
				insertNextContent(next);
			}
			else {
				updatePhotoRelatedContent();
			}
        	});
}

function insertNextContent(next) {

	var url = '/content/insertLinkContentCall.do?link='+escape(next)+'&title='+escape(nextPhotoTitle);
	
	$.getJSON(url,
		function(data){
			nextPhotoContentId=data.linkData.contentId;
			updatePhotoRelatedContent();        	
        	});
}

function updatePhotoRelatedContent() {
	
	if (photoContentId!='') {
	
		var url = '/content/addLinksToContentCall.do?mainContentId='+photoContentId;
		
		var linkIndex=1;
		
		if (prevPhotoContentId!='') {
		
			url=url+'&relatedContentId'+linkIndex+'='+prevPhotoContentId + '&relationType'+linkIndex+'=31';
			linkIndex++;
		}
		
		if (nextPhotoContentId!='') {
			url=url+'&relatedContentId'+linkIndex+'='+nextPhotoContentId + '&relationType'+linkIndex+'=30';
		
		}
		
		$.get(url);
	}
	
}

function renderPhoto(responseBatch) {   

	if (responseBatch.Responses.length > 0) {        	
		updatePhoto(responseBatch.Responses[0].Photo); 
	}
}

function renderGallerySize(responseBatch) { 
	
	var startPage=1;
	
	if (responseBatch.Responses.length > 0) {   
	
		var photoPage = responseBatch.Responses[0].PhotoPage;
		
		var maxPhotos=photoPage.NumberOfPhotos;
		
		var forwardIndex=maxPhotos-(rpage*fullPageSize+maxPhotos%fullPageSize);
		
		startPage=parseInt(forwardIndex/pageSize)-1;
		
		if (startPage<1) {
			startPage=1;
		}
	}
	
	var requestBatch = new RequestBatch();   
	var galleryKey = new GalleryKey(galleryId);   
	var photoPage = new PhotoPage(galleryKey, pageSize, startPage);   
	var requestBatch = new RequestBatch();   
	requestBatch.AddToRequest(photoPage);   
	requestBatch.BeginRequest(serverUrl, renderGallery1);

}

function renderGallery1(responseBatch) { 

	galleryHtml[0]='';
	
	if (prevPagePhotoKey!='') {
	
		galleryHtml[0]='<li id="prevSet"><a href="'+contentUrl+'?photoId='+prevPagePhotoKey+'&rp='+prevPageReversePage+'"><img src="/images/placer/photo/txtPreviousSet.png" width="160" height="57" alt="Previous Set of Images" border="0" /></a></li>';
	}
	else {
	
		galleryHtml[0]='<li id="prevSet"><img src="/images/spacer.gif" width="160" height="57" alt="" border="0" /></li>';
	}
	
	renderGallery(0,0,pageSize,responseBatch,true);

}

function renderGallery2(responseBatch) { 

	galleryHtml[1]='';

	renderGallery(1,0,pageSize,responseBatch,true);

	if (photoFound) {

		if (document.getElementById('firstRow')!=null) {
			document.getElementById('firstRow').innerHTML=galleryHtml[0];
		}
		if (document.getElementById('secondRow')!=null) {
			document.getElementById('secondRow').innerHTML=galleryHtml[1] + '<li id="nextSet"><img src="/images/spacer.gif" width="160" height="57" alt="" border="0" /></li>';	
		}
	}

}

function renderGalleryNextPage(responseBatch) { 

	updateRelatedContent();

	galleryHtml[2]='';
	renderGallery(2,0,1,responseBatch,false);
}

function renderGalleryStartImage(responseBatch) { 
		
	if (responseBatch.Responses.length > 0) {   
		
		var photoPage = responseBatch.Responses[0].PhotoPage;
	
		if (photoPage.Photos.length >0) {
		
			if (document.getElementById('galleryStartImage' + photoPage.GalleryKey.Key)!=null) {
				document.getElementById('galleryStartImage' + photoPage.GalleryKey.Key).src=photoPage.Photos[0].Image.Large;
			}
		}
	}
}

function updatePrevPhoto() {

	prevPhotoKey=lastPhotoKey;
	prevPhotoTitle=lastPhotoTitle;
	prevPhotoReversePage=lastPhotoReversePage;
	
	if (prevPhotoKey!='' && document.getElementById('prevImgBtn')!=null) {
		document.getElementById('prevImgBtn').innerHTML='<a href="'+contentUrl+'?photoId='+prevPhotoKey+'&rp='+prevPhotoReversePage+'"><img src="/images/btn-previous-off.gif" onmouseover="this.src=\'/images/btn-previous-on.gif\'" onmouseout="this.src=\'/images/btn-previous-off.gif\'" width="88" height="39" alt="Previous" border="0" /></a><br /><img src="'+lastPhotoThumb+'" width="76" height="51" border="0" alt=" Previous Image" id="prevImg" />';
	}
	else if (document.getElementById('prevImgBtn')!=null) {
		document.getElementById('prevImgBtn').style.display='none';
	}
}

function updateNextPhoto(nextPhoto,reversePage) {

	nextPhotoKey=nextPhoto.PhotoKey.Key;
	nextPhotoTitle=nextPhoto.Title;
	nextPhotoReversePage=reversePage;

	if (document.getElementById('nextImgBtn')!=null) {
		document.getElementById('nextImgBtn').innerHTML='<a href="'+contentUrl+'?photoId='+nextPhotoKey+'&rp='+nextPhotoReversePage+'"><img src="/images/btn-next-off.gif" onmouseover="this.src=\'/images/btn-next-on.gif\'" onmouseout="this.src=\'/images/btn-next-off.gif\'" width="88" height="39" alt="Next" border="0" /></a><br /><img src="'+nextPhoto.Image.RecentSlide+'" width="76" height="51" border="0" alt="Next Image" id="nextImg" />';
	}
}

function renderGallery(galleryIndex,startIndex,endIndex,responseBatch,getNextPage) {   

	if (responseBatch.Responses.length > 0) {   

		var photoPage = responseBatch.Responses[0].PhotoPage;

		if (photoPage.Photos.length >0) {

			var photoEnd=endIndex;

			if (photoPage.Photos.length<photoEnd) {
				photoEnd=photoPage.Photos.length;
			}

			for (var i=startIndex; i < photoEnd; i++) {

				var rp=getReversePageNum(i,photoPage.OnPage,photoPage.NumberOfPhotos);

				var selectedClass='';

				if (galleryIndex==2 && document.getElementById('nextSet')!=null) {

					document.getElementById('nextSet').innerHTML='<a href="'+contentUrl+'?photoId='+photoPage.Photos[i].PhotoKey.Key+'&rp='+rp+'"><img src="/images/placer/photo/txtNextSetOfImages.png" width="160" height="57" alt="Next Set of Images" border="0" /></a>';
				}


				if (photoPage.Photos[i].PhotoKey.Key==photoId || photoId=='') {

					rpage=rp;
					
					if (photoIdParm=='' || defaultPhotoId=='') {
						document.title=photoPage.Photos[i].Title + ' - ' + document.title;
					}

					bookmarkUrl=contentDomain+contentUrl + '?photoId='+photoPage.Photos[i].PhotoKey.Key+'&rp='+rp;
			
					if (photoId=='') {
						photoId=photoPage.Photos[i].PhotoKey.Key;

						if (photoId!=defaultPhotoId) {
							updatePhoto(photoPage.Photos[i]);
						}
					}

					updatePrevPhoto();

					selectedClass=' class="ib"';

					if (document.getElementById('photoNumber')!=null) {
						document.getElementById('photoNumber').innerHTML='Image ' + (((parseInt(photoPage.OnPage)-1)*pageSize) + (i+1)) + ' of ' + photoPage.NumberOfPhotos;
					}

					photoFound=true;
				}
				else if (photoFound && nextPhotoKey=='') {

					updateNextPhoto(photoPage.Photos[i],rp);

				}

				galleryHtml[galleryIndex]=galleryHtml[galleryIndex] + '<li><a href="'+contentUrl+'?photoId='+photoPage.Photos[i].PhotoKey.Key+'&rp='+rp+'"'+selectedClass+'><img src="' + photoPage.Photos[i].Image.RecentSlide + '" alt="'+photoPage.Photos[i].Title+'" width="76" height="51" border="0"></a></li>';

				lastPhotoKey=photoPage.Photos[i].PhotoKey.Key;
				lastPhotoTitle=photoPage.Photos[i].Title;
				lastPhotoThumb=photoPage.Photos[i].Image.RecentSlide;
				lastPhotoReversePage=rp;
			}

			if (galleryIndex==0 && getNextPage) {

				var requestBatch = new RequestBatch();   
				var galleryKey = new GalleryKey(galleryId);   
				var nextPhotoPage = new PhotoPage(galleryKey, pageSize, parseInt(photoPage.OnPage)+1);   
				var requestBatch = new RequestBatch();   
				requestBatch.AddToRequest(nextPhotoPage);   
				requestBatch.BeginRequest(serverUrl, renderGallery2);
			}

			if (galleryIndex==1) {

				var requestBatch = new RequestBatch();   
				var galleryKey = new GalleryKey(galleryId);   
				var nextPhotoPage = new PhotoPage(galleryKey, pageSize, parseInt(photoPage.OnPage)+1);   
				var requestBatch = new RequestBatch();   
				requestBatch.AddToRequest(nextPhotoPage);

				if (photoFound) {
					requestBatch.BeginRequest(serverUrl, renderGalleryNextPage);
				}
				else {
					prevPagePhotoKey=lastPhotoKey;
					prevPageReversePage=lastPhotoReversePage;
					requestBatch.BeginRequest(serverUrl, renderGallery1);
				}
			}
		}
	}
}

function getReversePageNum(photoIndex,pageNum,totalPhotos) {

	var reversePhotoIndex=totalPhotos-(((parseInt(pageNum)-1)*pageSize) + photoIndex);

	var reversePage=parseInt(reversePhotoIndex/fullPageSize);
	
	if (reversePhotoIndex%fullPageSize>0) {
		reversePage++;
	}
	return reversePage;
}


// podcasts

function retrievePodcastContent(contentId) {

    var myDate=new Date();

    var url = '/content/addContentDataCall.do?dataContentId='+contentId + '&dt='+escape(myDate);
    
    $.getJSON(url,
	function(data){

		var returnUrl=contentUrl;

		if (document.getElementById("contentViews")!= null) {
			document.getElementById("contentViews").innerHTML='Viewed '+data.contentData.views+' times';
		}
		
		populateRating(data.contentData.contentId,returnUrl,data.contentData.averageRating,data.contentData.userRating, data.contentData.votes);
	});
}



//  Begin slideshow pagination rollovers

function displayPreview(photoId) {
	document.getElementById(photoId + "Img").style.display = 'block';
}

function removePreview(photoId) {
	document.getElementById(photoId + "Img").style.display = 'none';
}

//  End slideshow pagination rollovers


