function Slider(params) {
	if (!$(params.viewbox).length) {
		return false;
	}

	this.init = function(){
		// slider objects/controls
		this.btnPrev = params.btnPrev ? params.btnPrev : false;
		this.btnNext = params.btnNext ? params.btnNext : false;
		this.btnGoTo = params.btnGoTo ? params.btnGoTo : false;
		this.viewbox = params.viewbox;
		this.panes = params.panes;

		// create the slide, and move the panes into it
		$(this.viewbox).append("<div class=\"slide\"></div>");
		this.slide = this.viewbox+" .slide";
		$(this.slide).append($(this.panes));
		
		// set required CSS values to allow the animation
		$(this.viewbox).css({"position":"relative"});
		$(this.slide).css({
			"position":"relative",
			"width":"999999px"
		});

		// calculate initial states/attributes
		this.curPos = $(this.slide).position().left;					// the current x co-ordinate/CSS left value of the slider
		this.startPos = this.curPos;									// allow an initial offset other than 0
		this.curGroup = params.group ? params.group : 1;				// the current group being viewed
		this.interval = params.interval ? params.interval : 300;		// the time (in milliseconds) for the duration of the animation
		this.groups = $(this.panes).length;								// the number of groups or panes to slide through
		this.viewBoxWidth = $(this.viewbox).width();
		
		// BIND EVENTS
		var slider = this;
		$(this.btnPrev).mousedown(function(){slider.retreat();});
		$(this.btnNext).mousedown(function(){slider.advance();});
		$(this.btnGoTo.btn).mousedown(function(){slider.goTo($(this).find(slider.btnGoTo.paneID).val());});
		
		var btns = [];
		if (this.btnPrev) {btns.push(this.btnPrev);}
		if (this.btnNext) {btns.push(this.btnNext);}
		if (this.btnGoTo && this.btnGoTo.btn) {btns.push(this.btnGoTo.btn);}
		$(btns.join(", ")).click(function(){return false;});

		if (this.groups > 1) {
			$(this.btnNext).addClass("active");
		}
	};

	this.resetSlider = function() {
		this.curPos = this.startPos;
		this.curGroup = 1;
		this.groups = $(params.pane).length;

		// BIND EVENTS
		var slider = this;
		$(this.btnPrev).mousedown(function(){slider.retreat();});
		$(this.btnNext).mousedown(function(){slider.advance();});
		$(this.btnGoTo.btn).mousedown(function(){slider.goTo($(this).find(slider.btnGoTo.paneID).val());});
		
		var btns = [];
		if (this.btnPrev) {btns.push(this.btnPrev);}
		if (this.btnNext) {btns.push(this.btnNext);}
		if (this.btnGoTo && this.btnGoTo.btn) {btns.push(this.btnGoTo.btn);}
		$(btns.join(", ")).click(function(){return false;});

		$(this.btnPrev+", "+this.btnNext).removeClass("active");
		if(this.groups > 1){
			$(this.btnNext).addClass("active");
		}
	};

	// Move the slider forward one pane
	this.advance = function() {
		if (this.curGroup >= this.groups) {
			return;
		}
		$(this.slide).stop(true,true);
		
		if(this.callbackBeforeAdvance) {
			this.callbackBeforeAdvance();
		}
		
		this.curGroup++;
		var slider = this;
		$(this.slide).animate({"left":this.curPos-this.viewBoxWidth},this.interval,function() {
			slider.curPos -= slider.viewBoxWidth;
			if (slider.curGroup == slider.groups) {
				$(slider.btnNext).removeClass("active");
			}
			if (slider.groups > 1) {
				$(slider.btnPrev).addClass("active");
			}
			
			if (slider.callbackAfterAdvance) {
				slider.callbackAfterAdvance();
			}
		});
	};

	// Move the slider backward one pane
	this.retreat = function() {
		if (this.curGroup <= 1) {
			return;
		}
		$(this.slide).stop(true,true);
		
		if (this.callbackBeforeRetreat) {
			this.callbackBeforeRetreat();
		}
		
		this.curGroup--;
		var slider = this;
		$(this.slide).animate({"left":this.curPos+this.viewBoxWidth},this.interval,function() {
			slider.curPos += slider.viewBoxWidth;
			if (slider.curGroup == 1) {
				$(slider.btnPrev).removeClass("active");
			}
			if (slider.groups > 1) {
				$(slider.btnNext).addClass("active");
			}
			
			if (slider.callbackAfterRetreat) {
				slider.callbackAfterRetreat();
			}
		});
	};
	
	// Move the slider to the selected pane
	this.goTo = function(paneID) {
		if (this.curGroup == paneID) {
			return;
		}

		var destination = (this.viewBoxWidth * (paneID - 1)) * -1;

		$(this.slide).stop(true,true);

		if (this.callbackBeforeGoTo) {
			this.callbackBeforeGoTo();
		}

		this.curGroup = paneID;
		var slider = this;
		$(this.slide).animate({"left":destination},this.interval,function() {
			slider.curPos = destination;

			// put the active class on the selected button
			$(slider.btnGoTo.btn).removeClass("active").eq(paneID - 1).addClass("active");

			if (slider.callbackAfterGoTo) {
				slider.callbackAfterGoTo();
			}
		});
	};

	// Callback events
	this.beforeAdvance = function(f) {this.callbackBeforeAdvance = f;};
	this.afterAdvance = function(f) {this.callbackAfterAdvance = f;};
	this.beforeRetreat = function(f) {this.callbackBeforeRetreat = f;};
	this.afterRetreat = function(f) {this.callbackAfterRetreat = f;};
	this.beforeGoTo = function(f) {this.callbackBeforeGoTo = f;};
	this.afterGoTo = function(f) {this.callbackAfterGoTo = f;};

	// put the slider object in play
	this.init();
}

