// $Id$

var config_debug				= typeof(config_debug) == "boolean" ? config_debug : true;
var epg_config_ajax_url_add		= "/ajax/epg-favorites.php?hash={hash}&uid={uid}&pid={pid}&action=add";
var epg_config_ajax_url_remove	= "/ajax/epg-favorites.php?hash={hash}&uid={uid}&pid={pid}";
var epg_config_add_link			= '&raquo; <a href=\'javascript:add_favorites({uid}, "{pid}", "{hash}")\'>Lisää suosikiksi</a>';
var epg_config_remove_link		= '&raquo; <a href=\'javascript:remove_favorites({uid}, "{pid}", "{hash}")\'>Poista suosikeista</a>';

// Trigger function on document load
$(document).ready( function(){
	$('.program-row .name a').tt({
		vAlign: 'below',
		align: 'right',
		nudgeY: -33,
		timeOut: 150,
		ttClass: 'tooltip'
	});
})

// AJAX function which adds favorite
function add_favorites(uid, pid, hash){
	
	// Make sure we have required parameters as right variable type
	if( typeof(uid) != "number" || typeof(pid) != "string" || typeof(hash) != "string" ){
		if( config_debug === true ){
			alert('ERROR: Some of required parameters are not valid');
			return;
		}
	}
	
	// Replace URL with parameters
	var ajax_url = epg_config_ajax_url_add.replace('{uid}', uid);
	ajax_url = ajax_url.replace('{pid}', pid);
	ajax_url = ajax_url.replace('{hash}', hash);
	
	// Run AJAX
	$.ajax({
		url: ajax_url,
		success: function(response){
			if( response == 'OK' ){
				
				// Replace link parameters
				var remove_link = epg_config_remove_link.replace('{uid}', uid);
				remove_link = remove_link.replace('{pid}', pid);
				remove_link = remove_link.replace('{hash}', hash);
				
				// Replace link with remove favorites -link
				$('.toggle_favorites_'+pid).html(remove_link);
				
				// Toggle favorite selector
				$('tr:has(a#program-link-id-'+pid+')').toggleClass('favorite');
				
			}
			else{
				if( config_debug === true ){
					alert(response);
				}
			}
		}
	});	
	
}

// AJAX function which removes favorite
function remove_favorites(uid, pid, hash){
	
	// Make sure we have required parameters as right variable type
	if( typeof(uid) != "number" || typeof(pid) != "string" || typeof(hash) != "string" ){
		if( config_debug === true ){
			alert('ERROR: Some of required parameters are not valid');
			return;
		}
	}
	
	// Replace URL with parameters
	var ajax_url = epg_config_ajax_url_remove.replace('{uid}', uid);
	ajax_url = ajax_url.replace('{pid}', pid);
	ajax_url = ajax_url.replace('{hash}', hash);
	
	// Run AJAX
	$.ajax({
		url: ajax_url,
		success: function(response){
			if( response == 'OK' ){
				
				// Replace link parameters
				var add_link = epg_config_add_link.replace('{uid}', uid);
				add_link = add_link.replace('{pid}', pid);
				add_link = add_link.replace('{hash}', hash);
				
				// Replace link with remove favorites -link
				$('.toggle_favorites_'+pid).html(add_link);
				
				// Toggle favorite selector
				$('tr:has(a#program-link-id-'+pid+')').toggleClass('favorite');
				
			}
			else{
				if( config_debug === true ){
					alert(response);
				}
			}
		}
	});	
	
}

/* End of file: ohjelmataulukot.js */
/* Location: ./assets/js/ohjelmataulukot.js */