$(document).ready(function() {
    var CurID = 1;
    
    $('.intro-0').show();
    
   // $("body").attr("id", "bg-home-1");
    $.getJSON('/wp-content/themes/fylde-coast/ajax/block.php', {theID : 0}, function(data) {
  		$("h2", ".intro-blk-bg").html(data.title);
    	$("p", ".intro-blk-bg").html(data.content);
	});
    
    $('.scroll').click(function(){
        var ClickID = $(this).attr('rel');
        ChangeFocus(ClickID);
        return false;
    });
    
    function ChangeFocus(ID)
    {
        $('.scroll').removeClass('active');        
        $('.item-'+ID).addClass('active');
        $("body").attr("id", "bg-home-" + ID);
        $('.intro-'+ID).show();
        
        $.getJSON('/wp-content/themes/fylde-coast/ajax/block.php', {theID : ID}, function(data) {
        	        
  			$("h2", ".intro-blk-bg").html(data.title);
    		$("p", ".intro-blk-bg").html(data.content);
		});
        
        CurID = ID;
    }
    
    $("a", ".carousel").click(function(){
    	
    	var id = parseInt($(this).attr("id"));
    	    	
    	$.getJSON('/wp-content/themes/fylde-coast/ajax/block.php', {theID : id}, function(data) {
  			$("h2", ".intro-blk-bg").html(data.title);
    		$("p", ".intro-blk-bg").html(data.content);
		});
    	
    	
    });
    
    
    $("#background").fadeIn();
    
    $("#1").click(function(){
    	$(".background").fadeOut();
    	$("#background2").fadeIn();
    	return false;
    });
    $("#2").click(function(){
    	$(".background").fadeOut();
    	$("#background3").fadeIn();
    	return false;
    });
    $("#3").click(function(){
    	$(".background").fadeOut();
    	$("#background4").fadeIn();
    	return false;
    });
    $("#4").click(function(){
    	$(".background").fadeOut();
    	$("#background5").fadeIn();
    	return false;
    });
    $("#5").click(function(){
    	$(".background").fadeOut();
    	$("#background6").fadeIn();
    	return false;
    });
    $("#6").click(function(){
    	$(".background").fadeOut();
    	$("#background7").fadeIn();
    	return false;
    });
    
    /* Open external links in new window */
    $("a[rel*=external]").click(function(){
    	var href = $(this).attr("href");
    	window.open(href);
    	return false;
    });
    
    
    // rounded corners on all content images
	$("img", ".col-a").wrap('<div class="img">').after('<span class="tr"></span><span class="tl"></span><span class="br"></span><span class="bl"></span>');
	
	$("a", "#contact #left-nav").click(function(){

		$("li", "#left-nav").each(function(){
			if($(this).hasClass('current_page_item')){
				$(this).removeClass("current_page_item");
			}
		});
		
		var title 	= $("h3", ".col-a");
		var address	= $("address", ".col-a");
		title.html($(this).attr("title"));
		$("#the_copy").html('<img src="/wp-content/themes/fylde-coast/images/loading.gif" alt="Loading" width="220" height="19" />');
		
		var li = $(this).parent().attr("class");
		var id = li.replace('page_item page-item-', '');
		
		$(this).parent().addClass("current_page_item");
		
		$.get('/wp-content/themes/fylde-coast/ajax/contact.php', {theid : id}, function(data) {
  			$("#the_copy").html(data).append('<p><a href="/contact/">Back to contact page</a></p>');
		});
		
		return false;
	});



	/* Sidebar telephone graphics */
	
	var widget_id = $("#widget_sp_image-5");
	
	if($("#about-us").length > 0){
		widget_id.addClass('red').append('<span class="lead">We care, call us now on</span><span class="num">01253 895115</span>');
	}
	else if($("#childcare").length > 0){
		widget_id.addClass('orange').append('<span class="lead">We care, call us now on</span><span class="num">01253 882105</span>');
	}
	else if($("#housing").length > 0){
		widget_id.addClass('purple').append('<span class="lead">We care, call us now on</span><span class="num">01253 872320</span>');
	}
	else if ($("#physical-activity").length > 0){
		widget_id.addClass('green').append('<span class="lead">Call us now on</span><span class="num">01253 882105</span>');
	}
	else if ($("#outdoor-education").length > 0){
		widget_id.addClass('blue').append('<span class="lead">For enquiries, call now on</span><span class="num">01539 539000</span>');
	}
	else if ($("#training").length > 0){
		widget_id.addClass('yellow').append('<span class="lead">We care, call us now on</span><span class="num">01253 895115</span>');
	}
	else if ($("#contact").length > 0){
		widget_id.addClass('red').append('<span class="lead">We care, call us now on</span><span class="num">01253 895115</span>');
	}
	
	else{
		widget_id.addClass('red');
		if($(".tel", ".col-a").length > 0){
			var number = $(this).html();
			widget_id.append('<span class="lead">For help, call us now on</span><span class="num">' + number + '</span>');
		}
		else{
			widget_id.append('<span class="lead">For help, call us now on</span><span class="num">01253 895115</span>');
		}
	}
	
	if($(".page-id-71").length > 0 || $(".page-id-73").length > 0 || $(".page-id-231").length > 0){
		$(".lead", "#widget_sp_image-5").remove();
		$(".num", "#widget_sp_image-5").remove();
		widget_id.append('<span class="lead">For help call us now on</span><span class="num">01253 720720</span>');
	}
	else if($(".page-id-83").length > 0){
		$(".lead", "#widget_sp_image-5").remove();
		$(".num", "#widget_sp_image-5").remove();
		widget_id.append('<span class="lead">For help call us now on</span><span class="num">01253 273320</span>');
	}
	
	
	if($(".page-id-29").length > 0){
		widget_id.after('<div class="widget-container widget_sp_image"><a href="mailto:info@fyldecoastymca.org"><img src="/wp-content/themes/fylde-coast/images/CTA-29.gif" /></a></div>');
	}
	
	if($(".page-id-231").length > 0){
		widget_id.after('<div class="widget-container widget_sp_image"><img src="/wp-content/themes/fylde-coast/images/CTA-231.gif" /></div>');
	}
	
	
	if($(".tel", ".col-a").length > 0){
		$(".num", "#widget_sp_image-5").remove();
		var number = $(".tel").html();
						
		$("#widget_sp_image-5").append('<span class="num">' + number + '</span>');
	}
	
	
	
	
	if($(".service_post").length > 0){
		$("a", ".service_post").click(function(){
			var post_id = $(this).parent().parent().attr("id").replace("post_", "");
			var breadcrumb = $(".bread-crumb").html();
			
			$.get('/wp-content/themes/fylde-coast/ajax/load_article.php', {id : post_id}, function(response){
				$(".col-a").html('<div class="bread-crumb">' + breadcrumb + '</div>' + response);
			});
			
			return false;
		});
	}

	$(".scroll").each(function(){
		var span = $("span", this).html();
		$("img", this).attr("alt", span).attr("title", span);
	});
	
});




// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008-2009 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	// Params: A reference to a DOM element, the PNG src file pathname, and a
	// hidden "ready-to-run" passed when called back after image preloading.

	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;

	// Cache of DIVs created per element, and image preloader/data.
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}
		};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;

	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			// New image? Preload it with a callback to detect dimensions.
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		// No image?
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;

	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
		return;
	}

	// Convert English and percentage positions to pixels.
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);

	// Handle backgroundRepeat.
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}

	// Go!
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {
						elm.appendChild(d);
					}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}

	this.hook.enabled = 1;

	// Cache so updates are infrequent.
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};


IEPNGFix.update = function() {
	// Update all PNG backgrounds.
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}
$(document).ready(function(){$('.col-a img').one('load',function(){var img=$(this);var img_width=img.width();var img_height=img.height();var wrapper=$('<div class="rounded_wrapper"></div>');wrapper.width(img_width);wrapper.height(img_height);wrapper.css('float',img.css('float'));img.css('float','none')
wrapper.css('margin-right',img.css('margin-right'));img.css('margin-right','0')
wrapper.css('margin-left',img.css('margin-left'));img.css('margin-left','0')
wrapper.css('margin-bottom',img.css('margin-bottom'));img.css('margin-bottom','0')
wrapper.css('margin-top',img.css('margin-top'));img.css('margin-top','0')
wrapper.css('display','block');img.css('display','block')
if($.browser.msie&&$.browser.version=='6.0')
{if(img_width%2!=0)
{wrapper.addClass('ie6_width')}
if(img_height%2!=0)
{wrapper.addClass('ie6_height')}}
img.wrap(wrapper);img.after('<div class="tl"></div>');img.after('<div class="tr"></div>');img.after('<div class="bl"></div>');img.after('<div class="br"></div>');}).each(function(){if(this.complete)$(this).trigger("load");});});/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false, 
			numeric: 		false,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);




