//*******************************************************************************************
//		GENERAL FUNCTIONS
//*******************************************************************************************

// Change HTML class to JS
document.documentElement.className =
     document.documentElement.className.replace(/\bno-js\b/,'js');

jQuery(document).ready(function($) {

// Function to swap two classes
$.fn.swapClass = function(c1, c2){
    return this.each(function(){
        var t = $(this);
        (!t.is('.'+c1)) ? 
            t.addClass(c1).removeClass(c2) : t.addClass(c2).removeClass(c1);
    });
}

// Fix IE not responding to change events on radio buttons:
// http://norman.walsh.name/2009/03/24/jQueryIE
$(function() {
    if ($.browser.msie) {
        $('input:radio').click(function () {
            this.blur();
            this.focus();
        });
    }
});
	
// New Windows
$('a.newwindow').bind('click',function(){
    window.open(this.href);
    return false;
});

// Close jQuery object
});


jQuery(document).ready(function($){
	if ( $('#map-canvas-wide').length ) {
		//###   Contact Us page - map for Carebase office   ###
		var mapPoint = new google.maps.LatLng(51.419871,-0.209084);
		map = new google.maps.Map(document.getElementById("map-canvas-wide"), {
			zoom: 15,
			maxZoom: 18,
			minZoom: 8,
			center: mapPoint,
			mapTypeControlOptions: {
				mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE],
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
			},
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		//Add Marker
		var markerPos = new google.maps.LatLng(51.419871,-0.209084);
		marker = new google.maps.Marker({
			map:map,
			draggable:false,
			animation: google.maps.Animation.DROP,
			position: markerPos
		});
	}



	if ($("a[rel^='popup-map']").length) {
		var lat = Array();
		var long = Array();
		$("a[rel^='popup-map']").each(function(index) {
			lat[index] = $(this).children(".latitude").html();
			long[index] = $(this).children(".longitude").html();
			$(this).prettyPhoto({
				custom_markup: '<div id="map-canvas'+index+'" style="width:600px; height:400px;"></div>',
				deeplinking: false,
				social_tools: '',
			    	theme: 'light_square',
				allow_resize: false,
				changepicturecallback: function(){initializeMap(long[index],lat[index],index);}
			});
		});

		function initializeMap(longitude, latitude, index) {
			var latlng = new google.maps.LatLng(latitude, longitude);
			var map = new google.maps.Map(document.getElementById("map-canvas"+index), {
				zoom: 13,
				maxZoom: 18,
				minZoom: 8,
				center: latlng,
				mapTypeControlOptions: {
					mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE],
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
				},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
			//Add Marker
			marker = new google.maps.Marker({
				map:map,
				draggable:false,
				animation: google.maps.Animation.DROP,
				position: latlng 
			});
		}
	}
});


//*******************************************************************************************
//		PRETTYPHOTO / HIDE FORMS
//*******************************************************************************************
jQuery(document).ready(function($){

	$('.pageWorkForUs #appForm').hide();
	$('.modal .optional h3').css("cursor","pointer");
	$('.modal .toggle').hide();
	
	// When dropdown clicked, hide or show the list of homes
	$('.modal .optional h3').click(function(e) {
			if($(this).next('.toggle').is(":visible")) {
				$(this).find('span').html('+');
				$(this).next('.toggle').slideUp('fast');
			}
			else {
				$(this).find('span').html('-');
				$(this).next('.toggle').slideDown('fast');
			}
		});  	

	//###############   PRETTYPHOTO, Zoom functionality throughout content   ###############
	$("a[rel^='prettyPhoto']")
		.each(function() {
			$(this).hover(function() {$(this).children('.zoom').fadeIn(250);}, function() {$(this).children('.zoom').fadeOut(250);});
		})
		.append('<img src="/themes/site_themes/carebase/img/zoom.png" alt="zoom" class="zoom" />')
		.prettyPhoto({
			showTitle: true,
			counter_separator_label: " of ",
			deeplinking: false,
	    	theme: 'light_square',			
			social_tools: ''
		});

	$("a[rel^='eventZoom']")
		.prettyPhoto({
			showTitle: true,
			counter_separator_label: " of ",
			deeplinking: false,
	    	theme: 'light_square',			
			social_tools: ''
		});

    $("a[rel^='modal']").prettyPhoto({
    	modal: true,
    	theme: 'light_square',
    	default_width: 520,
	default_height: 500,
	social_tools: '',
	show_title: false,
	deeplinking: false,
			markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_content_container"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<div id="pp_full_res"></div> \
										<div class="pp_details"> \
											<a class="pp_close" href="#">Close</a> \
										</div> \
									</div> \
								</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>'
    });
});


//*******************************************************************************************
//		TOGGLES: SHOW ALL HOMES
//*******************************************************************************************
jQuery(document).ready(function($){
	$('#homesroller').hide();
	$('#allhomes > h3').toggle(
    	function(e){
			e.preventDefault();
			$(this).find('a').html('Hide links to all our care homes<i></i>').blur();
			$(this).find('a > i').addClass('up');
			$('#homesroller').slideDown('fast');
    	},
    	function(e) {
			e.preventDefault();
			$(this).find('a').html('Show links to all our care homes<i></i>').blur();			
			$(this).find('a > i').removeClass('up');
			$('#homesroller').slideUp('fast');
    	}
    );
});

//*******************************************************************************************
//		TOGGLES: TEXT SIZE
//*******************************************************************************************
jQuery(document).ready(function($){
	$('#textsize ul').hide();
	$('#textsize > a').click(function(e) {
		if($('#textsize ul').is(":visible")) {
			$(this).blur(); e.preventDefault();
			$('#textsize ul').slideUp('fast');
		} else {
			$(this).blur(); e.preventDefault();
			$('#textsize ul').slideDown('fast');
		}
    		e.stopPropagation(); e.preventDefault();			
	});
});

//*******************************************************************************************
//		TOGGLES: MORE TEXT IN BODY COPY
//*******************************************************************************************
jQuery(document).ready(function($){

	if ( ($(".showtext").length) && ($(".moretext").length)) {
		// Hide all answers & show that headings are clickable
		$('.showtext').show();
		$('.moretext').hide();

		// Individual FAQ toggles
		$('.showtext').click(function(e) {
	    	e.stopPropagation(); e.preventDefault();
			if( $(this).parent().next('.moretext').is(":hidden") ){$(this).parent().next('.moretext').slideDown('fast');} else {$(this).parent().next('.moretext').slideUp('fast');}
   		});
   	}
});

//*******************************************************************************************
//		TOGGLES: MORE RESULTS
//*******************************************************************************************
jQuery(document).ready(function($){

	var resPanel = $('#more-results');
	
	resPanel.hide();
	$('#more-results-button').click(function(e) {
			if(resPanel.is(":visible")) {
				$(this).blur(); e.preventDefault();
				resPanel.slideUp('fast');
			}
			else {
				$(this).blur(); e.preventDefault();
				resPanel.slideDown('fast');
			}
    	e.stopPropagation(); e.preventDefault();			
		}); 	


});


//*******************************************************************************************
//		TOGGLES: FIND A HOME (SIDEBAR)
//*******************************************************************************************
jQuery(document).ready(function($){
	$('#sidebar #listofhomes').hide();
	$('#sidebar #selecthome h3').toggle(
    	function(e){
			$(this).blur(); e.preventDefault();
			$('#sidebar #selecthome').addClass('open');
			$('#sidebar #listofhomes').slideDown('fast');
    	},
    	function(e) {
			$(this).blur(); e.preventDefault();
			$('#sidebar #selecthome').removeClass('open');
			$('#sidebar #listofhomes').slideUp('fast');
    	}
    );
});

//*******************************************************************************************
//		TOGGLES: PLANNER
//*******************************************************************************************
jQuery(document).ready(function($){

	function planToggle(ID) {
		if( ID.is(":hidden") ){
			ID.parent().addClass('more');
			ID.slideDown('fast');
   		} else {
			ID.slideUp('fast');
			ID.parent().removeClass('more');
   		}
	ID.blur(); return false;
	}


	$('#planner .reveal').hide();
	$('#planner .panel .button').click(function(){planToggle($(this).next('.reveal'));});
	
	var tabContainers = $('#planner > .panel');

    $('#planner ol a').click(function () {
        $(this).blur();
        tabContainers.hide().filter(this.hash).fadeIn('fast');
        tabContainers.filter(this.hash).find('.reveal').each(function(){
			// Expand the hidden panel after a delay
			elem = $(this);
			setTimeout(function(){
				if (elem.is(":hidden")){
					elem.parent().addClass('more');
					elem.slideDown('fast');
    	    	}
    	    },3000);
        });
        $('#planner li').removeClass('selected');
        $(this).parent().addClass('selected');
        return false;
    }).filter(':first').click();	
	
});

//*******************************************************************************************
//		TOGGLES: FIND A HOME (WIDE VERSION ABOVE CAROUSELS)
//*******************************************************************************************
jQuery(document).ready(function($){

	// Store refs to the two elements
	var $sh_select = $('#findacarehome.wide #selecthome');
	var $sh_list = $('#findacarehome.wide #listofhomes');

	// Define hiding/showing functions
	function hide_find_home() {
			$sh_select.removeClass('open');
			$sh_list.slideUp('fast');
	}
	function show_find_home() {
			$sh_select.addClass('open');
			$sh_list.slideDown('fast');
		}

	// Hide the dropdown on page load
	$sh_list.hide();
	
	// When dropdown clicked, hide or show the list of homes
	$('#findacarehome.wide #selecthome h3').click(function(e) {
			if($sh_list.is(":visible")) {hide_find_home();}
			else {show_find_home();}
    	e.stopPropagation(); e.preventDefault();			
		});    	

// Hide the list of homes when the page is clicked outside the list
$('body, #slidecontrol a,#tablinks a').click(function() {
	if($sh_list.is(":visible")) {hide_find_home();}
});
// Hide the list of homes when the search field is selected
$('#findacarehome.wide #findhome input[type=text]').focus(function() {
	if($sh_list.is(":visible")) {hide_find_home();}	
})
// Allow clicks on the list of homes
$sh_list.click(function(e) {e.stopPropagation();});

});


//*******************************************************************************************
//	CAREBASE LIFE
//*******************************************************************************************
jQuery(document).ready(function($){

	var lifeContainers = $('.lifeEntry');
	lifeContainers.hide().filter(':first').show();

    $('#gallery a:not(.button)').click(function () {
        lifeContainers.hide().filter(this.hash).fadeIn('fast');
        $('#gallery li').removeClass('selected');
        $(this).parent().addClass('selected');
        return false;
    });

});

//*******************************************************************************************
//	DOUBLE VIDEO
//*******************************************************************************************
jQuery(document).ready(function($){

	var vidContainers = $('.video');
	vidContainers.hide().filter(':first').show();

    $('.videointro.double a').click(function () {
        vidContainers.hide().filter(this.hash).fadeIn('fast');
        $('.videointro.double div').removeClass('selected');
        $(this).parent().addClass('selected');
        return false;
    });

});

//*******************************************************************************************
//		MANAGE ANCHORS AND HASH CHANGES
//*******************************************************************************************
jQuery(document).ready(function($){

	// Add smooth anchor scroll for specified links
	// Load the plugins
	// createScript("jquery.scrollTo-142");
	// createScript("jquery.localscroll-127");
		
	/*
	$('#wrapper').localScroll({
		stop: true,
		lock: true,
		hash: false,
		filter: '#tablinks a'
	});	
	*/
	
});

//*******************************************************************************************
//		TOGGLES: WORK FOR US
//*******************************************************************************************

jQuery(document).ready(function($){

	// Create Togglers
	$('.vacancy').each(function(index) {
		var jobdesc = $(this).find('b').html();
		$('<a href="#" class="showmore">Job Description for "' + jobdesc + '"</a>').insertBefore($(this).find('.morecontent'));
	});	

	// Hide all job descriptions
	$('.morecontent').hide();
	
	// Individual toggles
	$('.showmore').click(function(){
		if( $(this).next('.morecontent').is(":hidden") ){$(this).next('.morecontent').slideDown('fast');} else {$(this).next('.morecontent').slideUp('fast');}
	$(this).blur(); return false;
	// e.preventDefault();
   	});
});

//*******************************************************************************************
//		TOGGLES: FAQ
//*******************************************************************************************

jQuery(document).ready(function($){

	if ( $(".faqgroup").length ) {
		// Hide all answers & show that headings are clickable
		$('.answer').hide();
		$('.faqgroup h3').css("cursor","pointer");

		// Individual FAQ toggles
		$('.faqgroup h3').click(function() {
			if( $(this).next('.answer').is(":hidden") ){$(this).next('.answer').slideDown('fast');} else {$(this).next('.answer').slideUp('fast');}
   		});

	// Show / Hide all toggles
	// Only insert togglers if JS enabled:
	$('<a class="showall button">+ Show all</a>').insertBefore('.faqgroup:first');
	$('<a class="hideall button" style="display:none;">- Hide all</a>').insertBefore('.faqgroup:first');		
        $('.hideall').click(function () {
        	$(this).hide();
        	$('.showall').show();
			$('.faqgroup h3').each(function(index) {$(this).next('.answer').slideUp('fast');});
		});
        $('.showall').click(function () {
        	$(this).hide();
        	$('.hideall').show();
			$('.faqgroup h3').each(function(index) {$(this).next('.answer').slideDown('fast');});
		});
		
	// If URL has #hash, 'click' the H3 (expands the FAQ selected)
	var queryhash = window.location.hash;
	if (queryhash) {
		var selector = queryhash + ' h3';
		// alert (selector);
		$(selector).triggerHandler('click');
	}

	}
});

//*******************************************************************************************
//		CYCLERS / SLIDESHOWS / CAROUSELS
//*******************************************************************************************

jQuery(document).ready(function($){
	if ( $('#slides').length ) {
		// Setup cycler on carousel
		$('#slides').cycle({
			fx:			'scrollHorz',
			speed:		600,
			timeout:	8000,
			next:		'#next',
			prev:		'#prev',
			pause:		1,
			autostop:	0,
			requeueOnImageNotLoaded: true,
			requeueTimeout: 250
		});
	}

	if ( $('#ticker').length ) {
		//setup cycler on ticker
		$('#ticker .scroller').cycle({
			fx:			'scrollLeft',
			speed:		1000,
			timeout:	5000,
			pause:		1,
			autostop:	0
		});
	}
	
	// Insert Prev/Next arrows
		$('<div class="prevnext"><span class="prev">Prev<i></i></span><span class="next">Next<i></i></span><span class="play">Play<i></i></span><span class="pause">Pause<i></i></span><span class="playmsg"></span></div>').insertAfter('.pageOurhomes #findhome');	

	if ( $('.homes').length ) {
		$(".homes").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			circular: true,
			auto: true,
			timeout: 5000,
			speed: 600
	        /* pauseOnHover: true */
		});
		
        $('.pause').click(function () {
        	$(".homes").trigger("pauseCarousel");
        	$(this).hide();	$('.play').show();
        	$('.playmsg').text('Paused');
        });
		
        $('.play').click(function () {
        	$(".homes").trigger("resumeCarousel");
        	$(this).hide(); $('.pause').show();
        	$('.playmsg').text('');        	
        });        
		
	}

	if ( $('.wtintro').length ) { // console.log("found it");
		$("a[rel^='walkthrough']").prettyPhoto({
			modal: false,
			theme: 'light_square',
			default_width: 940,
			default_height: 600,
			social_tools: '',
			show_title: false,
			deeplinking: false,
			keyboard_shortcuts: false,
			iframe_markup: '<iframe style="" src ="/pages/walkthrough" width="940" height="600" frameborder="no" scrolling="no" marginwidth="0" marginheight="0"></iframe>',
				markup: '<div id="wtmodal" class="pp_pic_holder"> \ <div class="pp_content_container"> \ <div class="pp_content"> \ <div class="pp_loaderIcon"></div> \ <div class="pp_fade"> \ <div id="pp_full_res"></div> \ <div class="pp_details"> \ <a class="pp_close" href="#">Close</a> \ </div> \ </div> \ </div> \ </div> \ </div> \ <div class="pp_overlay"></div>'
		});
	}
});

//*******************************************************************************************
//	TABS
//*******************************************************************************************
jQuery(document).ready(function($){

	var tabContainers = $('#hometabs > .tab');

    $('#tablinks a').click(function () {
        tabContainers.hide().filter(this.hash).fadeIn('fast');
        $('#tablinks li').removeClass('selected');
        $(this).parent().addClass('selected');

	if (tabContainers.filter(this.hash).find("#map-canvas-home").length) {
		if ( $('#map-canvas-home div').length == 0) {
			var lat = $('#map-canvas-home').children(".latitude").html();
			var long = $('#map-canvas-home').children(".longitude").html()

			var mapPoint = new google.maps.LatLng(lat,long);
			map = new google.maps.Map(document.getElementById("map-canvas-home"), {
				zoom: 13,
				maxZoom: 18,
				minZoom: 8,
				center: mapPoint,
				mapTypeControlOptions: {
					mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE],
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
				},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
			//Add Marker
			marker = new google.maps.Marker({
				map:map,
				draggable:false,
				animation: google.maps.Animation.DROP,
				position: mapPoint
			});
		}
	}
        return false;
    }).filter(':first').click();
//$("#hometabs").find(".tab:not(:first)").delay(5000).hide();

});
