/* 
 * Show/hide Ajax results
 */
$(function() {

	$(document).ready(function(){

		$("#commType").find("input[@type$='checkbox']").each(function(){
			this.checked = true;
		});

		$("#searchBy3Month").attr("checked", "checked");
		//$("#searchBy12Months").attr("checked", "checked");
		$("#searchBy3Month").click(showCommResults);
		$("#searchBy12Months").click(showCommResults);
		//$(document).ready(showCommResults);
		$('#F').click(showCommResults);
		$('#U').click(showCommResults);
		$('#B').click(showCommResults);
		$('#M').click(showCommResults);
		$('#P').click(showCommResults);

		function showCommResults() {

			var F = $('#F').attr('checked') ? 'true' : 'false';
			var U = $('#U').attr('checked') ? 'true' : 'false';
			var B = $('#B').attr('checked') ? 'true' : 'false';
			var M = $('#M').attr('checked') ? 'true' : 'false';
			var P = $('#P').attr('checked') ? 'true' : 'false';
			var commTypeChk = $("#commType input:checked").length;
			checkComm(commTypeChk);
			//alert(commTypeChk);
			$("#commResults > div.searchResults").load("/de/jsp/app/ajax_html.jsp?content=commentaryLookup&type1=" + F + "&type2=" + U + "&type3=" + B + "&type4=" + M + "&type5=" + P + "&id=" + $("input[@name='searchGroup']:checked").val());
		}

		showCommResults();

		var toolCont = $(".tip");

		//form check
		var commTypeChk = $("#commType input:checked").length;
		checkComm(commTypeChk);
		$("#commType input:disabled").parent("li:first").append(toolCont).hover(
			function() {
				showTip($(this)); $(this).addClass("dropTrigger");
			},
			function() {
				hideTip();$(this).removeClass("dropTrigger");
			}
			);

		$("a#showAll").click(function(){
			$(this).addClass("disable");
			$("#commType input").removeAttr('disabled').attr("checked","checked").parent("li").unbind('mouseenter').unbind('mouseleave');

			checkComm(commTypeChk);
			showCommResults();
			return false;
		});

		function readCookie(name){
			var cookieValue = "";
			var search = name + "=";
			if(document.cookie.length > 0){
				offset = document.cookie.indexOf(search);
				if (offset != -1){
					offset += search.length;
					end = document.cookie.indexOf(";", offset);
					if (end == -1) end = document.cookie.length;
					cookieValue = unescape(document.cookie.substring(offset, end))
				}
			}
			return cookieValue;
		}

		function checkComm(e){
			var numberOfCheckBoxes = 4;
			var numberOfCheckBoxesForFA = 5;
			var cookie = readCookie('logged');
			
			if (cookie == 'fa'){
				numberOfCheckBoxes = numberOfCheckBoxesForFA;
			} 
			var isCheckAlllink  = (e > (numberOfCheckBoxes - 1)? true: false)

			e = $("#commType input:checked").length;
			isCheckAlllink ? $("a#showAll").addClass("disable") : $("a#showAll").removeClass("disable");

			if (e == 1) {

				$("#commType input:checked, #commType input:disabled").attr("disabled", "true").parent("li:first").append(toolCont).hover(
					function() {
						showTip($(this)); $(this).addClass("dropTrigger");
					},
					function() {
						hideTip();$(this).removeClass("dropTrigger");
					}
					);
			} else {
				$("#commType input:disabled").removeAttr('disabled').parent("li:first").unbind('mouseenter').unbind('mouseleave');
				$('#BD').attr('disabled', 'true');
			}
		}

		function showTip(e){
			$(".tip").fadeIn("fast");
		}

		function hideTip(){
			$(".tip").hide();
		}
	});
});

