(function() {

	lib.obj.itemFader = function(settings) {
		if(arguments.length > 0)
		{ this.init(settings); }
	};
	
	lib.obj.itemFader.prototype.init = function(settings) {
		var settings = $.extend({
			viewport : "#itemFader",
			item : "#itemFader .item",
			interval : 250,
			next : "#itemFader .next",
			prev : "#itemFader .prev",
			first : "#itemFader .first",
			last : "#itemFader .last",
			circular : false,
			preFadeCallback : null,
			postFadeCallback : null
		}, settings);
		
		this.viewport = settings.viewport;
		this.item = settings.item;
		this.interval = settings.interval;
		this.next = settings.next;
		this.prev = settings.prev;
		this.first = settings.first;
		this.last = settings.last;
		this.circular = settings.circular;
		this.preFadeCallback = settings.preFadeCallback;
		this.postFadeCallback = settings.postFadeCallback;
		this.faderInfo = {};
		this.faderInfo.index = 0;
		this.faderInfo.prevIndex = 0;
		this.faderInfo.end = $(this.item).size() - 1;
		this.zIndex = 0;
		
		/* Setup the Items */
		var jqThisViewport = $(this.viewport);
		if( jqThisViewport.size() > 0 )
		{
			var jqThisItems = $(this.item);
			this.zIndex = jqThisItems.eq(0).css("z-index");
			this.zIndex = ( this.zIndex == "auto") ? 0 : parseInt(this.zIndex);
			
			/* Size the items to fit the viewport, show first one */
			var w = jqThisViewport.width();
			var h = jqThisViewport.height();
			jqThisItems.each(function() { h = $(this).height() > h || $(this).height() < h ? $(this).height() : h; });
				/* Cabelas Specific Update here */
				//jqThisItems.width(w).height(h).css({ position: "absolute", top : 0, left : 0 }).fadeTo(0,0).css("z-index", this.zIndex).eq(0).fadeTo(0,1).css("z-index", this.zIndex+1);
				$(this.viewport).css( {position: 'relative' });
				/* ------------------ */
			/* ------------- */
				
			/* Set the default states of the buttons */
			$(this.next).hide();
			$(this.prev).hide();
			if(this.circular && ($(this.item).size() > 1))
			{
				$(this.next).show();
				$(this.prev).show();
			}
			else if( !this.circular && ($(this.item).size() > 1))
			{ $(this.next).show(); }
			/* ----------------------- */
			
			$(this.next + "," + this.prev + "," + this.first + "," + this.last).click(function(evt){ evt.preventDefault(); });
			this.removeEvents();
			this.createEvents();
		}
		/* ----------------- */
	};
	
	lib.obj.itemFader.prototype.createEvents = function() {
		var currObj = this;
		$(this.prev).bind("click.itemFader", function(evt) {
			currObj.backward(); 
		});
		$(this.next).bind("click.itemFader", function(evt) {
			currObj.forward();
		});
		$(this.first).bind("click.itemFader", function(evt){
			currObj.toFirst();
		});
		$(this.last).bind("click.itemFader", function(evt){
			currObj.toLast();
		});
	};
	
	lib.obj.itemFader.prototype.removeEvents = function() {
		$(this.prev).unbind("click.itemFader");
		$(this.next).unbind("click.itemFader");
		$(this.last).unbind("click.itemFader");
		$(this.first).unbind("click.itemFader");
	};
	
	lib.obj.itemFader.prototype.forward = function() {
		this.faderInfo.prevIndex = this.faderInfo.index;
		this.faderInfo.index++;
		this.fade();
	};
	
	lib.obj.itemFader.prototype.backward = function() {
		this.faderInfo.prevIndex = this.faderInfo.index;
		this.faderInfo.index--;
		this.fade();
	};
	
	lib.obj.itemFader.prototype.toFirst = function() {
		this.faderInfo.prevIndex = this.faderInfo.index;
		this.faderInfo.index = 0;
		this.fade();
	};
	
	lib.obj.itemFader.prototype.toLast = function() {
		this.faderInfo.prevIndex = this.faderInfo.index;
		this.faderInfo.index = this.faderInfo.end;
		this.fade();
	};
	
	lib.obj.itemFader.prototype.fade = function() {
		/* Make index is in range */
		if(this.circular)
		{
			if( this.faderInfo.index > this.faderInfo.end )
			{ this.faderInfo.index = 0; }
			else if( this.faderInfo.index < 0 )
			{ this.faderInfo.index = this.faderInfo.end; }
		}
		else
		{
			if( this.faderInfo.index > this.faderInfo.end )
			{ this.faderInfo.index = this.faderInfo.end; }
			else if( this.faderInfo.index < 0 )
			{ this.faderInfo.index = 0; }
		}
		/* --------------- */
		
		/* show/hide buttons */
		if( !this.circular )
		{
			if( this.faderInfo.index == this.faderInfo.end )
			{ $(this.next).hide(); }
			else if(this.faderInfo.end > 0 )
			{ $(this.next).show(); }
			
			if( this.faderInfo.index == 0 )
			{ $(this.prev).hide(); }
			else
			{ $(this.prev).show(); }
		}
		/* ----------------------- */
		
		if( this.faderInfo.prevIndex != this.faderInfo.index)
		{
			this.removeEvents();
			if($.isFunction(this.preFadeCallback))
			{ this.preFadeCallback( this ); }
			
			var currObj = this;
			var jqThisItems = $(currObj.item);
			//jqThisItems.css({ position: "absolute", top : 0, left : 0 }).fadeTo(1000,0).css("z-index", this.zIndex);
			//jqThisItems.eq(this.faderInfo.index).fadeTo(1000,1).css("z-index", this.zIndex+1);
			
			jqThisItems.eq(currObj.faderInfo.prevIndex).css({ position: "absolute", top : 0, left : 0 }).fadeTo( currObj.interval, 0 ).css("z-index", this.zIndex);
			jqThisItems.eq(currObj.faderInfo.index).fadeTo( currObj.interval, 1, function() {
				currObj.createEvents();
				if($.isFunction(currObj.postFadeCallback))
				{ currObj.postFadeCallback( currObj ); }
			}).css("z-index", this.zIndex+1);
		}
	};

})();


