// the Sliding Tabs mootools plugin is a creation of Jenna ?Blueberry? Fox!
// Jenna released it under a donationware license on the 7th of December ?07
// Use of sliding Tabs for more than evaluation requires donation unless you
// really cannot afford a couple of bucks. Regardless, I'd like to know where
// the script gets used! My email address is at http://creativepony.com/#contact
// Documentation: http://creativepony.com/journal/scripts/sliding-tabs/
// version: 1.6.1

var offersScrollerId = 0;

window.addEvent('load', function () {
	loadSlidingTabs();
});	

function loadSlidingTabs() {
	// obtain collection of all divs of class "offersPanes"
	var panesCollection = $(document).getElements('div[class=offersPanes]');
	// obtain collection of all divs of class "offersButtons"
	var buttonsCollection = $(document).getElements('ul[class=offersButtons]');
	// obtain collection of all divs of class "offersPrevious"
	var previousCollection = $(document).getElements('img[class=offersPrevious]');
	// obtain collection of all divs of class "offersNext"
	var nextCollection = $(document).getElements('img[class=offersNext]');

	
	// loop over "offersPanes" / "offersButtons" divs and "offersPrevious" / "offersNext" iamges to incrementally give them a unique id value
	for( var i = 0; i < panesCollection.length; ++i ){
		$(panesCollection[i]).setProperty('id', 'offersPanes'+(i+1));
		$(buttonsCollection[i]).setProperty('id', 'offersButtons'+(i+1));
		$(previousCollection[i]).setProperty('id', 'offersPrevious'+(i+1));
		$(nextCollection[i]).setProperty('id', 'offersNext'+(i+1));
	}
	
	

	// check for number of offer scrollers that exist in the page
	var offerScrollerCount = $(document).getElements('div[class=offersScroller]');

	// setup correct number of instances of SlidingTabs depending on how many offersScroller instances there are in the page
	if( offerScrollerCount.length > 0 ){
		for( var i = 1; i <= offerScrollerCount.length; ++i ){
			offersScrollerId = i;
			window["slidingtabs"+i] = new SlidingTabs('offersButtons'+i, 'offersPanes'+i);
			
			// this sets up the previous/next buttons (binds them to the instance of SlidingTabs
			$('offersPrevious'+i).addEvent('click', window["slidingtabs"+i].previous.bind(window["slidingtabs"+i]));
			$('offersNext'+i).addEvent('click', window["slidingtabs"+i].next.bind(window["slidingtabs"+i]));	
			
			// this takes care of making the next button grey if the number of panels is 1 or less than 1							
			var panelCount = $('offersPanes'+i).getElements('div[class=scrollItem]');
			if (panelCount.length <= 2){
					$('offersNext'+ i ).src = '/shared/resource/images/arrow_right_gray.gif';
					$('offersNext'+i).style.cursor = 'default';	
			}

		}
	}
	if ( document.getElementById('mainContent') ) {
		document.getElementById('mainContent').style.display = 'inline-block';
	}
}

