var ajaxRequest;

function add_to_watchlist(id, channel){
    $.get(absolute_path + 'ajax.php?cmd=addToWatchlist',
        { 
            id: id,
            ch: channel
        },
        function(data)
        {
            $("dl#my_watchlist").html(data);
        }
     );
}

function remove_from_watchlist(id, channel){
    $.get(absolute_path + 'ajax.php?cmd=removeFromWatchlist',
        { 
            id: id,
            ch: channel
        },
        function(data)
        {
            $("dl#my_watchlist").html(data);
        }
     );
}

/* --- 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"), "");
}
/* --- --- */

/* --- popup --- */
// Floater plain
function floater(width,height,url) {
    floaterWin = window.open(url,'floaterDoc','height='+height+',width='+width+',left=0,top=0,dependent=yes,resizable=yes,scrollbars=no');
    window.floaterWin.focus();
}
// Floater with scrollbars       
function floaterScroll(width,height,url) {
    scrollfloaterWin = window.open(url,'floaterScrollDoc','height='+height+',width='+width+',left=0,top=0,dependent=yes,resizable=yes,scrollbars=yes');
    window.scrollfloaterWin.focus();
}
// Popup
function openWindow(width, height, name, url) {
    SepWindow = window.open(url, name, 'height='+height+', width='+width+', left=0, top=0, dependent=yes, resizable=yes, scrollbars=yes');
    window.SepWindow.focus();
}

/* --- text-chars counter / limiter --- */
function textCounter(textfield, counterfield, maxlimit) {
	if (textfield.value.length > maxlimit) {
		textfield.value = textfield.value.substring(0, maxlimit);
	}
	else {
		if(counterfield.value != undefined) {
			counterfield.value = maxlimit - counterfield.value.length;
		}
	}
}

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';
                }
                
            });
            
            add_to_watchlist(id, watchlist_channel);

        } 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';
                }
                
            });
    
            remove_from_watchlist(id, watchlist_channel);
        }
        
        return false;
        
    });

}

function addJquerySwitchteaserFunctionality() {
    
    var switchInterval;
    var switchDelay = 5000;
    var hoverTeaserNr = -1;
        
    $('a.posMarker').hover(function() {
        var marker_id = $(this).attr('id');                     //zB: filmtipps_marker_1 
        var teaser_id = marker_id.replace('marker', 'teaser');  //zB: filmtipps_teaser_1
        var teaser_id_split = teaser_id.split('_');
        var tipteaser_id = teaser_id_split[0];                  //zB: filmtipps
        
        $(this).addClass('posMarker_active');
        $(this).siblings('.posMarker').removeClass('posMarker_active');
        
        $('div#'+teaser_id).siblings('.tipSwitchteaser_'+tipteaser_id).stop(true, true).fadeOut('medium');
        $('div#'+teaser_id).stop(true, true).fadeIn('medium');
    });
    
    $('div.tipSwitchteaser').mouseover(function() {
        var index = $("div.tipSwitchteaser").index(this);
        hoverTeaserNr = index;
    });
    $('div.tipSwitchteaser').mouseout(function() {
        hoverTeaserNr = -1;
    });
    
    switchInterval = setInterval(function(){
        
        $('div.tipSwitchteaser').each(function(index, teaser) {
            if(hoverTeaserNr != index) {
                var id_cur_visible_teaser =  $('div.tipSwitchteaserContent:visible', this).attr('id');
                var total_teasers = $('div.tipSwitchteaserContent', this).size();
                var id_split = String(id_cur_visible_teaser).split('_');
                var index_cur_visible_teaser = id_split[2];
                var id_next_teaser;
                var id_next_marker;
                if(total_teasers > 1) {
                    if(parseInt(index_cur_visible_teaser) + 1 <= total_teasers) {
                        id_next_teaser = id_split[0] + '_teaser_' + String(parseInt(index_cur_visible_teaser) + 1);
                        id_next_marker = id_split[0] + '_marker_' + String(parseInt(index_cur_visible_teaser) + 1);
                    } else {
                        id_next_teaser = id_split[0] + '_teaser_1'; 
                        id_next_marker = id_split[0] + '_marker_1'; 
                    }
                    $('div.tipSwitchteaserContent:visible', this).stop(true, true).fadeOut('medium');
                    $('div#'+id_next_teaser).stop(true, true).fadeIn('medium');
                    
                    $('a#'+id_next_marker).addClass('posMarker_active');
                    $('a#'+id_next_marker).siblings('.posMarker').removeClass('posMarker_active');
                }
            }
        });    
        
    }, switchDelay ); 
    
}