var MenuHandler = {
		visible : new Array(),
		init : function(){
			$('ul ul', '#channels_menu').hide();
			MenuHandler.setVisible();
			$('ul h3 a', '#channels_menu').click(function(event){MenuHandler.menuBlockController(event, $(this))})
		},
		menuBlockController : function(event, obj){
			var s_block = obj.parent().parent();
			var block_id = s_block.attr('id').substr(5);
			if($.inArray(block_id, MenuHandler.visible) != -1){
				var idx = 0;
				if(!MenuHandler.visible.indexOf){
					for(var i=0; i<MenuHandler.visible.length; i++){
						if(MenuHandler.visible[i]==block_id){
							idx = i;
						}
					}
				} else
					idx = MenuHandler.visible.indexOf(block_id);
				if(idx != -1) 
					MenuHandler.visible.splice(idx, 1);
				$('ul', s_block).slideUp('normal');
			} else {
				MenuHandler.visible.push(block_id);
				$('ul', s_block).slideDown('normal');
			}
			$.get("index.php", { module: "ajax", action: "renew_menu", cont: "ajax", set_menu: '[' + MenuHandler.visible.join('][') + ']' });
			event.preventDefault();
		},
		setVisible : function(){
			var open_blocks = $('#open_menu_blocks').attr('value');
			if(open_blocks != '[]'){
				MenuHandler.visible = open_blocks.substr(1).slice(0, -1).split("][");
				$.each(MenuHandler.visible, function(key, value) { $('#menu_'+value+' ul').show();});
			}
		}
};
$(document).ready(MenuHandler.init );

var Watched_handler = {
		init : function() {
			$('.wached a').bind('click', function(event) { Watched_handler.check_vote(event, $(this), null, null); });
		},
		check_vote : function(event, obj, xhr, pr_tr) {
			if(xhr == null){
				var pr_id = obj.attr('id').substr(6);
				var pr_tr = obj.parent().parent();
				xhr = $.get("index.php", { module: "ajax", action: "count_vote", cont: "ajax", pr_id: pr_id });
				setTimeout(function() { Watched_handler.check_vote(event, obj, xhr, pr_tr) }, 100);
			}
			else{
				if(xhr.readyState != 4){
					setTimeout(function() { Watched_handler.check_vote(event, obj, xhr, pr_tr) }, 100);
				}
				else{
					if(!pr_tr.hasClass('prog_checked')){
						pr_tr.addClass('prog_checked');
						obj.text('Pašalinti');
						obj.siblings('img').attr('src', 'images/not_watch.png');
					}else{
						pr_tr.removeClass('prog_checked');
						obj.text('Pažymėti');
						obj.siblings('img').attr('src', 'images/will_watch.png');
					}
				}
			}
			event.preventDefault();
		}
	}

	$(document).ready(Watched_handler.init );