(function($){

$.fn.sequence = function(options) {

// multiple elements
if (this.length > 1) {
this.each(function() { $(this).sequence(options)});
return this;
}

// private vars 
this.defaults = {
start: 1,	// start index, set to 'random' or 'rnd' to random start
//interval: 8,	// interval, autoplay, set to false for no auto-play, in seconds
repeat: true,	// repeat at the end
transition: {
mode: 'none',
speed: 10
},
slideSize: 'auto',	// size for slides (used for mouseover and stuff)
hoverNavigation:false,	// enable mouse to change images 
slideClick:false,	// insert callback method for slide clicks
gotoSeqSlide:false	// slide change callback
};
this.options = $.extend({}, this.defaults, options);

// internal vars
this.numSlides = this.find('.seqSlide').length;
if (this.options.start == 'random' || this.options.start == 'rnd') {
this.current = Math.floor(Math.random() * this.numSlides) + 1;
} else {
this.current = this.options.start;
}
if (this.current >= this.numSlides) {
this.current = this.numSlides - 1;
}
this.last = false;
this.elm = $(this);
this.interval = false;
this.mouse = {
x: 0,	// store mouse x relative position on element
y: 0,	// store mouse y relative position on element
over: false	// store mouse over boolena value
};

// init sequence
this.init = function() {

// set slide container to correct width and height
// special note. This is hard coded because Firefox can not process this info fast enough in a iframe to keep up with the bamp. 
// if we ever change the size of the bamp make sure to take these into consideration
if (this.find('.seqSlides').length) {
// auto-detect slide size
if (this.options.slideSize == 'auto') {
this.options.slideSize = {
height:'586px',
width:'960px'
}
}

// don't set size of slides and slide container if not needed
// special note. This is hard coded because Firefox can not process this info fast enough in a iframe to keep up with the bamp. 
// if we ever change the size of the bamp make sure to take these into consideration
if (this.options.slideSize != 'none' && this.options.slideSize != false) {
this.find('.seqSlides, .seqSlide').css({
height:'586px',
width:'960px'//,
//overflow: 'auto'
});
}
}

// set slides to be positioned absolute
this.find('.seqSlide').css('position','absolute');
// hide slides if not hidden allready
this.find('.seqSlide:not(:eq(' + this.current + '))').hide();

// navigation shortcuts
this.find('.first, .next, .prev, .last, .seqNav, .seqSlide, .page, .seqSlides').data('sequence', this);
this.find('.first').click(this.first);
this.find('.next').click(this.next);
this.find('.prev').click(this.previous);
this.find('.last').click(this.last);

// init pagínation buttons if available
this.find('.seqNav .page:eq(' + this.current + ')').addClass('mouseSelected');
this.find('.page').mouseover(function() {


// IE doesnt like things faded down has to do with vis property in ie for mouseovers
// makes the hitboxes fade transparent on mouse over
//$(".page").fadeTo(100, 0.1,function() { 
//$(".page").fadeTo(100, 0.4);
//}); 








if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
// determine position in navigation
var index = $(this).html();
if (!(index = parseInt($(this).html()-1))) {
var index = $(this).parents('.seqNav').find('.page').index($(this));
}
sequence.gotoSeqSlide(index);

// mouse enters the sequence hitbox and turns the bottom bar FFFFFF
$('#bottomBar'+index).css("background-color","#FFFFFF");

});


// mouse leaves sequence hitbox and turns the bottom bar back to ff9900
this.find('.page').mouseleave(function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
var index = $(this).html();
if (!(index = parseInt($(this).html()-1))) {
var index = $(this).parents('.seqNav').find('.page').index($(this));
}
$('#bottomBar'+index).css("background-color","#4b9fe0");
});


// mouse enter handler
this.find('.seqSlide').mouseenter(function() {
var sequence = $(this).data('sequence');
sequence.mouse.over = true;
sequence.stopAuto();
});


// mouse leave handler
this.find('.seqSlide').mouseleave(function() {
var sequence = $(this).data('sequence');
sequence.mouse.over = false;
sequence.auto();
});


g = this.current;
this.current = -1;
this.gotoSeqSlide(g);
// start interval for auto animation
if (this.options.interval > 0) {
this.auto();
}
return this;
};

// public methods
this.auto = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
if (!sequence.interval && sequence.options.interval > 0.001) {
sequence.interval = window.setInterval(function() {
sequence.next();
}, sequence.options.interval * 1000);
}
return this;
}

// check if sequence is running
this.isPlaying = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
return sequence.interval;
}

// stop/play sequence automatic 
this.togglePlayback = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
if (sequence.isPlaying()) {
sequence.stopAuto();
} else {
sequence.auto();
}
},
// stop automatic sequence
this.stopAuto = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
if (sequence.interval) {
window.clearInterval(sequence.interval);
sequence.interval = false;
}
return this;
}
// goto first slide
this.first = function(elm) {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
return sequence.gotoSeqSlide(0);
};
// goto last slide
this.next = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
return sequence.gotoSeqSlide(sequence.current + 1);
};
this.previous = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
return sequence.gotoSeqSlide(sequence.current - 1);
};
this.last = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
return sequence.gotoSeqSlide(sequence.numSlides);
};
this.gotoSeqSlide = function(index, noanimation) {
if (index < 0) {
index = this.numSlides - 1;
}
if (index >= this.numSlides) {
index = 0;
}
if (index === this.current) return this;
// get slide elements
var oldSlide = this.find('.seqSlide:eq(' + this.current +')');
var newSlide = this.find('.seqSlide:eq(' + index +')');

// callbacks for animation finished
oldFinished = function () {
$(this).removeClass('selected');
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
sequence.elm.find('.seqNav .page:eq(' + sequence.current + ')').addClass('mouseSelected');
var findingRealCurrent = sequence.current//+1
//alert('#bottomBar'+findingRealCurrent);
//$('#bottomBar'+findingRealCurrent).css("background-color","#FFFFFF");
if (!sequence.mouse.over) {
sequence.auto();
}
}

newFinished = function() {
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
sequence.elm.find('.seqNav .page:not(:eq(' + sequence.current + '))').removeClass('mouseSelected');



$(this).addClass('selected');
}
// get sequence
if (!(sequence = $(this).data('sequence'))) {
var sequence = this;
}
sequence.stopAuto();
// call callback
if (typeof(this.options.gotoSeqSlide) == 'function') {
this.options.gotoSeqSlide(sequence, index);
}

// start transition
if (noanimation) {
oldSlide.hide(1, oldFinished);
newSlide.show(1, newFinished);
} else {
if (typeof(this.options.transition.mode) == 'function') {
this.call(this.options.transition.mode, newSlide, oldSlide);
} else {
switch(this.options.transition.mode) {
default:
case 'fade':
oldSlide.fadeOut(this.options.transition.speed, oldFinished);
newSlide.fadeIn(this.options.transition.speed, newFinished);
break;
}
}
}
this.last = this.current;
this.current = index;
return this;
};

return this.init();
}

})(jQuery);


