// JavaScript Document

$(document).ready(function() {
	textEnlarger();
	tabbedMenu();
	footerWidthCalculator();
	rateThisPageOver();
	checkCookie();
	ccptonloadSearch();
	postSearchParameters();
	postSearchMail();
	communityPathWay();
});
function getCookie(c_name)
{
if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
		    c_start=c_start + c_name.length+1 ;
		    c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length
	    return unescape(document.cookie.substring(c_start,c_end));
    	} 
  }
	return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
var cookies = document.cookie.split(";");
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString()) + ";path=/";
}

function checkCookie()
{
fontZoom=getCookie('fontZoom');
if (fontZoom!=null && fontZoom!="")
  {
	if(fontZoom == 110)
	  {
		$("#outerContainer").css("font-size","110%");
		$("a.style11").removeClass("selected");
		$("a.style14").addClass("selected");
		$("a.style17").removeClass("selected");
		$(".leftContent .topStory .topStoryContainer").height(225);
		$(".leftContent .teaserSection li div.teaserContent").attr("style", "top:-3px; height:145px;");
		if($("ul.channels li:first a").attr("class")=="selected"){
			$("ul.topShortcutLinks").attr("style","width:515px; margin-top:0px;");
		}
		else{
			$("ul.topShortcutLinks").removeAttr("style");
		}
	  }
	if(fontZoom == 120)
	  {
		$("#outerContainer").css("font-size","120%");
		$("a.style11").removeClass("selected");
		$("a.style14").removeClass("selected");
		$("a.style17").addClass("selected");
		$(".leftContent .topStory .topStoryContainer").height(285);
		$(".leftContent .teaserSection li div.teaserContent").attr("style", "top:-3px; height:165px;");
		if($("ul.channels li:first a").attr("class")=="selected"){
			$("ul.topShortcutLinks").attr("style","margin-left:-15px; width:545px; margin-top:0px;");
		}
		else{
			$("ul.topShortcutLinks").attr("style","margin-left:-15px;");
		}
	  }

  }
else 
  {
    setCookie('fontZoom',100,365); 
  }
} 

function textEnlarger(){
	$("a.style11").addClass("selected");
	$("a.style11").click(function(){
		$("#outerContainer").css("font-size","100%");
		$("a.style11").addClass("selected");
		$("a.style14").removeClass("selected");
		$("a.style17").removeClass("selected");
		$(".leftContent .topStory .topStoryContainer").height(200);
		$(".leftContent .teaserSection li div.teaserContent").attr("style", "top:0; height:127px;");
		$("ul.topShortcutLinks").removeAttr("style","margin-left:-15px;");
		setCookie('fontZoom',100,365);
		return false;
	});
	$("a.style14").click(function(){
		$("#outerContainer").css("font-size","110%");
		$("a.style11").removeClass("selected");
		$("a.style14").addClass("selected");
		$("a.style17").removeClass("selected");
		$(".leftContent .topStory .topStoryContainer").height(225);
		$(".leftContent .teaserSection li div.teaserContent").attr("style", "top:-3px; height:145px;");
		$("ul.topShortcutLinks").removeAttr("style","margin-left:-15px;");
		setCookie('fontZoom',110,365);
		return false;
	});
	$("a.style17").click(function(){
		$("#outerContainer").css("font-size","120%");
		$("a.style11").removeClass("selected");
		$("a.style14").removeClass("selected");
		$("a.style17").addClass("selected");
		$(".leftContent .topStory .topStoryContainer").height(285);
		$(".leftContent .teaserSection li div.teaserContent").attr("style", "top:-3px; height:165px;");
		$("ul.topShortcutLinks").attr("style","margin-left:-15px;");
		setCookie('fontZoom',120,365);
		return false;
	});	
}

