/*
 * jQuery AutoQueue v1.0.0 - http://www.onlinetvcast.com/
 *
 *
 */

// FIXME: глобальный рефакторинг
 
 (function($) {
 
 	function Module_AutoQueue(options) {
 		var defaults = {};
        this.options = $.extend(defaults, options);
 		this.construct();
 	}
 	 	
 	Module_AutoQueue.prototype = {
 		construct: function() {
 		},
 		
 		addInAutoQueue: function($link) {
 			var self = this;
 			// :first нужен если это episode или source (ведь у родителя тоже есть свой video_source:first)
 			var $video_source = $link.parents('.video_source:first');
			var id = $video_source.attr('id').split('_');
			var type = id[3];
			
 			$.ajax({
				url : $link.attr('rel'),
				type : "POST",
				dataType : 'json',
				timeout : 30000,
				error : function() {
					ajaxError();
				},
				success : function(json) {
					if (json.error) {
						showGrowl('<h1 class="mGrowl">' + json.error + '</h1>', 'error');
						return;
					}					
					// для удаления
					$video_source.find('a.removeFromAutoQueue').attr('id', 'autoQueny_id_' + json.idInAutoQueue);
					showGrowl('<h1 class="mGrowl">' + json.message + '</h1>', 'ok');
					$('.autoqueny_toggle', $video_source).toggle();
				},
				data : {
					'type' : type,
					'id' : id[2]
				}
			});
        },
                
        removeFromAutoQueue: function($link) {
        	var self = this;
        	/*if (! you_are_sure()) {
				return false;
			}*/
			// :first нужен если это episode или source (ведь у родителя тоже есть свой video_source:first)
			var id = $link.attr('id').split('_');
			var $video_source = $link.parents('.video_source:first');
			$j.ajax({
				url : $link.attr('rel'),
				type : "POST",
				dataType : 'json',
				timeout : 30000,
				error : function() {
					ajaxError();
				},				
				success : function(json) {
					if (json.error) {
						showGrowl('<h1 class="mGrowl">' + json.error + '</h1>', 'error');
						return;
					}										
					showGrowl('<h1 class="mGrowl">' + json.message + '</h1>', 'warning');
					$j('.autoqueny_toggle', $video_source).toggle();
				},
				data : {
					'id' : id[2]
				}
			});
        }      
 	}
 	
 	$.Module_AutoQueue = function(options) {
 		return new Module_AutoQueue(options);
 	};
 	
 })(jQuery);