var SlidingTabs = new Class({

	options: {
		startingSlide: false, // sets the slide to start on, either an element or an id 
		activeButtonClass: 'active', // class to add to selected button
		activationEvent: 'click', // you can set this to ?mouseover? or whatever you like
		wrap: true, // calls to previous() and next() should wrap around?
		slideEffect: { // options for effect used to animate the sliding, see Fx.Base in mootools docs
			duration: 400 // half a second
		},
		animateHeight: true, // animate height of container
		rightOversized: 0 // how much of the next pane to show to the right of the current pane
	},
	current: null, // zero based current pane number, read only
	buttons: false,
	outerSlidesBox: null,
	innerSlidesBox: null,
	panes: null,
	fx: null, // this one animates the scrolling inside
	heightFx: null, // this one animates the height


	initialize: function(buttonContainer, slideContainer, options) {


		if (buttonContainer) { this.buttons = $(buttonContainer).getChildren(); }
		this.outerSlidesBox = $(slideContainer);
		this.innerSlidesBox = this.outerSlidesBox.getFirst();
		this.panes = this.innerSlidesBox.getChildren();	

		this.setOptions(options);

		this.fx = new Fx.Scroll(this.outerSlidesBox, this.options.slideEffect);
		this.heightFx = this.outerSlidesBox.effect('height', this.options.slideEffect);

		// set up button highlight
		this.current = this.options.startingSlide ? this.panes.indexOf($(this.options.startingSlide)) : 0;
		if (this.buttons) { this.buttons[this.current].addClass(this.options.activeButtonClass); }

		// add needed stylings
		this.outerSlidesBox.setStyle('overflow', 'hidden');
		this.panes.each(function(pane, index) {
			pane.setStyles({
			 'float': 'left',
			 //'width': (this.outerSlidesBox.offsetWidth.toInt() - this.options.rightOversized) + 'px',
			 'overflow': 'hidden'
		  });
		}.bind(this));

		// stupidness to make IE work - it boggles the mind why this has any effect
		// maybe it's something to do with giving it layout?
		this.innerSlidesBox.setStyle('float', 'left');

		this.innerSlidesBox.setStyle(
			'width', (this.outerSlidesBox.offsetWidth.toInt() * this.panes.length) + 'px'
		);

		if (this.options.startingSlide) this.fx.toElement(this.options.startingSlide);

		// add events to the buttons
		if (this.buttons) this.buttons.each( function(button) {
		  button.addEvent(this.options.activationEvent, this.buttonEventHandler.bindWithEvent(this, button));
		}.bind(this));

		if (this.options.animateHeight)
		  this.heightFx.set(this.panes[this.current].offsetHeight);


	},


	changeTo: function(element, instanceValue) {
		var event = { cancel: false, target: $(element) };
		this.fireEvent('change', event);
		if (event.cancel == true) { return; };

		if (this.buttons) { this.buttons[this.current].removeClass(this.options.activeButtonClass); };
		this.current = this.panes.indexOf($(event.target));

		if (this.buttons) { this.buttons[this.current].addClass(this.options.activeButtonClass); };
		this.fx.stop();
		this.fx.toElement(event.target);
		if (this.options.animateHeight)
		  this.heightFx.start(this.panes[this.current].offsetHeight);

		// change next / previous arrow states depending on which pane is selected
		if (this.current == this.panes.length-1 ){
			//var rightArrowGray = ($('offersNext'+instanceValue).src).split("arrow_");
			$('offersNext'+instanceValue).src = '/shared/resource/images/arrow_right_gray.gif';
			$('offersNext'+instanceValue).style.cursor = 'default';	
		}
		else{
			//var rightArrow = ($('offersNext'+instanceValue).src).split("arrow_");
			$('offersNext'+instanceValue).src = '/shared/resource/images/arrow_right.gif';
			$('offersNext'+instanceValue).style.cursor = 'pointer';	
		}
		if (this.current <= 0){
			//var leftArrowGray = ($('offersNext'+instanceValue).src).split("arrow_");				
			$('offersPrevious'+instanceValue).src = '/shared/resource/images/arrow_left_gray.gif';
			$('offersPrevious'+instanceValue).style.cursor = 'default';	
		}
		else{
			//var leftArrow = ($('offersNext'+instanceValue).src).split("arrow_");								
			$('offersPrevious'+instanceValue).src = '/shared/resource/images/arrow_left.gif';
			$('offersPrevious'+instanceValue).style.cursor = 'pointer';
		}

	},

	// Handles a click
	buttonEventHandler: function(event, button) {
		if (event.target == this.buttons[this.current]) return;
		this.changeTo(this.panes[this.buttons.indexOf($(button))], ($(this.panes[0]).getParent().getParent().id).split("offersPanes")[1]);
	},

	next: function() {
		// find the incremental id of the "panes" div to determine which button to affect
		instanceValue = ($(this.panes[0]).getParent().getParent().id).split("offersPanes")[1];
		var next = this.current + 1;
		if (next == this.panes.length-1){
				$('offersNext'+instanceValue).src = 'images/arrow_right_gray.gif';
				$('offersNext'+instanceValue).style.cursor = 'default';	
		}
		else if (next == this.panes.length) {
			if (this.options.wrap == true) {
				next = this.current;
			} else { 
				return; 

			}	
		}
		else{
			$('offersPrevious'+instanceValue).src = 'images/arrow_left.gif';
			$('offersPrevious'+instanceValue).style.cursor = 'pointer';

		}
		this.changeTo(this.panes[next], instanceValue);
	},

	previous: function() {
		// find the incremental id of the "panes" div to determine which button to affect
		instanceValue = ($(this.panes[0]).getParent().getParent().id).split("offersPanes")[1];
		var prev = this.current - 1
		if (prev <= 0) {
			if (this.options.wrap == true) { 
				prev = 0;
				$('offersPrevious'+instanceValue).src = 'images/arrow_left_gray.gif';
				$('offersPrevious'+instanceValue).style.cursor = 'default';				
			} else {
			}
		}
		else{
				$('offersNext'+instanceValue).src = 'images/arrow_right.gif';
				$('offersNext'+instanceValue).style.cursor = 'pointer';								
			}
		this.changeTo(this.panes[prev], instanceValue);
	}

});

SlidingTabs.implement(new Options, new Events);