function tabbedMenu(){
	$(".tabbedMenu ul li:first a.tabs").addClass("selected");
	$(".tabbedMenu .tabbedContainerContent").hide();
	$(".tabbedMenu .tabbedContainerContent:first").show();
	$("a#news").click(function(){
		$(".tabbedMenu ul li a.tabs").removeClass("selected");
		$(this).addClass("selected");
		$(".tabbedMenu .tabbedContainerContent").hide();
		$(".tabbedMenu #newsContainer").show();
	});
	$("a#features").click(function(){
		$(".tabbedMenu ul li a.tabs").removeClass("selected");
		$(this).addClass("selected");
		$(".tabbedMenu .tabbedContainerContent").hide();
		$(".tabbedMenu #featureContainer").show();
	});
	$("a#faq").click(function(){
		$(".tabbedMenu ul li a.tabs").removeClass("selected");
		$(this).addClass("selected");
		$(".tabbedMenu .tabbedContainerContent").hide();
		$(".tabbedMenu #faqContainer").show();
	});
}

function footerWidthCalculator(){
	var totalSectionCount = $(".footerSection div").length;
	var widthCounter = (900/totalSectionCount)-20;
	$(".footerSection div").width(widthCounter);
	$("div.topStory div.whatSaving ul:last").attr("style", "border:none");
}


function rateThisPageOver(){
	$("#fRadio1opt1").mouseover(function(){
		$("div#counter span.textCounter").text("1");
	});
	$("#fRadio1opt2").mouseover(function(){
		$("div#counter span.textCounter").text("2");
	});
	$("#fRadio1opt3").mouseover(function(){
		$("div#counter span.textCounter").text("3");
	});
	$("#fRadio1opt4").mouseover(function(){
		$("div#counter span.textCounter").text("4");
	});
	$("#fRadio1opt5").mouseover(function(){
		$("div#counter span.textCounter").text("5");
	});
}
function rightModuleBorder(){
	$(".rightContent .rightSection:last").addClass("rightSectionLast");
}


