/**
*	writen by jaNg.
**/

try{ document.execCommand('BackgroundImageCache', false, true); } catch(e){};

(function($){
	$.fn.setLinks = function(m){
		$(this).click(function() {
			var href = this.href.match("[0-9]+\.jpg")[0].replace(".jpg", "");
			window.open(m+'/sketch_popup.asp?i='+href,'sketch_popup', 'width=540,height=440,scrollbar=no,toolbar=no');
			return false;
		});
	};
})( jQuery );

/* define easing */
//http://gsgd.co.uk/sandbox/jquery.easing.php
$.easing.backout = function(x, t, b, c, d){ var s=1.70158; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; };

(function($){$.scrollTo=function(a,b){return $('html,body').scrollTo(a,b)};$.fn.scrollTo=function(e,f){f=$.extend({axis:'y',speed:1},f||{});if(f.axis.length!=2)f.queue=false;if(f.queue)f.speed=Math.ceil(f.speed/2);return this.each(function(){var d=$(this),t=e,k,l,u={};switch(typeof t){case'string':if(/^([+-]=)?\d+(px)?$/.test(t))break;t=$(t,this);case'object':k=$(t).offset()}$.each(f.axis.split(''),parse);animate(f.onAfter);function parse(i,a){var b=a=='x'?'Left':'Top',p=b.toLowerCase();var c='scroll'+b;u[c]=k?k[p]+(d.is('html,body')?0:d[0][c]-d.offset()[p]):t;if(f.margin&&typeof t=='object')u[c]-=parseInt($(t).css('margin'+b))||0;if(!i&&f.queue){if(d[0][c]!=u[c])animate(f.onAfterFirst);delete u[c]}};function animate(a){d.animate(u,f.speed,f.easing,a)}})}})(jQuery);
(function($){
	var defaults = {
		elements:'img',//elements selector (relative to view)
		itemSize:{
			width:200,
			height:200
		},
		itemList:null,
		view:null,//container of the elements, the one to fix the width.
		photo:null,
		onStyle:{
			opacity: 1//,
			//marginTop: "-2px"
			//borderWidth: "3px"
		},
		offStyle:{
			opacity: 0.7//,
			//marginTop: "0"
			//borderWidth: "1px"
		},
		navigators:null,//selector to the elements to navigate the slideshow.
		navigationMode:'s',//sequential, random, localscroll, follow
		speed:600,//speed of transition, 1 for no-animation
		wrappers:'simple',//simple,resize,crop,link,view
		circular:false,//should the slideshow rewind/advance on the extremes ?.
		easing:'linear',//easing equation
		axis:'x',//axis to displace the slideshow
		margin:true,//take margin in account?
		start:null, //force the scroll to start at some position element.
		setWidth:false//whether to calculate and set, the overall width of the slideshow.
	};

	function wrap( $els, type, size, $photo ){//wrap the photos with several different wrappers.
		switch( type ){
			case 'crop': $els = $els.wrap('<div class="jq-ss-crop">').parent().css('overflow','hidden');
			case 'resize': return $els.css( size );
			case 'simple': return $els.wrap('<div class="jq-ss-simple">').parent();
			case 'link':
				if( $els.is('img') )
					return $els.wrap('<a target="_blank" class="jq-ss-link">').parent().each(function(){
						this.href = this.firstChild.src;
					});
			case 'view':
				if( $els.is('img') )
					return $els.click(function(){
						$(this).css("opacity", 1);
						$photo.hide().attr("src", $(this).parent().attr("href")).fadeIn("fast");
						return false;
					});
			default: return $els;
		}
	};

	$.fn.scrollShow = function( settings ){
		settings = $.extend( {}, defaults, settings );

		return this.each(function(){
			var
				widget	  = this,
				$view	  = settings.view ? $(settings.view, this) : this,
				$elements = $(settings.elements, $view),
				limit	  = $elements.length,
				active	  = 0,
				itemList  = settings.itemList ? settings.itemList : limit;

			$.each( settings.wrappers.split(/\s*,\s*/), function( i, wrapper ){
				$elements = wrap( $elements, wrapper, settings.itemSize, $(settings.photo) );
			});
			$elements.css( settings.itemSize );//ensure the outer elements have fixed size.
			$elements.css( settings.offStyle );

			if( !settings.navigators ){//this shouldn't get to happen
				settings.navigators = '';
				settings.navigationMode = 'r';
			}

			if( settings.navigators.constructor != Array )
				settings.navigators = [settings.navigators];

			$.each( settings.navigationMode.split(''), function( i, type ){
				switch( type.charAt(0) ){
					case 's'://sequential navigation
						$(settings.navigators[i],widget)
							.eq(0).bind('click', { dir: -1 }, sequential ).end()
							.eq(1).bind('click', { dir: +1 }, sequential );
					break;
					case 'r'://random navigation
						var $nav = $(settings.navigators[i] || $elements, widget),
							ratio = $elements.length / $nav.length;
						if( ratio === Infinity ) return;//no navigator found
						$nav.each(function( pos ){
							$(this).bind( 'click', { pos: Math.floor(ratio*pos) }, random );
						});
					break;
					case 'f'://follow navigation
						var $nav = $(settings.navigators[i] || $elements, widget);

						$nav.each(function( pos ){
							$(this).bind( 'click', { pos: pos - Math.floor(itemList/2) }, random );
						});
					break;
				}
			});

			(function( $e, w ){
				var imgw = ($e.width() + attrs('margin') + attrs('padding') + attr('border'));

				do w -= imgw;
				while( w > 0 && limit > itemList && limit-- );//find the last element we can scroll To.

				if( !settings.setWidth ) return;

				do{
					$e = $e.parent();
					if( $e[0] == $view[0] )
						return;
				}while( $e.length > 1 );
				$e.width( imgw * $elements.length  );//if there's a container for the elements, calculate it's width.

			})( $elements, $view.width() );

			if( settings.start != null )
				if( settings.start != 0 ) {
					var idx = settings.start - 1;
					$(settings.photo).hide().attr("src", $elements.eq(idx).parent().attr("href")).fadeIn("fast");
					random( idx - Math.floor(itemList/2) );
				} else {
					random( settings.start );
				}

			function attrs( name ){
				return attr(name+'Left') + attr(name+'Right');
			};
			function attr( name ){
				return parseInt($elements.css(name)) || 0;
			};

			function sequential( event ){
				event.data.pos = active + event.data.dir;
				return random( event );
			};

			function random( event ){
				e = event.srcElement || event.target;

				var pos = typeof event == 'number' ? event : event.data.pos;
				var cur = (event.data) ? (!e.alt) ? event.data.pos + 1 : active : (settings.start != 0) ? settings.start - 1 : settings.start;

				if( pos < 0 )
					pos = active == 0 && settings.circular ? limit : 0;
				else if( pos > limit )
					pos = active == limit && settings.circular ? 0 : limit;
				else if( limit <= itemList )
					pos = 0;
				
				for(i = 0; i < $elements.length; i++){
					if(cur == i) $elements.eq(i).css( settings.onStyle );
					else $elements.eq(i).css( settings.offStyle );
				}
				$view.stop().scrollTo( $elements[pos], settings );
				active = pos;
				return false;
			};
		});
	};
})( jQuery );