/*======================================
MedBC

Flint Management Ltd
support@flintmanagement.com

js/global.js
global javascript functions
======================================*/

// Contenders, are you ready?
$(document).ready(function(){
		
	// Load and cycle header images
	HeaderCycle();
	
	// Load and cycle testimonials
	TestimonialCycle();
	
	// Apply Purdyforms to contact form
	$("#contact_form").Purdyforms({
		postURL: docroot + "system/ajax.php?function=contactform",
		
		// Optional parameters
		staticBorderColor: "#999999",
		highlightBorderColor: "#4C3594"
		
	},function(data){
		
		// Split the returned data
		splitdata = data.split("::");
		
		// Check for success
		if(splitdata[0] == "success"){
			alert(splitdata[1]);
		} else {
			
			// Was JSON returned
			try{
				$.parseJSON(data)
			}
			catch(e){
				// Show returned error
				alert(data);
			}
		}
	});
		
	// Hide the item content on page load
	$(".faq_list li .content").hide();
	
	// Check if hash has been put in url
	if(window.location.hash){
		// Toggle the item that's selected
		ToggleFAQItem(window.location.hash);
	}
	
	// Bind live click to the FAW item
	$(".faq_list li a").live("click", function(event){
		
		// Prevent default anchor behaviour
		event.preventDefault();
		
		// Toggle the selected item
		ToggleFAQItem($(this).attr("href"));
	});
});

/*===========================
ToggleFAQItem
Toggle showing and hiding the provided FAQ item and updating it's status icon
===========================*/
function ToggleFAQItem(id){
	
	// Set the item
	var item = $(id);
	
	// Check if the content is on show
	if(item.is(":hidden")){
		
		// Update the status icon
		$("a[href=" + id + "]").removeClass("faq_expand").addClass("faq_contract");
		
		// Prevent animation queue build up and slide content down
		item.stop().slideDown();
		
	// Item is on show	
	} else {
		// Update the status icon
		$("a[href=" + id + "]").removeClass("faq_contract").addClass("faq_expand");
		
		// Prevent animation queue build up and slide content up
		item.stop().slideUp();
	}
}

/*==========================================
TestimonialCycle
Load in JSON testimonials and cycle through
==========================================*/
function TestimonialCycle(){
	
	// Define default vars
	var speed           = 1000;
	
	// Timed interval
	setInterval(function(){
		
		// Set the vars 
		var active        = $(".slide_testimonial.show_testimonial"); // The active slide
		var position      = active.index(); // Position of the active slide
		var last_position = $(".slide_testimonial:last").index(); // Last position in the cycle
		
		active.removeClass("show_testimonial").fadeOut(speed, function(){
			
			// The cycle is not at the last position
			if(position < last_position){
				active.next(".slide_testimonial").addClass("show_testimonial").fadeIn(speed);
			// End of the cycle
			} else {
				$(".slide_testimonial:first").addClass("show_testimonial").fadeIn(speed);
			}
		});
					
	}, 8000);
}

/*==================================
HeaderCycle
Handle header image cycle 
Images pre-loaded into DOM using PHP
==================================*/
function HeaderCycle(){

	// get the details
	var current = $(".slide.show_header").attr("rel")*1; // currently active banner
	var imagecount = ($(".slide:last").index())+1; // Last banner available
	// next banner
	next = current+1;
	if(next > imagecount){
		next = 1;
	}
	// preload the next image
	$(".slide[rel='"+next+"']").html("<img src=\""+docroot+"images/banners/"+next+".png\">");
	
	
	// reapply supersleight to header images
	$(".slide img").supersleight();

	// Define default vars
	var speed      = 2000; // crossfade speed
	var cycle      = 6000; // time between changes
	
	// Timed interval
	setInterval(function(){
	
		// get the details
		var current = $(".slide.show_header").attr("rel")*1; // currently active banner
		var imagecount = ($(".slide:last").index())+1; // Last banner available
		// next banner
		next = current+1;
		if(next > imagecount){
			next = 1;
		}
		// banner to preload
		preload = next+1;
		if(preload > imagecount){
			preload = 1;
		}
				
		// fade out current slide
		$(".slide[rel='"+current+"']").removeClass("show_header").fadeOut(speed);
		
		// fade in the next slide
		$(".slide[rel='"+next+"']").addClass("show_header").fadeIn(speed);
		
		// reapply supersleight to header images
		$(".slide img").supersleight();
		
		// preload the next image (if needed)
		if(!$(".slide[rel='"+preload+"']").html()){
			$(".slide[rel='"+preload+"']").html("<img src=\""+docroot+"images/banners/"+preload+".png\">");
		}
		
	}, cycle);
}
