/*  Transpose v0.90 - jQuery-powered plugin that transitions list items automatically, manually on user-demand, or both.
 *	Verified by JSLint, The Good Parts (http://www.jslint.com/)	
 *	
 */


/*global $: false clearInterval: false document: false setInterval: false Transpose: false Transpose: true jQuery: false*/

"use strict";

(function ($) {	
	$.fn.transpose = function (options) {
		return this.each(function () {
			if ($(this).data('transpose')) {
				return;
			}
			var settings = new Transpose.setup(this, options);
			transpose = $(this).data('settings', settings);
			Transpose.launch(settings);			
		});		
	};	
	var Transpose = {		
		setup: function (element, options) {
			element = "#" + $(element.id).selector;
			var defaults = {
				automatic: true,
				autoRestart: true,
				targetId: element,
				controller: false,
				paginatorLabel: false,
				fadeTimer: 'fast',
				nextId: 'target-right',
				paginatorCurrent: 'current',
				paginatorId: 'paginator',
				prevId: 'target-left',
				prevNext: false,
				timer: 10000
			},
		    config = $.extend(defaults, options || {});
			return config;
		},	
		launch: function (settings) {
			this.settings = settings;
			this.targetCount = $(settings.targetId + " > li").size();
			$(settings.targetId + ' li:not(:first)').fadeTo(0, 0, function () {
				$(this).hide();
			});
			$(settings.targetId + ' li:first').fadeTo(0, 1).addClass('active');
			if (settings.automatic) {
				Transpose.startTimer(settings.timer);
			}
			if (settings.prevNext) {
				Transpose.prevNext();
			}
			if (settings.paginator) {
				Transpose.paginator.buildPaginator();
			}
		},
		
		// function  to fade out, swap the image, increase the index and fade in the image 
		autoSperse: function () { 
			Transpose.motion.fade();
		},
		
		index: {
			currentIndex: 1,
			modifyIndex: function (selectedBanner, alteredIndex) {
				if (alteredIndex !== undefined) {
					if ($(Transpose.settings.targetId + " :animated").length > 0) {
						return false;
					}
					else {					
						if (alteredIndex === -1) {
							alteredIndex = Transpose.targetCount - 1;
							selectedBanner = $(Transpose.settings.targetId + " li").eq(alteredIndex);
						}
						else if (alteredIndex === -2) {
							alteredIndex = Transpose.targetCount - 2;
							selectedBanner = $(Transpose.settings.targetId + " li").eq(alteredIndex);
						}
						
						Transpose.index.currentIndex = alteredIndex + 1;
						//Index Precaution - Don't let the index be greater than the total number of targets minus one and don't let the index fall below 0.
						if (Transpose.index.currentIndex > Transpose.targetCount - 1) {
							Transpose.index.currentIndex = 0;
						}
						else if (Transpose.index.currentIndex < 0) {
							Transpose.index.currentIndex = Transpose.targetCount - 1;
						}
						else {
							Transpose.index.currentIndex = alteredIndex + 1;
						}
						Transpose.motion.fade(selectedBanner, alteredIndex);
					}
				}
				else {			
					this.currentIndex += 1;
					if (this.currentIndex === Transpose.targetCount) { 
						this.currentIndex = 0; 
					}
				}			
			}
		},
		 
		motion: {
			fade: function (selectedBanner, alteredIndex) {
				var activeTarget = $(Transpose.settings.targetId + " li.active");
				// first fade the image out
				activeTarget.fadeTo(Transpose.settings.fadeTimer, 0, function () {
					Transpose.updateStatus.target(this);
					Transpose.updateStatus.paginator(alteredIndex);
					if (selectedBanner === undefined) {
						$(Transpose.settings.targetId + " li").eq(Transpose.index.currentIndex).show().fadeTo(Transpose.settings.fadeTimer, 1, function () {
							Transpose.updateStatus.target(this);			
							// Index Manipulation
							Transpose.index.modifyIndex();
						});		
					}
					else {
						//Fade the image in.
						$(selectedBanner).show().fadeTo(Transpose.settings.fadeTimer, 1, function () {
							Transpose.updateStatus.target(this);	
							if (Transpose.settings.autoRestart) {
								Transpose.startTimer(Transpose.settings.timer);
							}
						});
					}
				});	
			},
			slideLeft: function (selectedBanner, alteredIndex) {
				var activeTarget = $(Transpose.settings.targetId + " li.active");
				activeTarget.hide("slide", { direction: "right" }, function () {
					Transpose.updateStatus.target(this);
					Transpose.updateStatus.paginator(alteredIndex);
				}).fadeTo(0, 0);
				if (selectedBanner === undefined) {	
					$(Transpose.settings.targetId + " li").eq(Transpose.index.currentIndex).show("slide", { direction: "left" }, function () {			
						Transpose.updateStatus.target(this);			
						// Index Manipulation
						Transpose.index.modifyIndex();
					});
				}
				else {
					//Fade the image in.
					$(selectedBanner).fadeTo(0, 1).show("slide", { direction: "left" }, function () {
						Transpose.updateStatus.target(this);	
						if (Transpose.settings.autoRestart) {
							Transpose.startTimer(Transpose.settings.timer);
						}
					});
				}
			},
			slideRight: function (selectedBanner, alteredIndex) {
				var activeTarget = $(Transpose.settings.targetId + " li.active");
				activeTarget.hide("slide", { direction: "left" }, function () {
					Transpose.updateStatus.target(this);
					Transpose.updateStatus.paginator(alteredIndex);
				}).fadeTo(0, 0);
				if (selectedBanner === undefined) {	
					$(Transpose.settings.targetId + " li").eq(Transpose.index.currentIndex).show("slide", { direction: "left" }, function () {			
						Transpose.updateStatus.target(this);			
						// Index Manipulation
						Transpose.index.modifyIndex();
					});
				}
				else {
					//Fade the image in.
					$(selectedBanner).fadeTo(0, 1).show("slide", { direction: "right" }, function () {
						Transpose.updateStatus.target(this);	
						if (Transpose.settings.autoRestart) {
							Transpose.startTimer(Transpose.settings.timer);
						}
					});
				}
			}
		},
				
		paginator: {
			buildPaginator: function () {
				Transpose.paginator.panelCreator();
				Transpose.paginator.assignButtons();
				$("#target0").addClass(Transpose.settings.paginatorCurrent);
			},
			panelCreator: function () {
				var targetController = document.createElement("ul"),
			    counter = 0;
				targetController.id = Transpose.settings.paginatorId;	
				for (; counter < Transpose.targetCount; counter += 1) {
					Transpose.paginator.nodeCreator(counter, targetController);
				}
				$(Transpose.settings.targetId).append(targetController);			
			},
			nodeCreator: function (counter, targetController) {
				var targetAnchor = document.createElement("a"),
				li = document.createElement("li");
				targetAnchor.setAttribute('href', '#');
				targetAnchor.id = "target" + counter;
				li.appendChild(targetAnchor);
				targetController.appendChild(li);
				if (Transpose.settings.paginatorLabel) {
					Transpose.paginator.nodeLabeler(targetAnchor, counter);
				}
			},
			nodeLabeler: function (targetAnchor, counter) {
				var anchorText = document.createTextNode(counter + 1);
				targetAnchor.appendChild(anchorText);
			},
			individualAssigner: function (i) {
				$("#target" + i).click(function () {
					if ($(this).hasClass(Transpose.settings.paginatorCurrent)) {
						return false;
					}
					clearInterval(Transpose.timer);
					Transpose.index.modifyIndex($(Transpose.settings.targetId + " li").eq(i), i);
					return false;
				});
			},
			assignButtons: function () {
				for (var i = 0; i < Transpose.targetCount; i += 1) {
					this.individualAssigner(i);	
				}
			}
		},
		
		prevNext: function () {
			var prevButton = document.createElement("a"),
			nextButton = document.createElement("a");
			prevButton.id = Transpose.settings.prevId;
			prevButton.setAttribute('href', '#');
			$(Transpose.settings.targetId).append(prevButton);
			$("#" + Transpose.settings.prevId).click(function () {
				clearInterval(Transpose.timer);
				Transpose.index.modifyIndex($(Transpose.settings.targetId + " li").eq(Transpose.index.currentIndex - 2), Transpose.index.currentIndex - 2);
				return false;
			});
			nextButton.id = Transpose.settings.nextId;
			nextButton.setAttribute('href', '#');
			$(Transpose.settings.targetId).append(nextButton);
			$("#" + Transpose.settings.nextId).click(function () {
				clearInterval(Transpose.timer);
				Transpose.index.modifyIndex($(Transpose.settings.targetId + " li").eq(Transpose.index.currentIndex), Transpose.index.currentIndex);
				return false;
			});
		},
		
		//Method that invokes the timer used to automatically swap images
		startTimer: function (milliseconds) {
			clearInterval(Transpose.timer);
			Transpose.timer = setInterval(Transpose.autoSperse, Transpose.settings.timer);
		},
		
		updateStatus: {
			paginator: function (alteredIndex) {
				$("#" + Transpose.settings.paginatorId + " li a").removeClass(Transpose.settings.paginatorCurrent);
				if (alteredIndex !== undefined) {
					$("#target" + alteredIndex).addClass(Transpose.settings.paginatorCurrent);
				}
				else {
					$("#target" + Transpose.index.currentIndex).addClass(Transpose.settings.paginatorCurrent);
				}
			},
			target: function (target) {
				$(target).toggleClass('active');
			}		
		}		
	};
}(jQuery));

var labelHandler = function (formControl, searchStr) {
	if ($(formControl).attr("value") == searchStr) {
		$(formControl).attr("value", "");
	}
	$(formControl).blur(function () {
		if ($(formControl).attr("value") == "") {
			$(formControl).attr("value", searchStr);
		}
	});
}

$(document).ready(function (){

	$('#banners').transpose ({
		fadeTimer: '400',
		timer: 10000 
	});
	
	// Quick Form Label Handling Function Calls
	// $("#contact-name").focus(function () {
		// labelHandler(this, "Full Name*");
	// });
	// $("#contact-email").focus(function () {
		// labelHandler(this, "Email Address*");
	// });
	// $("#contact-phone").focus(function () {
		// labelHandler(this, "Phone Number");
	// });
	// $("#contact-comment").focus(function () {
		// labelHandler(this, "Question/Comment*");
	// });
	
	$("a.modal").colorbox();
	$("a.table-link").colorbox({width:"90%", height:"90%", iframe:true})
});
