//##########################################################################################

//place our event hook
window.onload = function() {
	//setup main array
	Page.CurrentPhoto = -1;
	Page.Photos = new Array();
	
	//find all images and setup array
	var Containers = PageTemplate.GetNodesByAttribute("data-label","photo").GetNodes();
	var Photos = PageTemplate.GetNodesByAttribute("data-label","thumbnail").GetNodes();
	var MainPhoto = PageTemplate.GetNodesByAttribute("data-label","full").GetNode();
	var PhotoDescriptions = PageTemplate.GetNodesByAttribute("data-label","description").GetNodes();
	for(var i = 0 ; i < Containers.length ; i++) {
		//get the full src, raw src and description
		var tmpSrcFull = Photos[i].getAttribute("data-src-full");
		var tmpSrcRaw = Photos[i].getAttribute("data-src-raw");
		var tmpDescription = PhotoDescriptions[i].innerHTML;
		
		//set current photo
		if(MainPhoto.getAttribute("src").indexOf(tmpSrcFull) != -1) {
			Page.CurrentPhoto = i;
		}
		
		//setup onclick
		Containers[i].onclick = new Function("Page.SetPhoto("+ i +");");
		
		//add to array
		Page.Photos.push(new Array(tmpSrcFull, tmpSrcRaw, tmpDescription, Containers[i]));
	}
	
	//setup previous next buttons
	var NextButton = PageTemplate.GetNodesByAttribute("data-field","next_photo_button").GetNode();
	var PrevButton = PageTemplate.GetNodesByAttribute("data-field","prev_photo_button").GetNode();
	if(NextButton) {
		NextButton.onclick = function(Event) {
			Page.NextPhoto();
		};
	}
	if(PrevButton) {
		PrevButton.onclick = function(Event) {
			Page.PreviousPhoto();
		};
	}
	
	//find the remove link
	var RemoveButton = PageTemplate.GetNodesByAttribute("data-field","remove_button").GetNode();
	if(RemoveButton){
		RemoveButton.onclick = function(Event){
			if(confirm("Are you sure?")){
				//get the event
				Event = BrowserWindow.GetEvent(Event);
				
				//get the comment id from the link
				var comment_id = Event.Target.getAttribute("data-id");
				
				//put the comment id in the hidden field
				PageTemplate.GetNodesByAttribute("data-field", "comment_id").SetAttribute("value", comment_id);
			}
		};
	}
};

//##########################################################################################

//--> Setup Object :: Page
	Page = {};
//<-- End Setup Object :: Page

//##########################################################################################

//--> Begin Function :: PreviousPhoto
	Page.PreviousPhoto = function() {
		//remove highlight from current
		Page.Photos[Page.CurrentPhoto][3].className = "photo";
		
		//change current
		if(Page.CurrentPhoto == 0) {
			Page.CurrentPhoto = (Page.Photos.length - 1);
		}
		else {
			Page.CurrentPhoto -= 1;
		}
		
		//set photo
		Page.SetPhoto(Page.CurrentPhoto);
	}
//<-- End Function :: PreviousPhoto

//##########################################################################################

//--> Begin Function :: NextPhoto
	Page.NextPhoto = function() {
		//remove highlight from current
		Page.Photos[Page.CurrentPhoto][3].className = "photo";
		
		//change current
		if(Page.CurrentPhoto == (Page.Photos.length - 1)) {
			Page.CurrentPhoto = 0;
		}
		else {
			Page.CurrentPhoto += 1;
		}
		
		//set photo
		Page.SetPhoto(Page.CurrentPhoto);
	}
//<-- End Function :: NextPhoto

//##########################################################################################

//--> Begin Function :: SetPhoto
	Page.SetPhoto = function(Index) {
		//remove highlight from current
		Page.Photos[Page.CurrentPhoto][3].className = "photo";
		
		//change current
		Page.CurrentPhoto = Index;
		
		//highlight new current
		Page.Photos[Page.CurrentPhoto][3].className = "photo current_photo";
		PageTemplate.GetNodesByAttribute("data-label","current_photo").SetInnerHTML(Page.CurrentPhoto + 1);
		
		//show photo
		PageTemplate.GetNodesByAttribute("data-label","full").SetAttribute("src", Page.Photos[Page.CurrentPhoto][0]);
		PageTemplate.GetNodesByAttribute("data-label","raw_photo_link").SetAttribute("href", Page.Photos[Page.CurrentPhoto][1]);
		PageTemplate.GetNodesByAttribute("data-label","main_description").SetInnerHTML(Page.Photos[Page.CurrentPhoto][2]);
	}
//<-- End Function :: SetPhoto

//##########################################################################################