/*============================================================================================

				**************** BEGIN Community pathways ****************

=============================================================================================== */
        
	function postSearchParameters(){
		$("#projectTypeFormFilter").click(function(){
			window.scroll(600,600);
			$(".loading").ajaxStart(function(){
				$(this).show();
				$(this).css({
					"opacity": "0.5"
				});
			});
			$(".loading").ajaxStop(function(){
  				$(this).hide();
			});
			var strForm1 = $("#documentFinder").serialize();
       		//alert("hello");
			$.ajax({
				type: "POST",
				url: "/extension/cafe/design/cafe/javascript/ProcessSearchResult.php",
				//url: "http://10.210.6.58/index.php/cafeproject/search",
				data: strForm1 ,
				cache: false,
				success: function(html){
					$("#tabResultDocSearch1").html(html);
					showHideResultsTab();
                                        clickButton();
				}
			});
			return false;
		});
	}
	function postSearchMail(){
		$("#MailResult").click(function(){
                        window.scroll(600,600);
			$(".sendingEmail").ajaxStart(function(){
				$(this).show();
			});
			$(".sendingEmail").ajaxStop(function(){
  				$(this).hide();
			});
			var strForm2 = $("#emailform").serialize();
			$.ajax({
				type: "POST",
				url: "/cafe/cafeproject/mail",
				data: strForm2 ,
				cache: false,
				success: function(html1){
                    $("#thankYou").html(html1);
					$("#LinkResult").attr("href",$("#ResultUrl").val());
					$("#emailForm").hide();
					$("#thankYou").show();
					showHideResultsTab();
				}
			});
    		return false;
		});
	}
	function checkValue( name ){
            	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
                if( results == null )
			return "";
		else
			return results[1];
	}
	function ccptonloadSearch(){
		var flag=0;
		var currentURL=document.location.toString();
		var selectedURLIndex = currentURL.indexOf("&");
		var selectedURLIndexmet = currentURL.indexOf("=");
		if(selectedURLIndex != -1) {
			var selectedURL = currentURL.substring(selectedURLIndex,selectedURLIndexmet+1);
			document.documentFinder.slider_position.value = selectedURL;
		}
		var selectedURLPage = currentURL.indexOf("page=");
                var page_no = currentURL.substring(selectedURLPage+6,selectedURLPage);
		
              if(checkValue('p0')){
            		$('input[name="projecttype[]"]')[0].checked = true;
			flag = 1;
		}
		if(checkValue('p1')){
			$('input[name="projecttype[]"]')[1].checked = true;
			flag = 1;
		}
		if(checkValue('p2')){
			$('input[name="projecttype[]"]')[2].checked = true;
			flag = 1;
		}
		if(checkValue('p3')){
			$('input[name="projecttype[]"]')[3].checked = true;
			flag = 1;
		}

               /* To show hide Search By criteria starts here */
		if(flag==0){
			
			$('input[name="projecttype[]"]')[0].checked = true;
			$('input[name="projecttype[]"]')[1].checked = true;
			$('input[name="projecttype[]"]')[2].checked = true;
			$('input[name="projecttype[]"]')[3].checked = true;
		}
        	window.scroll(600,600);
		$(".loading").ajaxStart(function(){
             		$(this).show();
			$(this).css({
				"opacity": "0.5"
			});
		});
		$(".loading").ajaxStop(function(){
  			$(this).hide();
		});
	
		var strForm1 = $("#documentFinder").serialize();
		$.ajax({
			type: "POST",
			url: "/extension/cafe/design/cafe/javascript/ProcessSearchResult.php",
              	         //url: "http://10.210.6.58/index.php/cafeproject/search",
			data: strForm1 ,
			async: true,
			cache: false,
			success: function(html){
				$("#tabResultDocSearch1").html(html);
				showHideResultsTab();
				var currentURL=document.location.toString();
				var selectedURLIndex = currentURL.indexOf("&");
				var selectedURLIndexmet = currentURL.indexOf("=");
				if(selectedURLIndex != -1) {
					var selectedURL = currentURL.substring(selectedURLIndex,selectedURLIndexmet+1);
					if(selectedURL == 0){
				$(".docTypeSlider .background").width(0);
				$(".ui-slider-handle").attr("style","left:0");
				$("#docStage_0").val("Show all");
                	}
			if(selectedURL == 1){
				$(".docTypeSlider .background").width(60);
				$(".ui-slider-handle").attr("style","left:14.2857% !important;");
				$("#docStage_0").val("Reset");
			  }
			if(selectedURL == 2){
				$(".docTypeSlider .background").width(120);
				$(".ui-slider-handle").attr("style","left:28.5714%;");
				$("#docStage_0").val("Reset");
			  }
			if(selectedURL == 3){
				$(".docTypeSlider .background").width(180);
				$(".ui-slider-handle").attr("style","left:42.8571%;");
				$("#docStage_0").val("Reset");
			  }
			if(selectedURL == 4){
				$(".docTypeSlider .background").width(240);
				$(".ui-slider-handle").attr("style","left:57.1429% !important;");
				$("#docStage_0").val("Reset");
			  }
			if(selectedURL == 5){
				$(".docTypeSlider .background").width(300);
				$(".ui-slider-handle").attr("style","left:71.4286%;");
				$("#docStage_0").val("Reset");
			  }
			if(selectedURL == 6){
				$(".docTypeSlider .background").width(360);
				$(".ui-slider-handle").attr("style","left:85.7143%;");
				$("#docStage_0").val("Reset");
			}
			if(selectedURL == 7){
				$(".docTypeSlider .background").width(420);
				$(".ui-slider-handle").attr("style","left:100%;");
				$("#docStage_0").val("Reset");
		  	}
				}
				clickButton();
			}
		});
		return false;
		/* various function for search result ends here */
	}
