/*
Project: bCarousel
Author: Brian DiChiara
Version: 1.3
Usage: Add this js file to your site and simply give any element a class of "bcarousel"
Available Options: 
	wrapper : 'bCarousel-wrapper',	// default: bCarousel-wrapper
	target : 'carousel-target',		// default: carousel-target
	event : 'hover',				// default: click, optional hover
	speed : 600,					// default: 600
	increment: 8,					// default: 8 (for hover)
	easing : 'easeOutQuad'			// default: easeOutQuad, requires easing plugin
*/
var bCarousel={$target:'',$wrapper:'',is_closing:false,options:{wrapper:'bCarousel-wrapper',target:'carousel-target',event:'hover',speed:600,increment:8,easing:'easeOutQuad'},init:function(selector,opts){if(!selector){selector='.bcarousel';}
bCarousel._setopts(opts);
bCarousel.$target=jQuery(selector);bCarousel.$target.addClass(bCarousel.options.target);bCarousel.setup_wrapper();bCarousel.add_controls();},setup_wrapper:function(){bCarousel.$wrapper=jQuery('<div />',{width:bCarousel.$target.width()});bCarousel.$wrapper.addClass(bCarousel.options.wrapper);bCarousel.$target.wrap(bCarousel.$wrapper);bCarousel.$wrapper=bCarousel.$target.parents('.'+bCarousel.options.wrapper+':first');bCarousel.$target.css('width','5000000px');
	var $ul = bCarousel.$target.find(':first-child');
	var total_width = parseInt($ul.css('padding-left').replace('px',''));
	total_width += parseInt($ul.css('padding-right').replace('px',''));
	total_width += parseInt($ul.css('margin-left').replace('px',''));
	total_width += parseInt($ul.css('margin-right').replace('px',''));
	
	var $as = jQuery('a',bCarousel.$target);
	
	jQuery.each($as, function(i, el){
		var $a = jQuery(el);
		var a_width = 0;
		a_width += parseInt($a.css('margin-left').replace('px',''));
		a_width += parseInt($a.css('margin-right').replace('px',''));
		a_width += parseInt($a.css('padding-left').replace('px',''));
		a_width += parseInt($a.css('padding-right').replace('px',''));
		a_width += parseInt($a.css('border-left-width').replace('px',''));
		a_width += parseInt($a.css('border-right-width').replace('px',''));
		a_width += $a.width();
		total_width += a_width;
	});
	
	bCarousel.$target.css('width',(total_width)+'px');
	
},add_controls:function(){$wrapper=bCarousel.$wrapper;var $forward=jQuery('<a />',{html:'&gt;',href:'#forward'});$forward.addClass('forward');var $back=jQuery('<a />',{html:'&lt;',href:'#back'});$back.addClass('back');$wrapper.prepend($forward).prepend($back);if(bCarousel.options.event=='hover'){$forward.hover(function(over){bCarousel.move(bCarousel.$target,'left');},function(out){bCarousel.stop(bCarousel.$target,'left');}).click(function(){return false;});$back.hover(function(over){bCarousel.move(bCarousel.$target,'right');},function(out){bCarousel.stop(bCarousel.$target,'right');}).click(function(){return false;});}else{$forward.click(function(e){bCarousel.move(bCarousel.$target,'left');return false;});$back.click(function(e){bCarousel.move(bCarousel.$target,'right');return false;});}},move:function($target,dir){var params=bCarousel.calculate($target,dir);if(params==false){return;}
if(bCarousel.options.event=='hover'){if(bCarousel.is_closing){$target.stop();bCarousel.is_closing=false;}
var op=(dir=='left')?'-':'+';var movement=op+'='+params.distance;$target.animate({marginLeft:movement},6,function(a){bCarousel.move($target,dir);});}else{$target.animate({marginLeft:params.placement},{queue:false,duration:bCarousel.options.speed},bCarousel.options.easing);}},calculate:function($target,dir){var $wrapper=$target.parents('.'+bCarousel.options.wrapper+':first');var wrapper_width=$wrapper.outerWidth();var target_width=$target.outerWidth();var maxleft=(target_width-wrapper_width)*-1;var marginleft=parseInt($target.css('margin-left'));if(!marginleft){marginleft=0;}
if(dir=='right'){maxleft=0;if(marginleft>maxleft)return false;}else if(marginleft<maxleft)return false;var distance=bCarousel.options.increment;if(bCarousel.options.event=='click'){distance=wrapper_width;}
var placement=(dir=='left')?marginleft-distance:marginleft+distance;if(dir=='left'){if(placement>=maxleft){if(placement==marginleft){return false;}}else{distance=(maxleft-marginleft)*-1;placement=maxleft;}}else{if(placement<=maxleft){if(marginleft==placement){return false;}}else{distance=0-marginleft;placement=maxleft;}}
return{maxleft:maxleft,marginleft:marginleft,distance:distance,placement:placement};},stop:function($target,dir){var do_closeout=false;if($target.is(':animated')){do_closeout=true;}
$target.stop();if(bCarousel.options.event=='hover'&&do_closeout){bCarousel.is_closing=true;var tmp=bCarousel.options.increment;bCarousel.options.increment=40;var params=bCarousel.calculate($target,dir);bCarousel.options.increment=tmp;if(params!=false){var op=(dir=='left')?'-':'+';var movement=op+'='+params.distance;$target.animate({marginLeft:movement},400,bCarousel.options.easing,function(e){bCarousel.is_closing=false;});}else{bCarousel.is_closing=false;}}},_setopts:function(opts){if(typeof(opts)=='object'){for(var opt in opts){bCarousel.options[opt]=opts[opt];}}}}
