function CRB_Slider(params) {
	this.init = function(){
		// slider objects/controls
		this.direction = params.direction == "vertical" ? "vertical" : "horizontal";
		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.slide = params.slide;
		this.panes = params.panes;
		
		// duplicate the first pane at the end of the group (Needed to create animation that doesn't 'rewind')
		$(this.slide).append($(this.panes).eq(0).clone());

		// set required CSS values to allow the animation
		$(this.viewbox).css({"position":"relative"});
		$(this.slide).css({
			"position":"relative",
			"width":"999999px"
		});

		// calculate initial states/attributes
		if (this.direction == "vertical") {
			this.curPos = $(this.slide).position().top;					// the current y co-ordinate/CSS top value of the slider
		} else {
			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.animateWidth = $(this.panes).outerWidth();					// the horizontal distance to slide when changing panes
		this.animateHeight = $(this.panes).outerHeight();				// the vertical distance to slide when changing panes
		
		// BIND EVENTS
		var self = this;
		$(this.btnPrev).mousedown(function(){self.retreat();});
		$(this.btnNext).mousedown(function(){self.advance();});
		$(this.btnGoTo.btn).mousedown(function(){self.goTo($(this).find(self.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");
		}
	};
	// Move the slider forward one pane
	this.advance = function() {
		var params = {};
		var destination = 0;
		if (this.direction == "vertical") {
			destination = this.curPos -= this.animateHeight;
			params.top = destination;
		} else {
			destination = this.curPos -= this.animateWidth;
			params.left = destination;
		}
		
		$(this.slide).stop(true,true);
		
		if(this.callbackBeforeAdvance) {
			this.callbackBeforeAdvance();
		}
		
		this.curGroup++;
		var self = this;
		$(this.slide).animate(params,this.interval,function() {
			self.curPos = destination;
/*
			if (self.curGroup == self.groups) {
				$(self.btnNext).removeClass("active");
			}
			if (self.groups > 1) {
				$(self.btnPrev).addClass("active");
			}
*/
			
			if (self.curGroup >= self.groups) {
				self.rewind();
			}
			
			// put the active class on the selected button
			$(self.btnGoTo.btn).removeClass("active").eq(self.curGroup - 1).addClass("active");
			
			if (self.callbackAfterAdvance) {
				self.callbackAfterAdvance();
			}
		});
	};
	
	this.rewind = function() {
		this.curPos = this.startPos;
		this.curGroup = 1;
		if (this.direction == "vertical") {
			$(this.slide).css({"top":this.startPos});
		} else {
			$(this.slide).css({"left":this.startPos});
		}
	};
/*
	// Move the slider backward one pane
	this.retreat = function() {
		if (this.curGroup <= 1) {
			return false;
		}

		var params = {};
		var destination = 0;
		if (this.direction == "vertical") {
			destination = this.curPost += this.animateHeight;
			params.top = destination;
		} else {
			destination = this.curPos += this.animateWidth;
			params.left = destination;
		}
		
		$(this.slide).stop(true, false);
		if (this.callbackBeforeRetreat) {
			this.callbackBeforeRetreat();
		}
		
		this.curGroup --;
		var self = this;
		$(this.slide).animate(params, this.interval, function() {
			self.curPos = destination;
			
			// put the active class on the selected button
			$(self.btnGoTo.btn).removeClass("active").eq(self.curGroup + 1).addClass("active");
			
			if (self.callbackAfterRetreat) {
				self.callbackAfterRetreat();
			}
		});
	};
*/

	// Move the slider to the selected pane
	this.goTo = function(paneID) {
		if (this.curGroup == paneID) {
			return;
		}
		
		var params = {};
		var destination = 0;
		if (this.direction == "vertical") {
			destination = (this.animateHeight * (paneID - 1)) * -1;
			params.top = destination;
		} else {
			destination = (this.animateWidth * (paneID - 1)) * -1;
			params.left = destination;
		}

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

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

		this.curGroup = paneID;
		var self = this;
		$(this.slide).animate(params,this.interval,function() {
			self.curPos = destination;

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

			if (self.callbackAfterGoTo) {
				self.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;};
}

var highlights = {
	slider:false,
	timer:false,
	init:function() {
		if (!$("#highlights").length || $("#highlights .slider li").length <= 1) {
			return false;
		}
		
		// create paging buttons
		this.createPaging();
		
		// Instantiate a slider object
		this.slider = new CRB_Slider({
			btnGoTo:{
				btn:"#highlights .paging li a",
				paneID:"[name=\"paneId\"]"
			},
			panes:"#highlights .slider > li",
			viewbox:"#highlights .viewbox",
			slide:"#highlights .viewbox .slider",
			interval:400
		});
		this.slider.init();
		
		// Define the timer to auto change the banners
		this.timer = window.setInterval(this.autoChange, 8000);

		// reset the auto-change timer if pane selected manually
		var self = this;
		this.slider.beforeGoTo(function() {
			clearInterval(self.timer);
			self.timer = window.setInterval(self.autoChange, 8000);
		});
	},
	createPaging:function() {
		var pagingList = [];
		var i = 1;
		$("#highlights .slider > li").each(function() {
			var aClass = i == 1 ? " class=\"active\"" : "";
			pagingList.push("<li><a" + aClass + " href=\"#\" title=\"Go to " + i + "\">" + i + "<input type=\"hidden\" name=\"paneId\" value=\"" + i + "\" /></a></li>");
			i ++;
		});
		$("#highlights .viewbox").append("<ul class=\"paging\">" + pagingList.join("") + "</ul>");
	},
	autoChange:function() {
		// automatically go to the next banner
		highlights.slider.advance();
	}
};
highlights.init();

/* FORMS
********************/

function CRB_InputValueAsLabel(el) {
	var text = $(el).val();
	$(el).focus(function() {
		$(this).addClass("active");
		if (!this.changed) {
			$(this).val("");
		}

		return;
	}).blur(function() {
		$(this).removeClass("active");
		if ((this.changed && !$.trim($(this).val())) || !this.changed) {
			this.changed = false;
			$(this).val(text);
		}

		return;
	}).keypress(function() {
		this.changed = true;

		return;
	});

	return;
}

var searchForm = {
	init:function() {
		var el = $("#search-form input[name='s']");
		if (!el.length) {
			return false;
		}
		
		new CRB_InputValueAsLabel(el);
	}
};
searchForm.init();

var newsletterSignUpForm = {
	init:function() {
		var el = $("#newsletter-signup input[name='email']");
		if (!el.length) {
			return false;
		}
		
		new CRB_InputValueAsLabel(el);
	}
};
newsletterSignUpForm.init();
var videoLink = {
	init:function() {
		var vidlink = $('a.video-link');
		if(!vidlink.length) {
			return false;
		}
		$('.video-link').fancybox({'width' :655,'height' : 400});
	}
};
videoLink.init();
var paginationhide = {
	init:function() {
		var leftlink = $("#pagination .left-page a");
		var rightlink= $("#pagination .right-page a");
		if(!leftlink.length && !rightlink.length) {
			$("#pagination").hide();
		}
	}
};
paginationhide.init();



function ModalDialog(parms) {
	this.requiredElement = parms.requiredElement;
	this.selectorClass = parms.selectorClass;


	this.init = function () {

		var self = this;
		// only proceed if the provided required element(s) exist
		if (!$(parms.requiredElement).length) {
			return;
		}
		
		// append popup window/overlay to end of body - IE7 can't handle z-index properly if added elsewhere
		if (!$(".modal-dialog." + parms.selectorClass).length) {
			$("body").append(
				"<div class=\"modal-dialog " + parms.selectorClass + "\">" +
					"<div class=\"controls\">" +
					"<a class=\"btn-close\" href=\"#\" title=\"Close\">Close X</a>" +
					"</div>" +
					"<div class=\"content\">" +
					"</div>" +
					"</div>" +
					"<div class=\"modal-dialog-overlay " + parms.selectorClass + "\"></div>"
			);
		}
		// click on dialog 'close' button
		$(".modal-dialog." + parms.selectorClass + " .btn-close, .modal-dialog-overlay").click(function () {
			self.hideDialog();
			return false;
		});
		$(".modal-dialog-overlay." + parms.selectorClass + " ").click(function () {
			self.hideDialog();
			return false;
		});
		// bind any application specific events
		this.bindControls();
	};

	/*
	*	overwrite this function, binding the desired events (typically the events that will show the dialog)
	*/
	this.bindControls = function () {
		return;
	};

	/*
	*	overwrite this function, generating the desired dialog content
	*/
	this.getDialogContent = function () {
		return "";
	};

	/*
	*	Show the dialog - identify/generate/insert dialog content
	*/
	this.showDialog = function () {
		var content = this.getDialogContent();
		this.populateDialog(content);
		var height = $(window).height();
		var dialogHeight = $(".modal-dialog." + parms.selectorClass).height();
		/*
		if (height > dialogHeight) {
			$(".modal-dialog." + parms.selectorClass).css({position: "fixed"});
		} else {
			$(".modal-dialog." + parms.selectorClass).css({position: "absolute", "margin-top": 0, "top": 100});
		}*/
		$(".modal-dialog." + parms.selectorClass).fadeIn("fast");
		$(".modal-dialog-overlay." + parms.selectorClass).fadeIn("fast");
	};

	/*
	*	Hide the dialog
	*/
	this.hideDialog = function () {
		var self = this;
		$(".modal-dialog." + parms.selectorClass).fadeOut("fast", function () {
			$(".modal-dialog-overlay." + parms.selectorClass).fadeOut("fast");
			// clear contents of dialog
			self.clearDialog();
		});
	};

	/*
	*	Clear the contents of the dialog
	*/
	this.clearDialog = function () {
		$(".modal-dialog." + parms.selectorClass + " > .content").empty();
	};

	/*
	*	Insert the provided HTML into the dialog
	*/
	this.populateDialog = function (html) {
		$(".modal-dialog." + parms.selectorClass + " > .content").html(html);
	};
}
var WagJagDialog = new ModalDialog({
	requiredElement: "#wagjag",
	selectorClass: "wagjag"
});
WagJagDialog.bindControls = function () {
	var self = this;
	$(".type-list li .tax-return-btn").click(function () {
		var parent = $(this).parent('li');
		self.showDialog(parent);
		return false;
	});
	$(".type-list li .wagjag-thumb").click(function () {
		var parent = $(this).parent('li');
		self.showDialog(parent);
		return false;
	});

};
WagJagDialog.showDialog = function (parent) {
	var self = this,
		content = parent.find('.dialog-content').html(),
		selectorClass = self.selectorClass;

	self.populateDialog(content);	

	var height = $(window).height(),
		dialogHeight = $(".modal-dialog." + this.selectorClass).height();

	if (height > dialogHeight) {
		$(".modal-dialog." + this.selectorClass).css({position: "fixed"});
	} else {
		$(".modal-dialog." + this.selectorClass).css({position: "absolute", "margin-top": 0, "top": 100});	
	}
	$(".modal-dialog." + selectorClass).fadeIn("fast");
	$(".modal-dialog-overlay." + selectorClass).fadeIn("fast");
};
WagJagDialog.init();
