// 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(){


	setTimeout(function(){
		$('#menu').find('.level1 > li > ul').each(function(){
			var ul = $(this), p = ul.parent();
			ul.width(p.width()).prepend('<li class="title">' + p.find('> a').text() + '</li>');
		});
	}, 800);


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

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

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

		$(window).hashchange(function(){
			var id = location.hash.substr(2),
				a = $('.jump-btns a[href="#!' + id + '"]');
			if (a.length)
				a.click();
			else
				$('.jump-slider .top a').eq(0).click();
		});
		if (location.hash.length)
			$(window).hashchange();
	}


	if ($.fn.jCarouselLite)
	{
		var slideshow1 = $('#slideshow1');
		if (slideshow1.length)
		{
			slideshow1.append('<p class="nav"><a href="#" class="prev">previous photo</a> | <a href="#" class="next">next photo</a></p>');
			slideshow1.find('.slider').jCarouselLite({
				btnPrev: 'a.prev',
				btnNext: 'a.next',
				visible: 1,
				scroll: 1,
				speed: 400,
				auto: false,
				circular: true,
			});
		}
	}


	if ($.fn.slideshowFade)
	{
		$('#slideshow-fade1').slideshowFade({
			buttons: false,
			speed: 500,
			auto: 2000
		});
	}

	if ($.fn.colorbox)
	{
		$('#sizes').find('a').colorbox({
			speed: 500,
			opacity: 0.67,
			photo: true,
			scalePhotos: false
		});
	}


});

// 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();
	});
};


})(jQuery, window, document);

/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);

