
$(function(){
	var awesome = new slider($('#feature-slider'));
  //loop map items
	if($("#map").length){
  	$('#map li').each(function(){
	    //split it
	    var posA = $(this).attr('rel').split('_');
	    $(this).css('top',(Math.random()*290)+'px');
	    $(this).css('left',(Math.random()*410)+'px');
	    $(this).animate({'opacity':'1.0','top':parseInt(posA[1].substring(1))-6+'px','left':parseInt(posA[0].substring(1))-6+'px'},500);
	  });
	}
  
});

function slider(elem){
	this.elem = elem;
	this.slideIndex = 0;
	this.modW = 809;
	this.slideMax = $(this.elem).find('.item').length;
	
	$(this.elem).find('.items').css('width',(this.modW*this.slideMax)+'px');
	this.isPlaying = true;
	this.slideInterval = 0;
	var scope = this;
	$(this.elem).find('.nav-prev a').click(function(){
		scope.advance(-1);
	});
	$(this.elem).find('.nav-next a').click(function(){
		scope.advance(1);
	});
		
	//hook up nav items
	$(this.elem).find('.nav-slides a').click(function(){
		scope.slideIndex = $(this).parent().index();		
		scope.viewSlide();
	});
	
	//set interval
	this.slideInterval = setInterval(function(){ scope.advance.apply(scope,[1]); },15000);
	
	this.viewSlide();
}

slider.prototype.advance = function(dir)
{
	clearInterval(this.slideInterval);
	if(this.isPlaying){
		var scope = this;
		this.slideInterval = setInterval(function(){ scope.advance.apply(scope,[1]); },15000);
	}
	switch(true)
	{
		case this.slideIndex + dir > this.slideMax - 1:
			this.slideIndex = 0;
		break;

		case this.slideIndex + dir < 0:
			this.slideIndex = this.slideMax - 1;
		break;

		default:
			this.slideIndex += dir;
		break;
	}
	this.viewSlide();
}

slider.prototype.viewSlide = function()
{
	// overlay
	var source = $(this.elem).find(".item:eq("+this.slideIndex+") div").clone();
  $(this.elem).find(".overlay .title").empty();
  var promo = $("p.promo", source);
  var title = $("h2", source);
  var desc = $("p", source);
  var link = $("a", source);
  $(source).css('display', 'block');
  $(this.elem).find('.overlay .title').append(title, desc, link);
  if (promo) {
    $("#sliderinfo .title").prepend(promo);
  }
	
	//update nav state
	$(this.elem).find('.nav-slides .active').removeClass('active');
	$(this.elem).find('.nav-slides li:eq('+this.slideIndex+') a').addClass('active');
	
	//slide things
	$(this.elem).find('.items').animate({'left':-this.slideIndex*this.modW},500,'easeInOutQuad');
	
}
