/**
 * @author Stéphane Roucheray 
 * @contributer Sam Cleaver
 * @extends jquery
 */


jQuery.fn.carousel = function(previous, next, options){
	var sliderList = jQuery(this).children()[0];
	
	if (sliderList) {
		var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		if(numElmts<shownInViewport)
			compareviewport = numElmts;
		else
			compareviewport = shownInViewport;
		if(compareviewport==1)
			compareviewport = 0;
		for (i = 0; i < compareviewport; i++) {
			
			jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
			jQuery(sliderList).append(jQuery(elmnts[i]).clone());
			
		}
		
		jQuery(previous).click(function(event){
			if (!isAnimating&&compareviewport>0) {
				if (firstElementOnViewPort == 1) {
					jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
					firstElementOnViewPort = numElmts;
				}
				else {
					firstElementOnViewPort--;
				}
				
				jQuery(sliderList).animate({
					left: "+=" + increment,
					y: 0,
					queue: true
				}, "swing", function(){isAnimating = false;});
				isAnimating = true;
			}
			
		});
		
		jQuery(next).click(function(event){
			if (!isAnimating&&compareviewport>0) {
				if (firstElementOnViewPort > numElmts) {
					firstElementOnViewPort = 2;
					jQuery(sliderList).css('left', "0px");
				}
				else {
					firstElementOnViewPort++;
				}
				jQuery(sliderList).animate({
					left: "-=" + increment,
					y: 0,
					queue: true
				}, "swing", function(){isAnimating = false;});
				isAnimating = true;
			}
		});
	}
};
$(document).ready(function(){
    $('#screen').carousel('#left', '#right');
	numElmts = $('#screen').children('ul').children().length;
	shownInViewport = Math.round($('#screen').width() / $('#screen').children('ul').children().outerWidth("true"));
	if(numElmts>=shownInViewport)
		{
		Autorotate = window.setInterval(function() { $('#right').click(); }, 6000);
		$('.slider').bind('mouseenter',function() { window.clearInterval(Autorotate); });
		$('.slider').bind('mouseleave', function() { Autorotate = window.setInterval(function() { $('#right').click(); }, 6000); });
		}
});
/*
 * (Appended to carousel script to reduce connections)
 * Chained - jQuery chained selects plugin
 *
 * Copyright (c) 2010 Mika Tuupola
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */
 (function($){$.fn.chained=function(parent_selector,options){return this.each(function(){var self=this;var backup=$(self).clone();$(parent_selector).each(function(){$(this).bind("change",function(){$(self).html($(backup).html());var selected="";$(parent_selector).each(function(){selected+="\\"+$(":selected",this).val();});selected=selected.substr(1);var first=$(parent_selector).first();var selected_first=$(":selected",first).val();$("option",self).each(function(){if(!$(this).hasClass(selected)&&!$(this).hasClass(selected_first)&&$(this).val()!==""){$(this).remove();}});if(1==$("option",self).size()&&$(self).val()==""){$(self).attr("disabled","disabled");}else{$(self).removeAttr("disabled");}});$(this).trigger("change");});});};$.fn.chainedTo=$.fn.chained;})(jQuery);
 $(document).ready(function(){
	$("#town").chained("#BusinessNeighborhood"); 
 });
