/**
 * @classDescription Fanclubs browse class, it allows the user to browse throught fanclubs alphabetically  
 * @author Givan
 * @author Iurie Safonov <iurie dot safonov at google dot com>
 * @version $id
 * @constructor 
 */
var Fanclubs = 
{
	/**
	 * page holds the current page number
	 */
	currentPage: 1,
	/**
	 * letter holds the current letter
	 */
	letter: "",
	/**
	* Search by string
	*/
	query: '',
	/**
	* Type can be either fanclub or stars
	*/
	type:'fanclub',
	/**
	* Last request URI
	*/
	params:'',
	/**
	 * Initializes browser
	 */
	init: function()
	{
		if ($('.searchStar').length > 0)
		{
			this.type = 'vedete';
		}
		if ( $("#gaseste > p > a").bind('click', function( e )//on letter click
		{
			$("#gaseste #search_form input").val('');
			Fanclubs.letter = $(this).html();//set the letter to the clicked letter
			Fanclubs.page(1);//go to the first page for selected letter
			return false;//avoid page refresh on button click
		}));
		$('#searchBtn').bind('click', function ( e )//on search button click
		{
			if ( $('#query').val().length >= 2 )
            {
                Fanclubs.letter = $('#query').val();//set the letter to the search text
                Fanclubs.page(1);//go to the first page for selected text
            } 
            else
            {
                alert('Trebuie sa selectezi o litera sau sa introduci cel putin 2 caractere in casuta!');
            }
			return false;//avoid page refresh on button click
		});
		var srchTimeoutID;
		$("#gaseste #search_form input").keyup(function(){
			var _this = $(this);
			window.clearTimeout(srchTimeoutID);
			srchTimeoutID = window.setTimeout(function() {
                if ( $('#query').val().length >= 2 )
                {
				    Fanclubs.query = $('#query').val();//set the letter to the search text
				    Fanclubs.page(1);//go to the first page for selected text
				    _this.blur();
                } 
                else
                {
                    alert('Trebuie sa selectezi o litera sau sa introduci cel putin 2 caractere in casuta!');
                    window.clearTimeout(srchTimeoutID);
                }
			    return false;//avoid page refresh on button click
			}, 1000);
		});
		//query
	},
	/**
	 * Generates pagination for Fanclubs
	 */
	pagination : function()
	{
		$('.pagination_link').click(function()
		{
			Fanclubs.page($(this).attr('title'));
		});
	},
	/**
	 * Go to the specified PAGE number 
	 * @param {Object} pageNr
	 */
	page : function( pageNr )
	{
		//if only one page then hide pagination 
		if (pageNr <= 0)
		{
			$(".pagination").html("");
			return true;
		}

		this.currentPage = pageNr;
		
		if (this.type == 'fanclub')
		{
			var action = 'getFanclubs';
		}
		else
		{
			var action = 'getStars';
		}
		
		if (0 < Fanclubs.query.length && '' == Fanclubs.letter)
		{
			parameters = { 'module' : 'Ajax', 'action' : action, 'query' : Fanclubs.query, 'page' : pageNr };
		}
		else if (0 == Fanclubs.query.length && '' != Fanclubs.letter)
		{
			parameters = { 'module' : 'Ajax', 'action' : action, 'startingWith' : Fanclubs.letter, 'page' : pageNr };
		}
		else if (1 <= pageNr && '' != Fanclubs.params)
		{
			parameters = Fanclubs.params;
			parameters.page = pageNr;
		}

		var target = $('#results');
		
		//display loader
		target.html('<img src="/images/ajax-loader.gif"/>');
		
		//clear pagination
		$(".pagination").html("");
		
		$.get( '/index.php', parameters, function( data )
		{
			target.html(data);//clear old options
			Fanclubs.pagination();
		});
		Fanclubs.params = parameters;
		Fanclubs.letter = '';
		Fanclubs.query = '';
		//rotateBanners();
	},
	/**
	* Subbscribe fanclub
	*/
	join : function( UserId, type )
	{
		var type_id = ( type == 'blog' ) ? "ownerId=" : "fanclubId=";
		var ucfirst = type.substring(0,1).toUpperCase()+type.substring(1,type.length);
		$.ajax({
			url: '/?module=Ajax&action=join'+ucfirst,
			cache: true,
			data: type_id+UserId,
			type: 'GET',
			success:function (msg)
			{
				switch (msg)
				{
					case '1':
						if ( type == 'blog' )
						{
							document.getElementById("warningMessage").style.display="block";
							document.getElementById('warningMessage').innerHTML="<p>Te-ai inscris cu succes la blog!</p>";
							document.getElementById("deleteFromBlog").style.display="block";
							document.getElementById("joinBlog").style.display="none";
							$("#inscriereFanclub"+UserId).remove();
						}
						else
						{
							alert('Te-ai inscris cu succes in fanclub!');
							$("#inscriereFanclub"+UserId).html('<a href="#" onClick="Fanclubs.leave(' + UserId + ', 0); return false;"><img src="/img/dezabonare_fanclub.gif" alt="" /></a><img src="/img/fancluburi_icon_2.gif" alt="" />');
							
						}
					break;
					case '-1':
						alert('Esti deja inscris in acest fanclub!');
					break;
					default:
						if ( type == 'blog' )
						{
							document.getElementById("warningMessage").style.display="block";
							document.getElementById('warningMessage').innerHTML="<p>Eroare inscriere la blog!</p>";
						}
						else
						{
							alert('Eroare inscriere '+ type + '!');
						}
						
				}
			},
			error: function(msg)
			{
				alert('eroare');
			}
		});
	},
	// nu se mai foloseste....
	leave : function(fanclubId, reload)
	{
		$.ajax({
			url: '/?module=Ajax&action=leaveFanclub',
			cache: true,
			data: "fanclubId="+fanclubId,
			type: 'GET',
			success:function (msg)
			{
				switch (msg)
				{
					case '1':		
						if (reload == 1)
						{
							window.location.reload();	
						}
						else
						{
							alert('Ai parasit cu succes fanclubul!');
							$("#inscriereFanclub"+fanclubId).html('<a href="#" onClick="Fanclubs.join(' + fanclubId + ', \'fanclub\'); return false;"><img src="/img/inscriere_fanclub.gif" alt="" /></a>');
						}
					break;
					default:
							alert('Eroare!');	
				}
			},
			error: function(msg)
			{
				alert('eroare');
			}
		});
	},
	deleteFanclubProposal : function(fanclubId)
	{
		$.ajax({
			url: '/?module=Ajax&action=deleteFanclubProposal',
			cache: true,
			data: "fanclubId="+fanclubId,
			type: 'GET',
			success:function (msg)
			{
			}
		});
	},
	deleteFanclubNews : function(fanclubId, newsId)
	{
		$.ajax({
			url: '/?module=Ajax&action=deleteFanclubNews',
			cache: true,
			data: "fanclubId="+fanclubId+"&newsId="+newsId,
			type: 'GET',
			success:function (msg)
			{
				switch (msg)
				{
					case '1':		
							window.location.reload();	
					break;
					default:
							alert('Eroare!');	
				}
			},
			error: function(msg)
			{
				alert('eroare');
			}
		});
	},
	deleteFromBlog : function( UserId )
	{
		$.ajax({
			url: '/?module=Ajax&action=deleteFromBlog',
			cache: true,
			data: "ownerId="+UserId,
			type: 'GET',
			success:function (msg)
			{
				switch (msg)
				{
					case '1':
						document.getElementById("warningMessage").style.display="block";
						document.getElementById('warningMessage').innerHTML="<p>Ai fost dezabonat cu succes!</p>";
						document.getElementById("deleteFromBlog").style.display="none";
						document.getElementById("joinBlog").style.display="block";
						$("#inscriereFanclub"+UserId).remove();
					break;
					default:
						document.getElementById("warningMessage").style.display="block";
						document.getElementById('warningMessage').innerHTML="<p>Eroare la dezabonare!</p>";
				}
			},
			error: function(msg)
			{
				alert('eroare');
			}
		});
	}
}
