var EmaModulePicturesPublicClass = Class.create();

EmaModulePicturesPublicClass.prototype =
{
	ClassName: null,
	ElementHash: null,
	DefaultHash: null,
	ElementToUpdate: null,
	ElementNumber: null,
	Poller: null,
	VotingInProgress: false,
	CommentInProgress: false,
	ResizeTimer: null,

	initialize: function(ClassName, ElementToUpdate)
	{
		this.ClassName = ClassName;
		this.ElementToUpdate = $(ElementToUpdate);
		
		/**
		* This makes sure that this.Classname is callable everywhere,
		* even when element is added via prototype's Element.update()
		*/
		eval(this.ClassName + " = this;");

		if (typeof ElementToUpdate != 'undefined')
		{
			this.ElementNumber = ElementToUpdate.match(/([0-9]*)_Content/i)[1];
			//setTimeout(function(){this.StartPolling();}.bind(this), 500);
		}
	},

	ShowAlbumsView: function()
	{
		this.UpdateHash('/modpic/albums/');
		
		sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
        CallPhpFunction('GetAlbumsViewView', this.ElementNumber, 
        	function()
        	{
				this.ElementToUpdate.update(arguments[0]);
				this.StartPolling();
			}.bind(this)
		)
	},

	ShowAlbum: function(Album, AlbumPage, IsElementDefault)
    {
    	IsElementDefault = (typeof(IsElementDefault) == 'undefined' ? false : IsElementDefault);
    	
		this.UpdateHash('/modpic/album/' + Album + '/' + AlbumPage + '/');

		sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
        CallPhpFunction('GetAlbumView', this.ElementNumber, Album, AlbumPage, IsElementDefault ? 1:0,
        	function(){
        		this.ElementToUpdate.update(arguments[0]);
				this.StartPolling();
			}.bind(this)
		);
    },

    ShowPicture: function(PictureId)
    {
		this.UpdateHash('/modpic/image/' + PictureId + '/');

		sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
        CallPhpFunction('GetPictureView', this.ElementNumber, PictureId, this.ElementToUpdate.getWidth(),
        	function()
        	{
		        this.ElementToUpdate.update(arguments[0]);
				this.StartPolling();
			}.bind(this)
		);
    },

    ShowPictureTags: function(Tag)
    {
    	this.UpdateHash('/modpic/tags/' + Tag + '/');
		
		sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
        CallPhpFunction('GetPictureTagsView', this.ElementNumber, Tag, 
        	function(){
        		this.ElementToUpdate.update(arguments[0]);
				this.StartPolling();
			}.bind(this)
		);
    },

    ShowRandomPicture: function(AlbumId)
    {
		this.UpdateHash('/modpic/random/');
		
		sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
        CallPhpFunction('GetRandomPictureView', this.ElementNumber, AlbumId,
        	function(){
        		this.ElementToUpdate.update(arguments[0]);
				this.StartPolling();
			}.bind(this)
		);
    },

    /**
    * Submits vote form when user clicks on a star
    */
    Vote: function(PictureId, Index)
    {
    	if (this.VotingInProgress == false)
    	{
    		this.StopPolling();
    		this.VotingInProgress = true;

    		this.UpdateHash(this.ElementHash + 'voted');

			sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
			CallPhpFunction(
				'VoteForPicture',
				this.ElementNumber,
				PictureId,
				Index,
				function()
				{
					this.VotingInProgress = false;
					this.StartPolling();
				}.bind(this)
			);
		}
	},

	Comment: function(PictureId)
	{
		var Comment = $('PicComment' + PictureId).value;
		var Name = $('PicComment' + PictureId).value;

		if (Comment.length < 2 || Name.length == 0)
		{
			alert(Lang.EmaModulePicturesCommentError);
			return false;
		}

		if (this.CommentInProgress == false)
		{
			this.StopPolling();
			this.CommentInProgress = true;

			this.UpdateHash(this.ElementHash + 'commented');

			sajax_remote_uri = '/modules/pictures/pictures_ajax.php';

			CallPhpFunction(
				'CommentPicture',
				this.ElementNumber,
				PictureId,
				$('PicComment' + PictureId).value /* Result of replace */,
				$('PicCommentName' + PictureId).value /* Result of replace */,
				$('PicCommentEmail' + PictureId).value /* Result of replace */,
				function()
				{
					this.CommentInProgress = false;
					this.StartPolling();
				}.bind(this)
			);
		}
	},
	
	DeleteComment: function(commentId)
	{
		if (confirm(Lang.EmaModulePicturesConfirmDeleteComment))
		{
			sajax_remote_uri = '/modules/pictures/pictures_ajax.php';
			CallPhpFunction('DeleteComment', commentId,
				function(result)
				{
					if ($('picturesSingleComment_' + commentId))
						$('picturesSingleComment_' + commentId).remove();
				}.bind(this)
			);
		}
	},

    StartPolling: function()
    {
    	if (this.Poller != null)
    		this.StopPolling();

		this.Poller = setInterval(function()
		{
			this.PollHash();
		}.bind(this), 100);
    },

    StopPolling: function()
    {
		clearInterval(this.Poller);
		this.Poller = null;
    },

    UpdateHash: function(newHash)
    {
    	this.ElementHash = newHash;

		if (this.DefaultHash == null || this.DefaultHash == '')
		{
			this.DefaultHash = '?' + this.ElementNumber + '=' + newHash;
		}
    },

	
    PollHash: function()
    {
    	if (window.location.hash.length > 2)
			var hash = window.location.hash;
		else
			var hash = this.DefaultHash;

		var match = hash.match(new RegExp('\\?' + this.ElementNumber + '=([^\\?]*)\\??'));

		var currentElementHash = '';

		if (match != null && typeof match[1] != 'undefined' && match[1] != null)
			currentElementHash = match[1];

		if (currentElementHash != '' && currentElementHash != this.ElementHash)
		{
			this.StopPolling();

			var strRegExp = '/modpic/(album|albums|image|tags)/([^\\?/]*)?/?(([0-9]*)/?)?';

			var re = new RegExp(strRegExp);
			var res = currentElementHash.match(re);

			if (res == null)
				return;

			if (res[1] == 'album')
			{
				var AlbumPage = res[4];
				if (typeof res[4] == 'undefined' || res[4] == null || res[4] == '')
					AlbumPage = 1;

				this.ShowAlbum(res[2], AlbumPage);
			}
			else if (res[1] == 'albums')
			{
				this.ShowAlbumsView();
			}
			else if (res[1] == 'image')
			{
				this.ShowPicture(res[2]);
			}
			else if (res[1] == 'tags')
			{
				this.ShowPictureTags(res[2]);
			}
		}
    },

    /**
    * Highlights a star when mouse hovers over one of the stars
    */
    HighlightStar: function(Index, PictureId)
    {
		for (i = 1; i <= 5; i++)
		{
			if (i > Index)
				$('star' + i + '_' + PictureId).src = "/modules/pictures/star1.png";
			else
				$('star' + i + '_' + PictureId).src = "/modules/pictures/star2.png";
		}
    },

    /**
    * Resets the stars when mouse moves away from the stars
    */
    LoseStarHighlight: function(PictureId, Rating)
    {
		for (i = 1; i <= 5; i++)
		{
			if (Rating < i)
				$('star' + i + '_' + PictureId).src = "/modules/pictures/star1.png";
			else
				$('star' + i + '_' + PictureId).src = "/modules/pictures/star2.png";
		}
    }
};

var EmaModulePicturesPublic = new EmaModulePicturesPublicClass('EmaModulePicturesPublic');
