/*
 * Copyright 2000-2008 namics ag. All rights reserved.
 */

/**
 * TV.Search class.
 * 
 * @author Remo Brunschwiler, namics ag
 */

TV.Search = function() {
	this.bindSearchEvents();
};

/* ==========================================================
 * Binds the click event to add broadcasts to favorites.
 * ==========================================================
 */
TV.Search.prototype.addToFavorites = function () {
	TV.$('a.add_to_fav, a.add_to_mytv').livequery('click', function () {
		var $this = TV.$(this);
		
		// ajax call to add slotId to the favoritebroadcasts cookie
		TV.$.getJSON(TV.CONSTANTS.SCRIPTS.ADD_BROADCAST + $this.attr('id') + '?format=json', 
			function(data){
				if (data.success == true) {
					// everything is ok
					
					// add the selected class
					if($this.hasClass('add_to_fav')) {
						// for the normal search results
						TV.$($this.parents('.right')[0]).addClass('rightred');
						$this.removeClass('add_to_fav').addClass('remove_from_fav');
					}
					else if($this.hasClass('add_to_mytv')) {
						// for the top search results
						$this.removeClass('add_to_mytv').addClass('remove_from_mytv')
						.next().text('Entfernen aus "Mein TV Programm"');
					}
					
					// add one to the mytvprogram counter
					var $countMyTv = TV.$('#count_mytv');
					
					$countMyTv.text(parseInt($countMyTv.text()) + 1);
				}
			}
		);
		
		return false;
	});
};

/* ==========================================================
 * Binds the click event to remove broadcasts from favorites.
 * ==========================================================
 */
TV.Search.prototype.removeFromFavorites = function () {
	TV.$('a.remove_from_fav, a.remove_from_mytv').livequery('click', function () {
		var $this = TV.$(this);
		
		// ajax call to add slotId to the favoritebroadcasts cookie
		TV.$.getJSON(TV.CONSTANTS.SCRIPTS.REMOVE_BROADCAST + $this.attr('id') + '?format=json', 
			function(data){
				if (data.success == true) {
					// everything is ok
					
					// remove the selected class
					if($this.hasClass('remove_from_fav')) {
						// for the normal search results
						TV.$($this.parents('.right')[0]).removeClass('rightred');
						$this.removeClass('remove_from_fav').addClass('add_to_fav');
					}
					else if($this.hasClass('remove_from_mytv')) {
						// for the top search results
						$this.removeClass('remove_from_mytv').addClass('add_to_mytv')
						.next().text('Hinzufügen zu "Mein TV Programm"');
					}
					
					// sub one from the mytvprogram counter
					var $countMyTv = TV.$('#count_mytv');

					$countMyTv.text(parseInt($countMyTv.text()) - 1);
				}
			}
		);
		
		return false;
	});
};

/* ==========================================================
 * Binds the click event to select genre buttons
 * ==========================================================
 */
TV.Search.prototype.selectGenres = function() {
	var that = this;
	
	TV.$('.selectgenre').each( function() {
		var $button = TV.$(this);
		var channels = [];
		var type = $button.attr('name');
		
		$button.toggle(
			function () {		
				// ajax call to get the correct channelids
				TV.$.getJSON(TV.CONSTANTS.SCRIPTS.GET_CHANNELS_BY_TYPE + type +  '?format=json', 
					function (data) {
						if (data.success == true) {
							channels = data.channels;
							
							for(var i=0; i < channels.length; i++) {
								var channelId = channels[i];
								TV.$('#channel_' + channelId + '').attr('checked', 'checked');
							}
							
							$button.attr('checked', 'checked');
						}
					}
				);
			},
			function () {		
				// ajax call to get the correct channelids
				TV.$.getJSON(TV.CONSTANTS.SCRIPTS.GET_CHANNELS_BY_TYPE + type +  '?format=json', 
					function (data) {
						if (data.success == true) {
							channels = data.channels;
							
							for(var i=0; i < channels.length; i++) {
								var channelId = channels[i];
								TV.$('#channel_' + channelId + '').attr('checked', '');
							}
							
							$button.attr('checked', '');
						}
					}
				);
			}
		);	
	});
};

/* ==========================================================
 * Binds the events
 * ==========================================================
 */
TV.Search.prototype.bindSearchEvents = function () {
	var that = this;
	TV.$(document).ready(function () {
		that.addToFavorites();
		that.removeFromFavorites();
		that.selectGenres();
	});
};
