var ajaxRequest;

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

function remove_from_watchlist(id, channel) {
    $.get(absolute_path + 'ajax.php?cmd=removeFromWatchlist',
        { 
            id: id,
            ch: channel
        },
        function(data)
        {
            if (channel == root) {
            	$("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"), "");
}

function getUrlParam( name , string ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( string );
	if ( results == null )
		return "";
	else
		return results[1];
}

/* --- --- */

/* --- 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) {
            	var filename_add = $(img).attr('src').indexOf("_grey") >= 0 ? '_grey' : '';
            	
                if ($(img).attr('src').indexOf("_12px.gif") >= 0) {
                    this.src = '/img/global/icons/watchlist_checked'+filename_add+'_12px.gif';
                } 
                else if ($(img).attr('src').indexOf("_24px.gif") >= 0) {
                    this.src = '/img/global/icons/watchlist_checked'+filename_add+'_24px.gif';
                }
                
            });
            
            add_to_watchlist(id, watchlist_channel);

        } else {
        // checked
        
            $('img.'+classname_2).each(function(idx, img) {
				var filename_add = $(img).attr('src').indexOf("_grey") >= 0 ? '_grey' : '';
				                 
                if ($(img).attr('src').indexOf("_12px.gif") >= 0) {
                    this.src = '/img/global/icons/watchlist_empty'+filename_add+'_12px.gif';
                } 
                else if ($(img).attr('src').indexOf("_24px.gif") >= 0) {
                    this.src = '/img/global/icons/watchlist_empty'+filename_add+'_24px.gif';
                }
                
            });
    
            remove_from_watchlist(id, watchlist_channel);
        }
        
        return false;
        
    });

}

// setzt bei veralberteten Seiten das Watchlist-icon
function checkWatchlistIcons(channel, ids_array) {
	if (channel != 'tv' && channel != 'cinema' && channel != 'events') { return false; }
	if (ids_array.length == 0) { return false; }

	var classname;
	for (var i=0; i<ids_array.length; i++) {
		classname = "watchlist_" + channel + "_" + ids_array[i];

		$('img.'+classname).each(function(idx, img) {
			var filename_add = $(img).attr('src').indexOf("_grey") >= 0 ? '_grey' : '';
	        if ($(img).attr('src').indexOf("_12px.gif") >= 0) {
	            this.src = '/img/global/icons/watchlist_checked'+filename_add+'_12px.gif';
	        }
	        else if ($(img).attr('src').indexOf("_24px.gif") >= 0) {
	            this.src = '/img/global/icons/watchlist_checked'+filename_add+'_24px.gif';
	        }
	    });
	}
}

function addJquerySwitchteaserFunctionality(switchteaser_size) {
    
    var teasername;
    if (switchteaser_size=='top') {
    	teasername = 'topTeaser';
    } else if (switchteaser_size == 'small') {
    	teasername = 'smallSwitchteaser';
    }else {
    	teasername = 'tipSwitchteaser';
    }
    
    var switchInterval;
    var switchDelay = 7000;
    var hoverTeaserNr = index = -1;
        
    $('a.posMarker').mouseover(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
        var tipteaser_nr = teaser_id_split[2];                  //zB: 1
		
		//hoverTeaserNr = $(this).index();
		if (switchteaser_size == 'top') {
			/* position des textcontainers anpassen - dieser könnte zu viel text enthalten */
        	var text_el = $('div#'+teaser_id+' div.textholder');
        	var text_el_height = text_el.outerHeight( true );
        	
			$('div#'+teaser_id).siblings('.'+teasername+'_'+tipteaser_id).stop(true, true).fadeOut('medium', function() {
    			//text_el.css('top', 0);
  			});
			
			$('div#'+teaser_id).stop(true, true).fadeIn('medium', function() {
				//text_el.stop(true, true).animate ( {"top": -text_el_height, opacity: 1 }, "medium" );
				text_el.css('top', -text_el_height);
			});
			
		}
		else {
			
			$('div#'+teaser_id).siblings('.'+teasername+'_'+tipteaser_id).stop(true, true).fadeOut('medium');
        	$('div#'+teaser_id).stop(true, true).fadeIn('medium');
			
		}
		
        $(this).addClass('posMarker_active');
        $(this).siblings('.posMarker').removeClass('posMarker_active');
        
        /*$('div#'+teaser_id).siblings('.'+teasername+'_'+tipteaser_id).stop(true, true).fadeOut('medium');
        $('div#'+teaser_id).stop(true, true).fadeIn('medium');*/
        
		
    });
    
    $('div.'+teasername).mouseover(function() {
		index = $("div."+teasername).index(this);
		hoverTeaserNr = index;
    });
    
    $('div.'+teasername).mouseout(function() {
		hoverTeaserNr = -1;
    });
    
    switchInterval = setInterval(function() {
        $('div.'+teasername).each(function(index, teaser) {
            if ($(teaser).hasClass("static"))
            	return true;	
            if (hoverTeaserNr != index) {
                var id_cur_visible_teaser = $('div.'+teasername+'Content:visible', this).attr('id');
                var total_teasers = $('div.'+teasername+'Content', 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.'+teasername+'Content:visible', this).stop(true, true).fadeOut('medium');
					
					if (switchteaser_size == 'top') {
                    	/* position des textcontainers anpassen - dieser könnte zu viel text enthalten */
						$('div#'+id_next_teaser).stop(true, true).fadeIn('medium', function() {
							var text_el = $('div#'+id_next_teaser+ ' div.textholder');
							var text_el_height = text_el.outerHeight( true );
							text_el.stop(true, true).animate ( {"top": -text_el_height }, "fast" );
						});
                    	
					} else {
                    	$('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 ); 
    
}

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