
// JavaScript Document

WatchList = Class.create();
						   
WatchList.prototype =	{

    container:null,
	tbody:null,
	linksClass:null,
    rUrl:'/artist/addwatchitem/',
    _action:null,
    addImg:'<img src="/images/add_watch_list.gif" />',
	removeImg:'<img src="/images/remove_watch_list.gif" />',
    _link:null,
    _by:null,
	loader:'<div style="text-align:center; width:100%;"><img src="/images/loading.gif" /></div>',
	
	initialize:function(container, linksClass, tbody)
	{
        this.container = $(container);
		this.tbody = $(tbody);
		this.linksClass = linksClass;

        this.container.select('span.' + this.linksClass).each(function(link) {
            link.observe('click', this.onWatchLinkClick.bindAsEventListener(this));
        }.bind(this));
        
        this.container.select('th.sortable').each(function(link) {
            link.observe('click', this.onSortLinkClick.bindAsEventListener(this));
        }.bind(this));   
	},


    onWatchLinkClick:function(e)
    {
        this._link = e.element();
        this._action = this._link.title;
        var code = this._link.id;
        this.addToWatchList(code);
        Event.stop(e);
    },
    
    onSortLinkClick:function(e)
    {
        var link = e.element();
        var sort = link.title;
        var by = '';
        
        if(this._by == 'DESC') {
            by = 'ASC';
            this._by = 'ASC';
        }       
        else{
            by = 'DESC';
            this._by = 'DESC';
        }                           
                
        this.sortArtist(sort, by);

        Event.stop(e);
        var ok;
        var okkk;
    },    
	
	addToWatchList:function(code)
	{
		//this.setToObjectLoading();
		var vari = '?_action=' + this._action + '&code=' + code;
		var SUrl = this.rUrl + 'format/json/' + vari;		

	
		var options = {
			method : 'get',
			evalJSON : true,
			onSuccess : this.onLoadSuccess.bindAsEventListener(this),
			onFailure : this.onLoadFailure.bindAsEventListener(this)
		};
		
		new Ajax.Request(SUrl, options);		
	},
    
    sortArtist:function(sort, by)
    {
        //this.setToObjectLoading();
        var vari = '?_action=sort'  + '&order=' + sort + '&by='+by;
        var SUrl = '/artist/sort/format/html/' + vari;        

    
        var options = {
            method : 'get',
            onSuccess : this.onSortSuccess.bindAsEventListener(this),
            onFailure : this.onLoadFailure.bindAsEventListener(this)
        };
        
        new Ajax.Request(SUrl, options);        
    },
    
    onSortSuccess:function(transport)
    {
        var result = transport.responseText;
        this.tbody.update(result);
        
        this.container.select('span.' + this.linksClass).each(function(link) {
            link.observe('click', this.onWatchLinkClick.bindAsEventListener(this));
        }.bind(this));        
        
        var okk;
        var ok;      
    },    
	
	onLoadSuccess:function(transport)
	{
		var result = transport.responseJSON;
		
		if(result.result)
		{
			if(this._action == 'Add') {
                
			    this._link.update(this.removeImg + 'remove from watch list');
                this._link.title = 'Remove';
            }
            else if(this._action == 'Remove') {
                
                this._link.update(this.addImg + 'add to watch list');
                this._link.title = 'Add';                
            }
            				
		}
		else
		{		
			
		}

	},
	
	onLoadFailure:function(transport)
	{
		var result = transport.responseText;
		alert(result);	
	},
	

	setToObjectLoading:function()
	{
        this.tbody.update(this.loader);			
	},
	
	setDone:function()
	{
		//show delete button
		this.delBtn.show();
		
		//show reply button
		if(this._item != 'item2'){
		   this.replyBtn.show();	
		   }	
	}	
}; 
