$.widget('ui.scrollbar', {

	_create: function() {
		this.button1 = this.element.find('.button1');
		this.button2 = this.element.find('.button2');
		this.element.find('.button1, .button2').bind('click', this, function(event) {
			event.preventDefault();
			event.data.clickInterval($(this).hasClass('button1') ? -1 : 1);
		}).mousedown($.delegate(this.click, this, true)).bind('mouseup mouseleave', $.delegate(this.click, this, false));
		this.track = this.element.find('.track');
		this.slider = this.track.find('*');
		
		var height = this.options.height;
		
		if (height) {
			height -= 40;
			this.track.css('height', height);
			this.slider.css('height', height - 23);
		}
		
		this.slider.slider({
			orientation: 'vertical',
			min: 0,
			max: 100,
			value: 100,
			slide: $.delegate(this.scroll, this)
		});
		this.scroll();
	},
	
	
	click: function(down, event) {
		var offset = ($(event.currentTarget).hasClass('button1') ? -1 : 1);

		event.preventDefault();

		if (down) {
			this.interval = setInterval($.delegate(this.clickInterval, this, offset), 100);
		}
		else {
			if (this.interval) {
				clearInterval(this.interval);
				this.interval = null;
			}
		}
	},
	
	
	clickInterval: function(offset) {
		var value = this.slider.slider('value');

		this.slider.slider('value', value + (-offset * Math.max(1, (1000 / this.limit))));
		this.scroll();
	},
	

	scroll: function(event, ui) {
		if (!ui) ui = { value: this.slider.slider('value') };

		this.element.show();
		if (this.options.scroll) this.limit = this.options.scroll.apply(this.element, [(100 - ui.value) / 100]);
		if (isNaN(this.limit)) this.limit = 1000;
		this.element[(this.limit > 0 ? 'show' : 'hide')]();
	}

});


function scrollbar($scrollbar, height) {
	var $parent = $scrollbar.next();
	var $target = $parent.find('*:first');

	$parent.css('height', height);
	$scrollbar.scrollbar({
		height: height,
		scroll: function(value) {
			var y;
			var limit = Math.max(0, $target.outerHeight() - $parent.outerHeight());

			y = -(value * limit);
			y = Math.round(y);
			$target.css('margin-top', y);

			return limit;
		}
	});	
}


$().ready(function() {
	$('#popup').popup();

	$('#gallery ul').gallery({
		player: 'http://www.miamiadschool.com/assets/swf/player.swf'
	});
	
	$('.video a').click(function(event) {
		event.preventDefault();
		
		if ($.browser.ipad || $.browser.iphone) window.scrollTo(0, 0);

		$('#popup').popup('show', 'video', {});
		$('#popup .video .player').video({ value: $(event.target).closest('.video').attr('rel') });
	});
	
	$('.introduction .column2 .scrollbar').each(function() {
		scrollbar($(this), Math.min(1000, $(this).closest('.block-programs').find('.column1 .description').height()));																												 
	});
	
	$('.introduction-media .column2 .scrollbar').each(function() {
		scrollbar($(this), Math.min(1000, $(this).closest('.block-programs').find('.column1').height()));																														 
	});
	
	var $contact = $('.block-programs .introduction .contact form');
	var $input;
	
	$contact.validate();
  
	$input = $contact.find('input[name=stickerbook]');
   $input.change(function() {
      $contact.find('table tr.address').toggleClass('hidden', !$(this).is(':checked'));
   });
   $input.trigger('change');

	$contact.find('select[name=state_options]').each(function() {
		$contact.find('input[name=state]').remove();
		$(this).removeClass('hidden');
		$(this).attr('name', 'state');
	});

   $contact.find('select[name=country]').change(function() {
      $contact.validate('destroy');
      $contact.attr('action', 'http://www.miamiadschool.com/advertising-programs/');
      $contact[0].submit();
   });
});
