/*****************************************************************************************************/
/*                                                                                                   */
/*                                         'LAST MINUTE'                                             */          
/*                                                                                                   */
/*****************************************************************************************************/

function LASTMINUTE(){
	var JSObject = this;
	this.type = "lastminute"; 
	
	this.DOMDoc; //document object from thickbox window
	
	this.JSON = [{'id':12,'title':'Titlu 1','picture':'application/default/views/images/','description':'Description de l\'annonce','link':'http://www.fzr.ro'},
				 {'id':12,'title':'Titlu 2','picture':'application/default/views/images/','description':'Description de l\'annonce','link':'http://www.fzr.ro'},
				 {'id':12,'title':'Titlu 3','picture':'application/default/views/images/','description':'Description de l\'annonce','link':'http://www.fzr.ro'},
				 {'id':12,'title':'Titlu 4','picture':'application/default/views/images/','description':'Description de l\'annonce','link':'http://www.fzr.ro'}];
	
	
	this.rowsPerPage;
	this.totalPages = 6;
	this.currentPage = 1;
	this.pageStep = 3;
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(){
		
		var table = $('#'+this.type+'_table',this.DOMDoc).get(0);
		this.rowsPerPage = $('#'+this.type+'_title',table).get().length;
		
		//test number of sounds in the library
		if (this.JSON.length == 0 || this.JSON == null){
			$('#lastminute_container', this.DOMDoc).empty();
			$('#lastminute_container', this.DOMDoc).html('<span class="formDarkGreyText" style="padding-left:33px;">'+'Aucune annonce.'+'</span>');
			$('#lastminute_container', this.DOMDoc).css('top','20px');
			return;
		}
		
		this.initCreate();
		this.initPagination();
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE PANEL                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		var table = $('#'+this.type+'_table',this.DOMDoc).get(0);
		
		for (var i=0; i<this.rowsPerPage && i<this.JSON.length; i++){
			
			//image
			var td = $('#'+this.type+'_image',table).get(i);
			$(td).html('<a href="'+this.JSON[i].link+'"><img src="'+this.JSON[i].picture+'" alt="'+this.JSON[i].title+'" border="0" class="" /></a>');
					
			//title
			var a = $('#'+this.type+'_title',table).get(i);
			$(a).html(this.JSON[i].title);
			$(a).attr('title',this.JSON[i].title);
			a.href = this.JSON[i].link;
			
			//description
			var span = $('#'+this.type+'_description',table).get(i);
			$(span).html(this.JSON[i].description);
			
			var td = $($('#'+this.type+'_title',table).get(i)).parent().parent().parent().parent().parent();
			td.removeClass('annonceBg');
			//td.addClass('annonceBg');
		}	
		
		for (var i=this.JSON.length; i<this.rowsPerPage; i++){
			
			//image
			var td = $('#'+this.type+'_image',table).get(i);
			$(td).html("");
					
			//title
			var a = $('#'+this.type+'_title',table).get(i);
			$(a).html("");
			$(a).attr('title',"");
			
			//description
			var span = $('#'+this.type+'_description',table).get(i);
			$(span).html("");
			
			var td = $($('#'+this.type+'_title',table).get(i)).parent().parent().parent().parent().parent();
			td.removeClass('annonceBg');
		}
		
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE PAGINATION                                       */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initPagination = function(){
		
		$('#'+this.type+'_pagination_box',this.DOMDoc).empty();
		
		$('#'+this.type+'_pagination_box',this.DOMDoc).append('<table cellpadding="0" cellspacing="0" border="0"><tr></tr></table>');
		
		var table = $('table',$('#'+this.type+'_pagination_box',this.DOMDoc));
		//alert(this.currentPage)
		//calculate min limit
		if (this.currentPage <= this.pageStep){
			var min_limit = 1;
		}
		else{
			var min_limit = this.currentPage - this.pageStep;
		}
		
		//calculate max limit
		if (this.currentPage >= this.totalPages-this.pageStep){
			var max_limit = this.totalPages;
		}
		else{
			var max_limit = this.currentPage + this.pageStep;
		}
		
		
		//insert numbers
		for (var i=min_limit; i<=max_limit; i++){
			
			//for pages that are different then the current page, use link
			if (this.currentPage != i){
				$('tr:eq(0)',table).append("<td class='numCellPast' width='20' height='20' align='center' style='cursor:pointer'><a href='javascript:void(0)' title='"+JSInterface.MultiLanguage.translate('Page')+" "+i+"'>"+i+"</a></td>");
				$('tr:eq(0) a:last',table).data('page',i);
			}
			else{
				$('tr:eq(0)',table).append("<td class='numCellCurrent' width='20' height='20' align='center'><font style='color:#FFFFFF; line-height:10px'>"+i+"</font></td>");
			}
			
			//add spacer between page numbers
			if (i < max_limit){
				$('tr:eq(0)',table).append("<td width='6' style='line-height:10px'></td>");
			}
		}
		
		
		//add Previous and Next link 
		if (this.totalPages > 1){
		
			//add total number of pages
			$('tr:eq(0)',table).append("<td height='10' class='formDarkGreyText' style='line-height:10px'>&nbsp;&nbsp;"+JSInterface.MultiLanguage.translate('Of')+"&nbsp;&nbsp;</td>");
			$('tr:eq(0)',table).append("<td class='numCellPast' width='20' height='20' align='center' style='cursor:pointer'><a href='javascript:void(0)' title='"+JSInterface.MultiLanguage.translate('Page')+" "+this.totalPages+"'>"+this.totalPages+"</a></td>")
			$('tr:eq(0) a:last',table).data('page',this.totalPages);
			
			//add Previous link
			if (this.currentPage != 1){
				$($('tr:eq(0) *:first',table)).before("<td style='padding-right:15px; line-height:13px'><a class='moreLink' href='javascript:void(0)' title='"+JSInterface.MultiLanguage.translate('Page')+" "+(this.currentPage-1)+"'>"+JSInterface.MultiLanguage.translate('Previous')+"</a></td>");
				$('tr:eq(0) a:first',table).data('page',this.currentPage-1);
			}
			else{
				$($('tr:eq(0) *:first',table)).before("<td class='lightGreyText' style='padding-right:15px; line-height:13px; color:#999999'>"+JSInterface.MultiLanguage.translate('Previous')+"</td>");	
			}
			
			//add Next link
			if (this.currentPage != this.totalPages){
				$('tr:eq(0)',table).append("<td style='padding-left:15px; line-height:13px'><a class='moreLink' href='javascript:void(0)' title='"+JSInterface.MultiLanguage.translate('Page')+" "+(this.currentPage+1)+"'>"+JSInterface.MultiLanguage.translate('Next')+"</a></td>");
				$('tr:eq(0) a:contains("'+JSInterface.MultiLanguage.translate('Next')+'")',table).data('page',this.currentPage+1);
			}
			else{
				$('tr:eq(0)',table).append("<td class='lightGreyText' style='padding-left:15px; line-height:13px; color:#999999'>"+JSInterface.MultiLanguage.translate('Next')+"</td>");	
			}
			
			
		}
		else{
			//add total number of pages
			$('tr:eq(0)',table).append("<td class='formDarkGreyText' style='padding-left:15px; line-height:13px;'>"+JSInterface.MultiLanguage.translate('Of')+" "+this.totalPages+"</td>");	
		}
		
		
		//attach click event to pagination links
		$('#'+this.type+'_pagination_box a',this.DOMDoc).parent().click(function(){
														  	JSObject.currentPage = $(this).children().eq(0).data('page');
															JSObject.changePage();
														  });
	
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CHANGE PAGE                                             */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.changePage = function(){
		
		$('#'+this.type+'_loader',this.DOMDoc).show();
		
		var start = (this.currentPage-1) * this.rowsPerPage + 1;
		var end = this.currentPage * this.rowsPerPage;
		
		//alert(JSInterface.localpath+"ads/ad/jsonlastminute?start="+start);
		
		$.ajax({
			   type: "POST",
			   url: JSInterface.localpath+"ads/ad/jsonlastminute",
			   cache: false,
			   async: true,
			   data: "start="+start,
			   dataType: "json",
			   success: function(responseJSON){
				    $('#'+JSObject.type+'_loader',JSObject.DOMDoc).hide();
					
					for (var i in responseJSON){
						//alert(i+" "+responseJSON[i]);
					}
					
					JSObject.totalPages = Number(responseJSON.totalPages);
					
					if (JSObject.totalPages == 0)  JSObject.totalPages = 1;
					JSObject.JSON = eval("(" + responseJSON.JSON + ")");
					
					if (JSObject.JSON == null || JSObject.JSON.length == 0){
						if (JSObject.currentPage != 1){
							$('#'+JSObject.type+'_pagination_box a:contains("'+JSInterface.MultiLanguage.translate('Previous')+'")',JSObject.DOMDoc).trigger("click");
							return;
						}
					}
					
					JSObject.initCreate();
					JSObject.initPagination();
					
			   },
			   error: function(){
					alert(JSInterface.MultiLanguage.translate('Our server is busy. Please try again later','alert'))
					$('#'+JSObject.type+'_loader',JSObject.DOMDoc).hide();
			   }
			 });
		
	}
	
	
	
	
}