(function() {
	var site = window.site = {
		data : {
			imageRotatorIntervals : []
		},
		init : {
			imageRotators : function() {
				$(".js-imageRotator").each(function(i) {
					
					site.data.imageRotatorIntervals[i] = null;
					var thisRotator = this;
					
					// Create the itemFader obj 
					var thisItemFader = new lib.obj.itemFader({
						viewport: ".js-imageRotator:eq(" + i + ")",
						item: ".js-imageRotator:eq(" + i + ") .panel",
						circular : true,
						preFadeCallback : function() { 
							// Show the Panel Link as Active
							$(".js-panelLink", thisRotator).eq(thisItemFader.faderInfo.index).triggerHandler("click.button"); 
						},
						interval : 1000
					});
					
					// Initialize the height (for Safari)
					$("img", thisItemFader.item).one("load",function(){
						var imgHeight = $(thisItemFader.viewport).height() + "px";
						$(thisItemFader.viewport).height(imgHeight);
					}).each(function(){
						if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
							$(this).trigger("load");
					}); 
					
					setTimeout(function(){
						// For IE, FOUC
						$(thisItemFader.item).not(":eq(0)").fadeTo(0,0);
						$(thisItemFader.item).eq(0).css({'z-index':'11', 'opacity': '1'});
						$(thisItemFader.item).css({'position':'absolute'});
					},500);
					
										
					// Create the Panel Links as Buttons 
					new lib.obj.button({
						cssButton: true,
						hasClick: true,
						hasHover: true,
						activeId: "js-activeRotatorPanelButton",
						cssOff: "off",
						cssHover: "on",
						cssOn: "on",
						buttonSelector: ".js-imageRotator:eq(" + i + ") .js-panelLink",
						buttonCollectionSelector : ".js-imageRotator:eq(" + i + ") .js-panelLink"
					});
										
					// Setup the Events for clicking a Panel Link 
					$(".js-panelLink", this).each(function(x) {
						$(this).bind("click.imageRotator", function(evt) {
							evt.preventDefault();
							
							// Stop the Rotator
							$(".js-pause", thisRotator).triggerHandler("click.imageRotator");
						
							// Update the Status of the itemFader, and fade it 
							thisItemFader.faderInfo.prevIndex = thisItemFader.faderInfo.index;
							thisItemFader.faderInfo.index = x;
							thisItemFader.fade();
						});
					}).eq( thisItemFader.faderInfo.index ).triggerHandler("click.button");
						
					
					// Setup the Play / Pause events 
					$(".js-play", thisRotator).bind("click.imageRotator", function(evt) {
						evt.preventDefault();
						$(".js-pause", thisRotator).show();
						$(this).hide();
						
						// Start the Rotator 
						clearInterval( site.data.imageRotatorIntervals[i] );
						site.data.imageRotatorIntervals[i] = setInterval(function() {
							thisItemFader.forward();
						}, 7000);
					});
					
					$(".js-play", thisRotator).triggerHandler("click.imageRotator");
					
					$(window).blur(function(){
						$(".js-pause", thisRotator).triggerHandler("click.imageRotator");
						//alert("js-pause");
					});
					
					$(window).focus(function(){
						$(".js-play", thisRotator).triggerHandler("click.imageRotator");
						//alert("js-play");
					});

					
					$(".js-pause", thisRotator).bind("click.imageRotator", function(evt) {
						evt.preventDefault();
						$(".js-play", thisRotator).show();
						$(this).hide();
						
						// Stop the Rotator
						clearInterval( site.data.imageRotatorIntervals[i] );
					});
					
					// Wait Until All Panel Images have loaded, then start the rotator
					var tempInterval = setInterval( function() {
						if( lib.image.isComplete(".js-imageRotator:eq(" + i + ") img") )
						{ clearInterval(tempInterval); $(".js-play", thisRotator).click(); }
					}, 250);
				}); 
			}
		},
		obj : {
			
			
		}
	};
})();

$(function() {
	
	site.init.imageRotators();

});


