function DictatorRanking(startingIndex, onScreen) {
    
	this.index = this.lastIndex = startingIndex;
	this.onScreen = onScreen;
	this.initialize();
};

DictatorRanking.prototype = {
		
	ranking: null,
	lastIndex: 0,
	index: 0,
	size: 0,
	editableMode: false,
	
	initialize: function()
	{
		
		var cc = this;
		$("#ranking").load("/ranking/index/", null, function() {
          
          cc.calculateSizes();		       
		  
		  cc.showRanking();
		});

	},
	
	
	calculateSizes: function()
	{
        var cc = this;
	  
	       cc.size = $("#rankingList li").size();
          
		   console.debug(cc.size);
		  
          if(cc.index >= cc.size - cc.onScreen) {
              cc.index = (cc.size - cc.onScreen);
              cc.lastIndex = cc.index -1;
			  cc.calculate();
          }

		
	},
	
	
	showRanking: function()
	{
        var cc = this;
		
        cc.calculate();
		
		$("#rankingPlaceholder").fadeOut('slow');
		
            $("#ranking").show(1000, function() {
												
				$("#ranking .scrollUp").click(function() {
                    cc.scrollUp();
                    return false;
				});
				
                $("#ranking .scrollDown").click(function() {
   				   cc.scrollDown();
                   return false;    
                });
				
				$("#editMyRanking").click(function() {
					
                    cc.makeEditable();
					return false;
					
				});
				
			});
        						
                
		
	},
	
	calculate: function()
	{
        var index = this.index;
        var cc = this;
		
        $("#rankingList").children("li").each(function(key, elm) {
        
            if(key >= cc.index && key <= cc.index + (cc.onScreen - 1))
	           $(this).show();
	       else
	           $(this).hide();
        });
		
		
		if(cc.index == 0) {
			$("#ranking .scrollUp img").fadeTo(500, 0.33);
		} else {
			 
			 if(cc.lastIndex == 0)
			     $("#ranking .scrollUp img").fadeTo(500, 1);
	    }

        if(cc.index == (cc.size - cc.onScreen)) {
            $("#ranking .scrollDown img").fadeTo(500, 0.33);
        } else {
			 if(cc.lastIndex == (cc.size - cc.onScreen - 1))
		      	 $("#ranking .scrollDown img").fadeTo(500, 1);
        }
		
	},
	
	
	scrollUp: function()
	{
		var cc = this;
		
		if(cc.index > 0) {
            this.lastIndex == this.index--;
            this.calculate();
		}
		
	},
	
	scrollDown: function() {
        
		var cc = this;
		
        if(cc.index < (cc.size - cc.onScreen)) {
			this.lastIndex = this.index++;
	        this.calculate();
        }		
		
	},
	
	makeEditable: function()
	{
				
		var cc = this;
		if(!cc.editableMode) {
			
			cc.editableMode = true;
		
			var theLink = '<img class="dictatorRemover" alt="x" src="/lib/icons/16x16/actions/cancel.png" />';
			
			$(".dictatorCard img").hide();
			
			$(".dictatorCard").prepend(theLink);
			
			
				 		
			$("#rankingList").sortable();
			cc.onScreen = 10;
					
			cc.calculate();
	
	
	        $(".dictatorRemover").click(function() {
	                                    
	            $(this).parents("li").fadeOut(500, function() {
	                
	                $(this).remove();
	                cc.calculateSizes();
	                
	            })
	            
	        });
			
			$("#rankingDescription").hide();
		    $("#rankingEditDescription").show();
			
			$("#editSave").click(function() {
				cc.editSave();
				return false;
				
			});
        }		
		
	},
	
	editSave: function()
	{
		var cc = this;
		if(cc.editableMode) {
			
			
			var saveArr = [ ];
			
			$("#rankingList li").each(function() {
				saveArr.push(this.id);				
			});
					
			$.ajax({
                type: "POST",
                url: "/ranking/save",
                data: { "list[]": saveArr },
				dataType: 'json',
                success: function(msg)
				{
                    if(msg.type == 0) {
						alert('ei onnas');
						
					} else {
						alert('onnas');
						
					}
					
					
                },
				error: function(msg)
				{
			         alert(msg);		
				}
            });
			
			
			
			
			console.debug(saveArr);			
			
			return false;
		}
				
	},
	
	
	editCancel: function()
	{
		var cc = this;
        if(cc.editableMode) {
            
            
            
        }
		
	}
	
	
	
	
	
	
	
	
};


