jQuery.fn.bcAutoHeight = function() {
	return this.each(function() {
		var $this = jQuery(this);
		$this.height(1);
		var h = $this.parent().height();
		$this.height(h);
	});
}

jQuery.fn.bcSameHeight = function() {
	var h = 0;
	this.each(function() {
		var e = jQuery(this).height()
		if (e > h) {
			h = e;
		}
	});
	return this.height(h);
}

jQuery.fn.bcCarousel = function(options) {
	var settings = {
		speed: 800,
		delay: 4000,
		spread: 4,
		auto: false
	};
	if (options) { 
		jQuery.extend(settings,options); // use the settings object, not options parameter!
	}
	var u = this.eq(0).outerWidth(true);
	var w = (u * this.length);
	var h = this.eq(0).outerHeight();
	var len = this.length;
	var $group = jQuery(document.createElement('div'));
	$group.css({
		width: w +'px',
		position: 'absolute',
		left: 0,
		top: 0
	});
	var $wrapper = jQuery(document.createElement('div'));
	$wrapper.css({
		width: u*settings.spread + (this.eq(0).outerWidth(true) - this.eq(0).outerWidth(false)) +'px',
		height: h +'px',
		overflow: 'hidden',
		position: 'relative'
	});	//<div class="wrapper" style="width: '+ u*settings.spread +'px; overflow: hidden; position: relative;"><div class="group" style="width: '+ w +'px;"/></div>
	this.eq(0).before($wrapper);
	$wrapper.append($group);
	this.each(function(){
		jQuery(this).appendTo($group);
	});
	if (settings.auto === true) {
		window.setInterval(function() {
			if (($group.css('left').replace('px','')*-1) < u * (len - settings.spread)) {
				$group.animate({left: '-='+u},settings.speed);
			} else {
				$group.animate({left: 0},settings.speed);
			}
		},settings.delay);
	} else {
		var inmotion = false;
		var $left = jQuery(document.createElement('a')).css({float:'left',width:'20px',height:h+'px',display:'block'}).addClass('carouselNav').attr('href','javascript:void(0);');
		var $right = $left.clone().css({float:'right'});
		$right.addClass('right').click(function() {
			if (($group.css('left').replace('px','')*-1) < u * (len - settings.spread) && inmotion == false) {
				inmotion = true;
				$group.animate({left: '-='+u},settings.speed,function() { inmotion = false });
			}
		});
		$left.addClass('left').click(function() {
			if (($group.css('left').replace('px','')*1) < 0 && inmotion == false) {
				inmotion = true;
				$group.animate({left: '+='+u},settings.speed,function() { inmotion = false });
			}
		});
		$wrapper.before($left);
		$wrapper.before($right);
	}
	return this;
}

