var cookieoptions = { expires: 30, path: '/' };

/**
 * make checkboxes dependent from each other
 */
$.fn.makeDependent = function(){
	var checkboxes = $(this);
	var children = checkboxes.filter(':gt(0)');
	var main = checkboxes.eq(0);

	main.change(function(){
		var id = this.id;
		
		if(this.checked){
			children.each(function(){
				this.checked = false;
			});
	
			// uncheck partner dbs
			switch(id){
				case 'radio-channel-laif':
					$('#laifchannels-laiftravel :radio').each(function(){
						this.checked = false;
					});
					break;
				case 'radio-channel-laiftravel':
					$('#laifchannels-laif :radio').each(function(){
						this.checked = false;
					});
					break;
			}
		}
	});
	children.change(function(){
		var cb = this;
		var checkbox = $(cb);
		
		if(cb.checked){
			main.get(0).checked = false;

			// uncheck partner dbs
			switch(main.attr('id')){
				case 'radio-channel-laif':
					$('#laifchannels-laiftravel :radio').each(function(){
						this.checked = false;
					});
					break;
				case 'radio-channel-laiftravel':
					$('#laifchannels-laif :radio').each(function(){
						this.checked = false;
					});
					break;
			}
		
		} else if(children.filter(':checked').length == 0) {
			main.get(0).checked = true;
			main.trigger('change');
		}
	});
}

/**
 * make radio buttons behave like checkboxes
 */
$.fn.behaveLikeRadio = function(){
	
}

/**
 * make checkboxes behave like radio buttons
 */
$.fn.behaveLikeRadio = function(){
	var buttons = $('#radio-channel-laif, #radio-channel-laiftravel');
	
	// make sure there's only one button checked initially
	if(buttons.find(':checked') > 0){
		buttons.find(':checked:gt(0)').each(function(){
			this.checked = false;
		});
	}
	
	// uncheck all other checkboxes when one checkbox is checked
	buttons.change(function(){
		var cb = this;
		var id = '#'+cb.id;
		
		if(cb.checked){
			buttons.filter(':not('+id+')').each(function(){
				this.checked = false;
				$(this).trigger('change');
			});
		}
	});
	
	// set channel
	$('#laifchannels-laif :radio').change(function(){
	   if(this.checked) $('#input-channel').val('laif');
	});
	$('#laifchannels-laiftravel :radio').change(function(){
	   if(this.checked) $('#input-channel').val('laiftravel');
	});
}


/**
 * Folding Main Menu
 */
jQuery.fn.foldingMainMenu = function() {
	// unfold current item parents
	$(this).find('.current-tree-item').parents('ul').each(function(){
		$(this).css('display', 'block');
	});
	$(this).find('.current-tree-item').parents('li').each(function(){
		$(this).addClass('current-tree-ancestor');
	});

	if ($.browser.msie && $.browser.version*1 < 7){
		this.find('li').hover(
			function(){
				$(this).addClass('jshover');
			},
			function(){
				$(this).removeClass('jshover');
			}
		);
	}
}


/**
 * Folding Channels
 */
$.fn.foldingChannels = function(){
	var tree = $(this);
	var treeitems = tree.find('li.topchannel');

	if(tree.length == 0 || treeitems.length == 0) return;

	// get cookie
	var foldedChannels = $.cookie('foldedChannels');
	if(foldedChannels === null){
		foldedChannels = new Array();
		treeitems.each(function(){
			foldedChannels.push($(this).children('ul').attr('id'));
		});
		$.cookie('foldedChannels', foldedChannels.join(','), cookieoptions);
	} else {
		foldedChannels = foldedChannels.split(',');
	}

	// hide all subtrees
	treeitems.children('ul').each(function(i, el){
		if($.inArray(this.id, foldedChannels) != -1){
			$(el).hide();
		} else {
			$(el).parent().addClass('children-expanded');
		}
	});
	
	// bind click eventhandler
	treeitems.children('label').click(function(){
		var parent_li = $(this).parent();
		parent_li.toggleClass('children-expanded');
		
		// get index of current treeitem
		var subitems = $(this).siblings('ul').attr('id');
		
		// toggle display and set cookie
		if($.inArray(subitems, foldedChannels) != -1){ // is in array
			$('#'+subitems).show();
			foldedChannels = jQuery.grep(foldedChannels, function(value, key){
				return value != subitems;
			});
			$.cookie('foldedChannels', foldedChannels.join(','), cookieoptions);
		} else {
			$('#'+subitems).hide();
			foldedChannels.push(subitems);
			$.cookie('foldedChannels', foldedChannels.join(','), cookieoptions);
		}
		
		return false; // do not change checkbox state
	});
	
}


/**
 * execute the following when the DOM is ready:
 */
$(document).ready(function() {
	$('#mainmenu').foldingMainMenu();
	
	// make radiobuttons behave like checkboxes
	var radio_checked;
	$('#laifchannels :radio')
	.focus(function(){
		// store radio state before click
		radio_checked = this.checked;
	})
	.click(function(){
		// fire blur event
		this.blur();
	})
	.blur(function(){
		// toggle radio state when blur event is fired
		this.checked = !radio_checked;
	});

	// make database checkboxes behave like radio buttons
	$('#radio-channel-laif, #radio-channel-laiftravel').behaveLikeRadio();
	
	// make checkboxes dependent from each other 
	$('#laifchannels-laif :radio').makeDependent();
	$('#laifchannels-laiftravel :radio').makeDependent();

	$('#laifchannels').foldingChannels();
	$('#searchform').submit(function(){
		var self = $('#hiddenchannels');
		self.empty();
		$('#laifchannels :radio:checked').each(function(){
			self.prepend('<input type="hidden" name="CHANNEL[]" value="' + $(this).val() + '">');
		});
	});
});