/**
 * FAQ Collapsing question script
 */


jQuery(document).ready(function(){
	
	var faqQuestion = jQuery('.faq-question'),
		faqAnswer = jQuery('.faq-answer');
		
	faqAnswer.not(':first').hide();
	
	
	faqQuestion.click(function(event){
		event.stopPropagation();
		jQuery(event.target).parent().siblings().children().slideToggle();
		})	
	
});
jQuery(document).ready(function() {
	//billborad

	var billboardMenuItems = jQuery("#billboard-menu li"),
		billboardMenuSlides = jQuery("#billboard-slides li").hide();

	billboardMenuSlides.eq(0).show();
	billboardMenuItems.eq(0).addClass('silde-hover');

	//Launch the scroll every 4 seconds
	var intervalId = window.setInterval(slide,2000);

	function showAndHide (el) {

		billboardMenuItems.removeClass('silde-hover');
		el.addClass('silde-hover');
		billboardMenuSlides.hide();
		billboardMenuSlides.eq(el.index()).show();
	}


	billboardMenuItems.mouseenter(function() {
		showAndHide(jQuery(this));
		window.clearInterval(intervalId);
	})

	billboardMenuSlides.mouseenter(function() {
		window.clearInterval(intervalId);
	})

	billboardMenuItems.mouseleave(function() {
		intervalId = window.setInterval(slide,2000);
	})

	billboardMenuSlides.mouseleave(function() {
		intervalId = window.setInterval(slide,2000);
	})

/////// Auto-scrolling function
	function slide(){
		for (var i=0; i <billboardMenuItems.length; i++) {
			if (billboardMenuItems.eq(i).hasClass('silde-hover')){
				billboardMenuItems.eq(i).index()==3 ? showAndHide(billboardMenuItems.eq(0)) : showAndHide(billboardMenuItems.eq(i+1));
				return

			}
		}
	};


	//vars


	var com_popup = jQuery('#com-popup').hide(),
		overlay = jQuery('#overlay').hide(),
		links= com_popup.find('a'),
		selection = '',
		currSelection = '',
		currCookie,
		popupClose = jQuery('#popup-close'),
		allCookies = document.cookie.split(';'),
		commuTitle = jQuery('.community-name'),
		currProv = jQuery('#currProv'),
		menuComm= jQuery('#main-menu .menu-394').find('a'),
		accountLink = jQuery('.account-links'),
		jumpMenu = jQuery('#jumpMenu'),
		jobApply= jQuery('#job-apply'),
		formJobNumerValue = jQuery('#edit-submitted-position-'),
		jobNumber = jQuery ('.field-name-field-position .field-item'),
		commJSON = {                    // object containing the state for each community
		      'avondale'	: 'Arizona',
		      'carefree'	: 'Arizona',
		      'cave-creek'	: 'Arizona',
		      'glendale'	: 'Arizona',
		      'gold-canyon'	: 'Arizona',
		      'goodyear'	: 'Arizona',
		      'hereford'	: 'Arizona',
		      'huachuca-city'	: 'Arizona',
		      'litchfield-park'	: 'Arizona',
		      'rio-rico'		: 'Arizona',
		      'sierra-vista'	: 'Arizona',
		      'canyon-lake'	: 'Texas',
		      'conroe'		: 'Texas',
		      'flint'		: 'Texas',
		      'hawkins'		: 'Texas',
		      'tyler'		: 'Texas',
		      'branson'		: 'Missouri',
		      'de-soto'		: 'Missouri',
		      'kimberling-city'	: 'Missouri',
		      'sheridan'	: 'Illinois',
		      'general'	: 'Liberty Water',
		      'default'	: 'Customer Service',

		},
		commAcountLink = {                    // object containing the state for each community
		      'avondale'	: 'A8600',
		      'carefree'	: 'A8100',
		      'cave-creek'	: 'A8100',
		      'glendale'	: 'A8600',
		      'gold-canyon'	: 'A8200',
		      'goodyear'	: 'A8600',
		      'hereford'	: 'A8140',
		      'huachuca-city'	: 'A8137',
		      'litchfield-park'	: 'A8600',
		      'rio-rico'		: 'A8134',
		      'sierra-vista'	: 'A8500',
		      'canyon-lake'	: 'A8600',
		      'conroe'		: 'A8600',
		      'flint'		: 'A8112',
		      'hawkins'		: 'A8110',
		      'tyler'		: 'A8400',
		      'branson'		: 'A8118',
		      'de-soto'		: 'A8120',
		      'kimberling-city'	: 'A8122',
		      'sheridan'	: 'A8600',
		      'general'	: 'A8600',
		      'default'	: '00000',
		 };

jobApply.click(function(){
	var jobN = jobNumber.html().split(' ').join('');
	window.location.href = "http://libertywater.com/?q=career/inquier#" + jobN;


})

jQuery('#job-back').click(function(){

	window.location.href = "http://libertywater.com/?q=career";


})

if(window.location.hash) {
  formJobNumerValue.val(window.location.hash.replace("#",''))
  	.attr("disabled","disabled");
}
  	// get the community cookie
   for (var i=0; i<allCookies.length; i++){
		currCookie = allCookies[i].split('=');

		if (currCookie[0].replace(/^\s+|\s+$/g, '')==='lw-community') {
			currSelection = currCookie[1];
				}
   }

	// When the community page is loaded then redirect to community held in the cookie

	var currURL = document.URL;
	if ((currURL.indexOf("community")>-1 && currURL.indexOf("content")<0 )||  (currURL.indexOf("communities")>-1 && currURL.indexOf(currSelection)<0)){


				window.location.href = "http://libertywater.com/?q=communities/" + currSelection;

	}

	if ((currURL.indexOf("contact-us")>-1)){
	jQuery('#contact-us').hide();

	}
	// if cookie is empty then open the popup
	if(currSelection==""){
		overlay.fadeTo('slow',0.4,function(){
			com_popup.slideDown();

		})


		// on click check if any radio had been checked
		links.click(function(event){
			event.preventDefault();
			selection= jQuery(event.target).html().replace(/( +- *)|(- +)|( +)/g, '-').toLowerCase();

			// Then we send in the cookie

			document.cookie='lw-community=' + selection + '; expires= Thu, 2 Aug 2021 20:47:11 UTC; domain=.libertywater.com; path=/';
			com_popup.slideUp();
			overlay.fadeOut();
			window.location.reload();

		})

	}

	popupClose.click(function(){
		document.cookie='lw-community=default; expires= Thu, 2 Aug 2021 20:47:11 UTC; domain=libertywater.com; path=/';
		com_popup.slideUp();
			overlay.fadeOut();
			window.location.reload();
	})

	menuComm.click(function(event){

			event.preventDefault();
			window.location.href = "http://libertywater.com/?q=communities/" + currSelection;

	})
	//window.open('url to open','window name','attribute1,attribute2')
	accountLink.click(function(event){
			event.preventDefault();
			if (currSelection=="gold-canyon" || currSelection=="hereford" || currSelection=="flint" || currSelection=="tyler"
				|| currSelection=="sheridan" || currSelection=="canyon-lake" || currSelection=="conroe"  || currSelection=="default") {
				window.open("http://libertywater.com/?q=redirect-" + currSelection);
			}else{
			window.open("https://accounts.libertywater.com/" + commAcountLink[currSelection]);
			//alert( "https://accounts.libertywater.com/" + commAcountLink[currSelection]);
		}
	})

	jumpMenu.change(function(){

				document.cookie='lw-community=' + jumpMenu.val() + '; expires= Thu, 2 Aug 2021 20:47:11 UTC; domain=libertywater.com; path=/';
				if (document.URL.indexOf('communities')>-1){
					window.location.href = "http://libertywater.com/?q=communities/" + jumpMenu.val();
				}else{window.location.reload()}
	})

function capWords(str){
   var words = str.split(" ");
   for (var i=0 ; i < words.length ; i++){
      var testwd = words[i];
      var firLet = testwd.substr(0,1);
      var rest = testwd.substr(1, testwd.length -1)
      words[i] = firLet.toUpperCase() + rest
   }
   return( words.join(" "));
}

	function getProvinceFromCom (){


		if (currSelection == "default"){
			commuTitle.html(commJSON[currSelection]);
			currProv.html(commJSON[currSelection]);
		}else{
		var formatedProv = currSelection.split('-').join(' ');
		var	curString = capWords(formatedProv) + ', ' + commJSON[currSelection];
		commuTitle.html(curString);
		currProv.html(curString);
		}
	}

	getProvinceFromCom();

	var calcul_popup = jQuery('#cacul-popup').hide();

    jQuery('#cacul-popup-close').click(function(){
		jQuery('#cacul-popup').slideUp();
			jQuery('#overlay').fadeOut();
	})





});


function calculpopup(){
		var overlay = jQuery('#overlay');
		var cacul_popul = jQuery('#cacul-popup');

  		cacul_popul.load('http://libertywater.zerofootprint.net');

    	overlay.fadeTo('slow',0.5, function(){});
		cacul_popul.slideDown();

}


;