function clickButton(){
	$(".dataTables_paginate a").click(function(){
		var strForm1 = $("#documentFinder").serialize();
		var currentPage = $(this);
		var currentPageHref = $(this).attr("rel");
                var currentTabValue= currentPage.parents(".tabResultDocSearch").attr("id");
		window.scroll(400,400);
		$.ajax({
			type: "POST",
			url: "/extension/cafe/design/cafe/javascript/ProcessSearchResult.php"+currentPageHref+"&tab_value="+currentTabValue,
              	data: strForm1,
			async: true,
			cache: false,
			success: function(html){
				$("#tabResultDocSearch1").html(html);
				showHideResultsTab();
				clickButton();
				$(".tabResultDocSearch").hide();
                                $(".tabDocSearch a").removeClass("selected");
				$("#"+currentPage.parents(".tabResultDocSearch").attr("id")+"Doc").addClass("selected");
				$("."+currentPage.parents(".tabResultDocSearch").attr("id")+"Doc").show();
			}
		});
		return false;
	});
}

function communityPathWay() {
	$("#slider").slider( { max: 7 } );
	$("#slider").slider( { values: [0] } );
	var values = $( "#slider" ).slider( "option", "values" );
	document.documentFinder.slider_position.value = values;

	$( "#slider" ).bind( "slidechange", function() {
		if(values == 0){
			$(".docTypeSlider .background").width(0);
			$(".ui-slider-handle").attr("style","left:0");
			$("#docStage_0").val("Show all");
                }
		if(values == 1){
			$(".docTypeSlider .background").width(60);
			$(".ui-slider-handle").attr("style","left:14.2857% !important;");
			$("#docStage_0").val("Reset");
		  }
		if(values == 2){
			$(".docTypeSlider .background").width(120);
			$(".ui-slider-handle").attr("style","left:28.5714%;");
			$("#docStage_0").val("Reset");
		  }
		if(values == 3){
			$(".docTypeSlider .background").width(180);
			$(".ui-slider-handle").attr("style","left:42.8571%;");
			$("#docStage_0").val("Reset");
		  }
		if(values == 4){
			$(".docTypeSlider .background").width(240);
			$(".ui-slider-handle").attr("style","left:57.1429% !important;");
			$("#docStage_0").val("Reset");
		  }
		if(values == 5){
			$(".docTypeSlider .background").width(300);
			$(".ui-slider-handle").attr("style","left:71.4286%;");
			$("#docStage_0").val("Reset");
		  }
		if(values == 6){
			$(".docTypeSlider .background").width(360);
			$(".ui-slider-handle").attr("style","left:85.7143%;");
			$("#docStage_0").val("Reset");
		
                }
		if(values == 7){
			$(".docTypeSlider .background").width(420);
			$(".ui-slider-handle").attr("style","left:100%;");
			$("#docStage_0").val("Reset");
		  }
                document.documentFinder.slider_position.value = values;
	});
	$("#docStage_0").click(function(){
		if(values != 0){
			$("#docStage_0").val("Show all");
			$( ".selector" ).slider( "option", "values", 0 );
			$(".ui-slider-handle").attr("style","left:0");
			$(".docTypeSlider .background").width(0);
			document.documentFinder.slider_position.value = 0;
		}
		return false;
	});

	$("#projectTypeFormReset").click(function(){
		$(".projectTypeList input").attr("checked", "checked");
		$(".projectTypeList label").removeAttr("style");
		$("#docStage_0").val("Show all");
		$( ".selector" ).slider( "option", "value", 0 );
		$(".ui-slider-handle").attr("style","left:0");
		$(".docTypeSlider .background").width(0);
		document.documentFinder.slider_position.value = 0;
		return false;
	});
	$(".projectTypeList input").click(function(){
		var currentStatusProjectList = $(this).attr("checked");
		if(!currentStatusProjectList){
			$(this).parent("label").attr("style", "background-color:#EBEBEB;");
		}
		else{
			$(this).parent("label").removeAttr("style");
		}
	});
	
	$(".sliderLabels div a").click(function(){
		$(".infoBubble").hide();
		$(this).next(".infoBubble").attr("style","display:block; left:-39px; top:-145px;");
		return false;
	});
	
	$('.hideLink').toggle (
        function(){
			$(this).parents(".curveBoxContent").children(".showHideContent").hide();
			$(this).parents(".curveBox").children(".showHideContent").hide();
			$(this).text("Show");
			$(this).attr("style","background:transparent url(/extension/cafe/design/cafe/images/communities/btn_plusShow.gif) no-repeat scroll right top;");	
                },
        function(){   
			$(this).parents(".curveBoxContent").children(".showHideContent").show(); 
			$(this).parents(".curveBox").children(".showHideContent").show();
               	$(this).text("Hide");
			$(this).attr("style","background:transparent url(/extension/cafe/design/cafe/images/communities/btn_minusHide.gif) no-repeat scroll right top;");
		  });
}
function showHideResultsTab(){
	
	$(".tabResultDocSearch").removeClass("showResultMe");
	$(".tabResultDocSearch:first").removeClass("showResultMe");
	$(".tabDocSearch a").click(function(){
		$(".tabResultDocSearch").removeClass("showResultMe");	
		var currentResultShow = $("."+$(this).attr("id"));
		if(currentResultShow.children("p.error").length < 1){
			$(".tabResultDocSearch").hide();
			$(".tabDocSearch a").removeClass("selected");
			$(this).addClass("selected");
			currentResultShow.show();
			currentResultShow.addClass("showResultMe");
		}
		else{
			return false;
		}
		return false;
	});
	$(".allTabDoc span.emailResult").click(function(){
		$(".printResult").hide();
		$("#allTabDocPrint").show();
	});
	$(".fundinginfoTabDoc span.emailResult").click(function(){
		$(".printResult").hide();
		$("#fundinginfoTabDocPrint").show();
	});
	$(".generalInfoTabDoc span.emailResult").click(function(){
		$(".printResult").hide();
		$("#generalInfoTabDocPrint").show();
	});
	$(".TraingTabDoc span.emailResult").click(function(){
		$(".printResult").hide();
		$("#TraingTabDocPrint").show();
	});
	$(".CaseStudiesTabDoc span.emailResult").click(function(){
		$(".printResult").hide();
		$("#CaseStudiesTabDocPrint").show();
	});
	$(".HowToGuidesTabDoc span.emailResult").click(function(){
		$(".HowToGuidesTabDoc").hide();
		$("#allTabDocPrint").show();
	});


	



	$("div.emailPrint span:last").click(function(){
		window.print();
	});
	
	

	var popupStatus = 0;
	function disablePopup(){
		if(popupStatus==1){
			$("#backgroundPopup").fadeOut("slow");
			$("#emailLightBox").fadeOut();
			$("#sendmail").val("");
			$("#frommail").val("");
			$("#message").val("");
			popupStatus = 0;		
		}
	}
	function loadPopup(){
		if(popupStatus==0){
			$("#backgroundPopup").css({
				"opacity": "0.7"
			});
			$("#backgroundPopup").fadeIn("slow");
			$("#emailLightBox").fadeIn("slow");
			window.scroll(750,750);
			popupStatus = 1;
		}
	}
	//centering popup
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var sendEmailHeight = $(".sendEmail").height();
	var sendEmailWidth = $(".sendEmail").width();
	$("sendEmail").css({
		"left": windowWidth/2-sendEmailHeight/2-300,
		"top": windowHeight/2-sendEmailWidth/2-200
	});

        

       


	$("div.emailPrint span:first").click(function(){
            	loadPopup();
		$("#thankYou").hide();
		$("#emailForm").show();
		$("#popupContactClose").click(function(){
			disablePopup();
			return false;
		});
		
		$("#backgroundPopup").click(function(){
			disablePopup();
			return false;
		});
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});	
		return false;
	});

}


/*============================================================================================

				**************** END Community pathways ****************

=============================================================================================== */




function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}