var homePageBanners = {
	slider:false,
	timer:false,
	init:function() {
		// make sure the object exists before assigning events
		if (!$("#banner .viewbox").length) {
			return false;
		}

		// Show controls that are hidden for users without javascript
		$("#banner .control .paging").show();

		homePageBanners.slider = new Slider({
			btnGoTo:{
				btn:"#banner .control .paging li a",
				paneID:"[name=\"paneID\"]"
			},
			panes:"#banner .viewbox .pane",
			viewbox:"#banner .viewbox"
		});
		homePageBanners.slider.beforeGoTo(homePageBanners.change);
		
		homePageBanners.timer = window.setInterval(homePageBanners.autoChange,8000);
//		$("#banner").mouseover(function() {
//			clearInterval(homePageBanners.timer);
//		}).mouseout(function() {
//			homePageBanners.timer = window.setInterval(homePageBanners.autoChange,5000);
//		});
	},
	change:function() {
		// reset autochange timer
		clearInterval(homePageBanners.timer);
		homePageBanners.timer = window.setInterval(homePageBanners.autoChange,8000);
		
		// swap text in control box
		$("#banner .control .pane").stop(true,true);
		$("#banner .control .pane:visible").fadeOut(100,function() {
			$("#banner .control .pane:eq("+(homePageBanners.slider.curGroup - 1)+")").fadeIn(200);
		});
	},
	autoChange:function(paneID) {
		// automatically go to the next banner
		var next = parseInt(homePageBanners.slider.curGroup,10) + 1;
		if (next > homePageBanners.slider.groups) {
			next = 1;
		}
		homePageBanners.slider.goTo(next);
	}
};
homePageBanners.init();

var feaPhotosVideos = {
	slider:false,
	init:function() {
		// make sure the object exists before assigning events
		if (!$("#sidebar-pics-video-slider").length) {
			return false;
		}
		
		// show the left/right arrows
		$("#sidebar-pics-video-slider .larr, #sidebar-pics-video-slider .rarr").css({"visibility":"visible"});


		feaPhotosVideos.slider = new Slider({
			btnPrev:"#sidebar-pics-video-slider .larr",
			btnNext:"#sidebar-pics-video-slider .rarr",
			panes:"#sidebar-pics-video-slider .viewbox .pane",
			viewbox:"#sidebar-pics-video-slider .viewbox"
		});
		feaPhotosVideos.slider.afterAdvance(feaPhotosVideos.change);
		feaPhotosVideos.slider.afterRetreat(feaPhotosVideos.change);
	},
	change:function() {
		// swap text/description under thumbnail
//		var html = $("#home-feas .fea.photos-video .slider .viewbox .pane").eq(feaPhotosVideos.slider.curGroup - 1).find(".meta").html();
		var html = $("#sidebar-pics-video-slider .viewbox .pane").eq(feaPhotosVideos.slider.curGroup - 1).find(".meta").html();
//		$("#home-feas .fea.photos-video .content .summary").html(html);
		$("#sidebar-pics-video-slider .summary").html(html);
	}
};
feaPhotosVideos.init();

var galleryPhotos = {
	slider:false,
	init:function() {
//		if ($("#photo-gallery .thb").length == 1) {
//			$("#photo-gallery .slider").hide();
//			return false;
//		}
		galleryPhotos.slider = new Slider({
			btnPrev:"#photo-gallery .larr",
			btnNext:"#photo-gallery .rarr",
			panes:"#photo-gallery .pane",
			viewbox:"#photo-gallery .viewbox"
		});
		
		$("#photo-gallery .thb").click(galleryPhotos.swapPhoto);
	},
	swapPhoto:function() {
		
		var file = $(this).find("img").attr("src").split("/");
		file = file[file.length - 1];
		
		// swap the photo
		$("#photo-gallery .fullview img").attr("src","/wp-content/themes/communitystrength/_public/photos/"+file);
		
		// swap the date
		var date = $(this).find(".meta .date").text();
		$("#photo-gallery .photo-info .meta").html(date);
		
		// swap the description
		var desc = $(this).find(".meta .desc").html();
		$("#photo-gallery .photo-info .desc").html(desc);
		
		return false;
	}
};
galleryPhotos.init();

// Show controls that are hidden for users without javascript
$("#photo-gallery .larr,#photo-gallery .rarr").css({"visibility":"visible"});

// Initialize poll values
var pollsL10n={
	ajax_url:"http://www.communitystrength.ca/wp-content/plugins/wp-polls/wp-polls.php",
	text_wait:"Your last request is still being processed. Please wait a while ...",
	text_valid:"Please choose a valid poll answer.",
	text_multiple:"Maximum number of choices allowed: ",
	show_loading:"1",
	show_fading:"1"
};
