/**
* Assign the view handler
*/

viewHandler = Celebrity;

dojo.require("eta.image.Lightbox");


/**
* Creates a new object with methods used by the Celebrity page
*
* @author				Matt Gifford
* @copyright			2008 Timeshifting Interactive Limited
*/
function Celebrity()
	{
	// Step 1. Define Properties

	var _instance = this;

	this.votingEnabled = true;
	this.lightboxDialog = null;
	this.photosDatasetLoaded = false;


	// Step 2. Define Public Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		// Call generic page init method
		this.base.init.call(this);

		// Initialize the dojox lightbox
		this.lightboxDialog = new eta.image.LightboxDialog({});
		this.lightboxDialog.startup();

		// Add event handler to clear tooltips when clicking on a lightbox
		dojo.connect(document.getElementById('celebrityMedia'), 'click',
			function()
				{
				if (document.getElementById('dijit__MasterTooltip_0'))
					{
					setTimeout("document.getElementById('dijit__MasterTooltip_0').style.top = '-10000px';", 500);
					}
				}
			);

		// Load initial content
		var author = document.getElementById('celebrityLatest').attributes.ref.value;
		this.loadBlogPage(author);
		this.loadPhotosPage(1);
		this.loadVideosPage(1);
		}

	
	/**
	* Creates lightboxes for photos items (deleting any previously made lightboxes)
	*/
	this.initLightBoxes = function()
		{
		// Build the new lightbox group
		var groupName = 'lightboxGroupETA';
		var anchors = document.getElementById('photoThumbnails').getElementsByTagName('a');
		for (var x = 0; x < anchors.length; x++)
			{
			// Add image to lightbox
			if (this.photosDatasetLoaded == false)
				{
				this.lightboxDialog.addImage({ title: anchors[x].getAttribute('title'), href: anchors[x].href, rel: anchors[x].getAttribute('rel') }, groupName);
				}
			
			// Add the event handler to the anchor
			anchors[x].onclick = new Function("xhtml.lightboxDialog.show({ group: '" + groupName + "', href: '" + anchors[x].href + "', rel: '" + anchors[x].getAttribute('rel') + "' });");
			anchors[x].href = 'javascript:;'
			}

		// Set that the initial dataset has been loaded
		this.photosDatasetLoaded = true;
		}


	/**
	* Changes the blog page displayed
	*
	* @param		id						The id of the celebrity page
	* @param		page					The page of photos to load
	*/
	this.loadBlogPage = function(page)
		{
		var id = document.getElementById('celebrityLatest').attributes.ref.value;
		// Update the content
		dojo.xhrGet(
			{
			url: '/celebrity/blog/id/' + id + '/page/' + page,
			preventCache: true,
			handleAs: "text",
				load: function(data)
					{
					// Add the new playlist in to the document
					document.getElementById('celebrityLatest').innerHTML = data;
					xhtml.initTooltips();
					}
				}
			);
		}
	

	/**
	* Sends the vote and disables the vote box
	*
	* @param		obj		The anchor clicked on
	* @param		id			The name of the person to vote for
	*/
	this.vote = function(obj, id)
		{
		// Update and disable the vote box
		obj.parentNode.className += ' voteBoxYes';

		// Send the ajax request
		dojo.xhrGet(
				{
				url: '/index/vote/id/' + id,
				preventCache: true,
				handleAs: "text",
				load: function(data)
					{
					// Display message
					alert(data);
					},
				error: function(error)
					{
					// Display message
					alert(error);
					}
				}
			);
		}


	/**
	* Changes the page of photos displayed
	*
	* @param		page					The page of photos to load
	*/
	this.loadPhotosPage = function(page)
		{
		var id = document.getElementById('celebrityMediaPhotos').attributes.ref.value;
		// Update the content
		dojo.xhrGet(
			{
			url: '/celebrity/imagelist/id/' + id + '/page/' + page,
			preventCache: true,
			handleAs: "text",
				load: function(data)
					{
					// Add the new playlist in to the document
					document.getElementById('celebrityMediaPhotos').innerHTML = data;
					xhtml.initTooltips();
					xhtml.initLightBoxes();
					}
				}
			);
		}


	/**
	* Changes the page of videos displayed
	*
	* @param		page					The page of photos to load
	*/
	this.loadVideosPage = function(page)
		{
		var id = document.getElementById('celebrityMediaVideos').attributes.ref.value;
		// Update the content
		dojo.xhrGet(
			{
			url: '/celebrity/videolist/id/' + id + '/page/' + page,
			preventCache: true,
			handleAs: "text",
				load: function(data)
					{
					// Add the new playlist in to the document
					document.getElementById('celebrityMediaVideos').innerHTML = data;
					xhtml.initTooltips();
					}
				}
			);
		}
	}