jQuery.fn.bcLightBox = function(options) {
	var settings = {
		speed: 800,
		delay: 4000,
		mode: 'group'
	};
	if (options) { 
		jQuery.extend(settings,options); // use the settings object, not options parameter!
	}
	var close = function() {
		jQuery('#bcLightBox').animate({
			opacity: 0
		},Math.round(settings.speed/2),function() {
			jQuery('#bcLightBox').css({display:'none',opacity:1});
		});
		jQuery('#bcLightBoxBg').animate({
			opacity: 0
		},Math.round(settings.speed/2),function() {
			jQuery('#bcLightBoxBg').css('display','none');
		});
	};
	var arr = this;
	showImage = function(url,index) {
		$lbc = 
		jQuery('#bcLightBoxContent').empty();
		jQuery('#bcLightBoxPrev,#bcLightBoxNext').hide();
		$bg = jQuery('#bcLightBoxBg');
		if ($bg.css('display') != 'block') {
			$bg.css({opacity:0,display:'block'}).animate({
				opacity: 0.9
			},Math.round(settings.speed/2));
		}
		var img = new Image();
		img.src = url;
		img.onload = function() {
			img.onload = null;
			var w = img.width;
			var h = img.height;
			if (index > 0) {
				jQuery('#bcLightBoxPrev').unbind('click').show().click(function() {
					jQuery('#bcLightBoxContent img').fadeOut(Math.round(settings.speed/2), function(){
						showImage(arr.eq(index-1).attr('href'),index-1);
					});
				})
			}
			if (index < arr.length-1) {
				jQuery('#bcLightBoxNext').unbind('click').show().click(function() {
					jQuery('#bcLightBoxContent img').fadeOut(Math.round(settings.speed/2), function(){
						showImage(arr.eq(index+1).attr('href'),index+1);
					});
				})
			}
			$lb = $('#bcLightBox');
			if ($lb.css('display') != 'block') {
				$lb.css({
					display:'block',
					overflow: 'hidden',
					width: 0,
					height: 0,
					marginTop: 150
				});
			}
			$lb.animate({
				marginTop: 50,
				width: w,
				height: h
			},settings.speed,function() {
				$(img).css({opacity:0}).appendTo('#bcLightBoxContent').animate({
					opacity: 1
				},settings.speed);
			});
		}
	};
	if (!document.getElementById('bcLightBoxBg')) {
		$('body').append('<div id="bcLightBoxBg" style="display:none;background:#000;height:100%;width:100%;z-index:99999;position:fixed;top:0;left:0;"></div><div id="bcLightBoxContainer" style="position:fixed;top:0;left:0;width:100%;height:0;z-index:100000"><div id="bcLightBox" style="position:relative;top:50%;margin: 20px auto;z-index:100001;background:#fff;padding:2px;display:none;"><div id="bcLightBoxLoading" style="position:absolute;top:0;left:0;width:100%;height:100%;z-index:100002"></div><div id="bcLightBoxContent"style="position:relative;z-index:100003"></div><div id="bcLightBoxClose" style="position:absolute;cursor:pointer;z-index:100004;"></div></div></div>');
		$('#bcLightBoxBg,#bcLightBoxClose').click(function() {
			close();
		});
		if (settings.mode != 'single') {
			jQuery('#bcLightBox').append('<div id="bcLightBoxPrev" style="position:absolute;cursor:pointer;z-index:100004;"></div><div id="bcLightBoxNext" style="position:absolute;cursor:pointer;z-index:100004;"></div>');
		}
	}
	return this.each(function(i) {
		var $this = jQuery(this);
		$this.click(function(e) {
			e.preventDefault();
			var h = $this.attr('href');
			showImage(h,i);
		});
	});
}

jQuery.fn.bcListRotate = function(selector,options) {
	var settings = {
		speed: 800,
		delay: 4000,
		visible: 3,
	};
	if (options) { 
		jQuery.extend(settings,options); // use the settings object, not options parameter!
	}
	return this.each(function() {
		var p = jQuery(this);
		if (p.find(selector).length <= settings.visible) {
			return true;
		}
		p.find(selector).slice(settings.visible).hide();
		window.setInterval(function() {
			p.find(selector).eq(0).animate({
				height: 0,
				opacity: 0
			},settings.speed, function() {
				p.find(selector).eq(settings.visible).css({height:'auto',opacity:0,display:'block'}).animate({
					opacity: 1
				},settings.speed);
				$(this).remove().appendTo(p).hide();
			});
		}, (settings.delay + settings.speed));
	});
}
$(document).ready(function() {
	$("ul#menu li").hover(function() {
		$(this).addClass("over");
	}, function() {
		$(this).removeClass("over");
	});
	$('#customerQuotes').bcListRotate('div.customer',{delay:8000}).find('div.quote p:last-child').append('<img src="/img/quote-right.png" alt=""/>');;
});

$(window).load(function() {
	$('#productBrowser .product').bcSameHeight().bcCarousel({spread:4/*,auto:true,delay:1000*/});
	$('.newsItem').bcSameHeight();
	$('div#feature div.morePhotos').bcAutoHeight();
	$('ul.imageGallery a').bcLightBox();
});
