var ajaxRequest;

function add_to_watchlist(id){
    $.get(absolute_path + 'ajax.php?cmd=addToWatchlist',
        {
            id: id
        }
     );
}

function remove_from_watchlist(id){
    $.get(absolute_path + 'ajax.php?cmd=removeFromWatchlist',
        { 
            id: id
        }
     );
}

/* --- utils --- */
function splitPipe(str){
    str = ltrim(rtrim(str, '|'), '|');

    newarr = new Array();
    while(str.indexOf('|') != -1){
        var pos = str.indexOf('|');
        newarr.push(str.substr(0, pos));
        str = str.substr(pos+1);
    }
    newarr.push(str);

    return newarr;
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/* --- --- */

function addJqueryWatchlistFunctionality() {
    // Auf Watchlist setzen / removen
    
    $("img.watchlistIcon").unbind("click");                             /* entferne alte listener, falls nach einem ajax-aufruf neu initialisiert wird */
    
    $("img.watchlistIcon").click(function () {
        var classname_2 = $(this).attr('class').split(' ').slice(-1);   /* zB: watchlist_tv_2010040712345, watchlist_cinema_12332, ... */
        var classname_2_split = String(classname_2).split('_');         
        var watchlist_channel = classname_2_split[1];                   /* tv, cinema, ... */
        var id = classname_2_split[2];									/* 2010040712345, 12332, ... */
        
        // unchecked
        if(this.src.indexOf("empty") >= 0) {
            
            $('img.'+classname_2).each(function(idx, img) {
                if($(img).attr('src').indexOf("_12px.gif") >= 0){
                    this.src = '/img/global/icons/watchlist_checked_12px.gif';
                } 
                else if($(img).attr('src').indexOf("_24px.gif") >= 0){
                    this.src = '/img/global/icons/watchlist_checked_24px.gif';
                }
                else if($(img).attr('src').indexOf("_43px.gif") >= 0){
                    this.src = '/img/global/icons/watchlist_checked_43px.gif';
                }
                
            });
            
            add_to_watchlist(id);

        } else {
        // checked
        
            $('img.'+classname_2).each(function(idx, img) {
                
                if($(img).attr('src').indexOf("_12px.gif") >= 0){
                    this.src = '/img/global/icons/watchlist_empty_12px.gif';
                } 
                else if($(img).attr('src').indexOf("_24px.gif") >= 0){
                    this.src = '/img/global/icons/watchlist_empty_24px.gif';
                }
                else if($(img).attr('src').indexOf("_43px.gif") >= 0){
                    this.src = '/img/global/icons/watchlist_empty_43px.gif';
                }
                
            });
    
            remove_from_watchlist(id);
        }
        
        return false;
        
    });

}