// remove the "$" namespace from jQuery, avoids conflicts with other libraries
jQuery.noConflict();

// closure, mapping jQuery to $, window, document and undefined - useful for minifing tools
(function($, window, document, undefined){

// document ready method
$(function(){

	
	if ($.fn.slideshowFade)
	{
		$('#slideshow1').slideshowFade({
			buttons: false
			//speed: 600, // default 600
			//auto: 5000 // default 5000
		});
	}
	
	if ($.fn.dynamicLabel)
	{
		$('input.dynamic, input[placeholder], textarea[placeholder]').dynamicLabel();
	}


	var jumper = $('.jumper');
	if (jumper.length)
	{
		jumper.each(function(){
			var self = $(this),
				btns = self.find('.jump-btn a'),
				slider = self.find('.jump-slider');

			btns.click(function(){
				var id = this.hash.substr(1).replace(/^go-/, ''),
					item = $('#' + id),
					pos = item.position();
				slider.animate({ top: '-' + Math.ceil(pos.top) + 'px' }, 'normal');
				btns.removeClass('active').filter(this).addClass('active');
				return false;
			});

			slider.find('.top a').click(function(){
				slider.animate({ top: 0 }, 'normal');
				btns.removeClass('active').eq(0).addClass('active');
			});
		});
	}


	if ($.fn.colorbox)
	{
		var p = {
			speed: 500,
			opacity: 0.67,
			scalePhotos: false
		};

		function initModal2()
		{
			var modal = $('#modal-2'), 
				img = $('#modal-2-image'),
				title = $('#modal-2-title');
			modal.find('area').click(function(){
				img.attr('src', this.href);
				title.html(this.title);
				return false;
			});
		}

		function initModal3()
		{
			if ($.fn.slideshowFade)
			{
				var sl = $('#modal-3-slideshow'),
					title = sl.append('<p class="title"></p>').find('.title');
					nav = sl.append('<p class="nav"><a href="#" class="prev">back</a><a href="#" class="next">next</a></p>').find('.nav a');
				sl.bind('slideChanged', function(ev, parent, slides, next){
					title.html(next.find('img').attr('alt'));
				}).slideshowFade({
					buttons: false,
					auto: 0
				});
				nav.click(function(){
					var a = $(this), dir = (a.hasClass('prev') ? -1 : 1);
					sl.trigger('slideAdvance', [dir]);
					return false;
				});
			}
		}


		$('#gallery').
			find('a.modal-1').colorbox($.extend(p, {
				onOpen: function(){
					$('#colorbox')[0].className = 'modal-1';
				}
			})).end().
			find('a.modal-2').colorbox($.extend(p, {
				onOpen: function(){
					$('#colorbox')[0].className = 'modal-2';
				},
				onComplete: initModal2
			})).end().
			find('a.modal-3').colorbox($.extend(p, {
				onOpen: function(){
					$('#colorbox')[0].className = 'modal-3';
				},
				onComplete: initModal3
			})).end();

	}


});

// plugins

// slideshowFade - jQuery plugin - creates a fading slideshow
$.fn.slideshowFade = function(args)
{
	if (!this.length) return this;

	var opts = $.extend({
		slides: '> ul > li',	// slides selector
		buttons: true,			// show buttons
		speed: 600,				// speed of transition
		auto: 5000				// speed of auto transition
	}, args || {});

	return this.each(function(){
		var parent = $(this), slides = parent.find(opts.slides), buttons = $(), timer = null, n = 0, s = '';

		slides.each(function(){
			this.id = parent[0].id + '-' + (++n);
			s += '<a href="#' + this.id + '">' + n + '</a>';
		});

		if (opts.buttons)
		{
			buttons = parent.append('<div class="buttons">' + s + '</div>').
				find('.buttons a').click(function(){
					var hash = this.hash.substr(1);
					parent.triggerHandler('slideChange', [slides.filter('#' + hash)]);
					return false;
				});
		}

		function change(next)
		{
			var active = slides.filter('.active');
			if (!next || !next.jquery)
				next = active.next();
			if (!next.length)
				next = slides.eq(0);

			var stop = parent.triggerHandler('slideChanging', [parent, slides, opts, active, next]);
			if (!stop)
			{
				next.addClass('next');
				active.fadeOut(opts.speed, function(){
					next.addClass('active').removeClass('next');
					active.removeClass('active').show();
					buttons.removeClass('active').filter('[href="#' + next[0].id + '"]').addClass('active');
				});
			}
			else
			{
				buttons.removeClass('active').filter('[href="#' + next[0].id + '"]').addClass('active');
			}

			parent.trigger('slideChanged', [parent, slides, next]);
		}

		function changeNow(next)
		{
			slides.removeClass('active');
			next.addClass('active').show();
			buttons.removeClass('active').filter('[href="#' + next[0].id + '"]').addClass('active');

			parent.trigger('slideChanged', [parent, slides, next]);
		}

		function auto()
		{
			if (opts.auto)
				timer = setInterval(change, opts.speed + opts.auto);
		}

		parent.bind('slideChange', function(ev, slide, now){
			clearInterval(timer);
			now ? changeNow(slide) : change(slide);
			auto();
		});
		parent.bind('slideAdvance', function(ev, delta){
			var dir = (delta > 0), cnt = Math.abs(delta),
				active = slides.filter('.active'), next = active;
			while (cnt--)
			{
				next = active[(dir ? 'next' : 'prev')]();
				if (!next.length)
					next = slides.eq(dir ? 0 : slides.length - 1);
			}
			parent.trigger('slideChange', [next]);
		});
		parent.bind('changeAuto', function(ev, time){
			clearInterval(timer);
			opts.auto = time;
			auto();
		});

		if ((a = slides.filter('.active')) && !a.length)
			changeNow(slides.eq(0));
		else
			changeNow(a);

		auto();
	});
};

// dynamicLabel - jQuery plugin - adds default labels to text inputs
$.support['placeholder'] = ('placeholder' in document.createElement('input'));

if (!$.support['placeholder'])
{
$.fn.dynamicLabel = function(args)
{
	if (!this.length) return this;

	var opts = $.extend({
		className: 'placeholder'
	}, args || {});

	return this.each(function(){
		var t = this, $t = $(t), ph = $t.attr('placeholder'), cls = opts.className, key = '_ph';

		if (!ph.length && t.id)
			ph = $('label[for="' + t.id + '"]').html();
		if (!ph.length && t.value.length)
			ph = t.value;

		t.value = ph;
		$.data(t, key, ph);

		$t.addClass(cls).bind('focus', function(){
			$t.removeClass(cls);
			if (this.value == $.data(this, key))
				this.value = '';
		}).bind('blur', function(){
			if (this.value == '')
			{
				$t.addClass(cls);
				this.value = $.data(this, key);
			}
		});
	});
};
$('form').bind('submit', function(){
	$(this).find('input, textarea').trigger('focus');
});
}


})(jQuery, window, document);

