$(document).ready(function(){

	// set up the thumbnail behavior
	$('ul.thumbs').thumbies();

	// force browser to set scroll to top left
	$.scrollTo(0);

	// enable gallery scrolling
	$('#strip div.flipper').serialScroll({
		items: 'li',
		axis: 'x',
		start: 1,
		force: true,
		cycle: true,
		duration: 1000000,
		easing: 'linear',
		interval: 1,
		step: 50
	});
	$('#strip div.flipper').stop().trigger('start');

	// fix pngs in ie6
	if($.browser.msie && $.browser.version.split('.')[0] <= 6){
		$('img[src$=.png]').ie6png();
	}

	// initialize movie controls
	var movie = $.browser.msie ? window['fingerprint'] : $('embed')[0];
	if(movie){
		$('#way a').click(function(){
			var txt = $(this).text().toLowerCase();
			switch(txt){
				case 'pause':
					$(this).text('Continue');
					command = 'stop';
					break;
				case 'continue':
					$(this).text('Pause');
					command = 'start';
					break;
				case 'start over':
					command = 'again';
			}
			movie.jsControl(command);
			return false;
		});
	}

});

(function($){
	$.fn.thumbies = function(o){
		return this.each( function(){
			var $self = $(this).parents('div.portitem:eq(0)');
			// store references to important parts
			var $zoom = $('div.zoom', $self);
			var $info = $('div.info', $self);
			var badge = {
				'src': $('img', $zoom).attr('src'),
				'height': $('img', $zoom).height(),
				'width': $('img', $zoom).width()
			}
			var $list = $('ul', $self);

			// resets anchors and list items to their non-selected state
			var reset = function(){
				$('a.here', $list).removeClass('here');
				$('div.waiter', $zoom).remove();
			}

			// handle clicks that are not caught by the anchors
			$self.click(function(){
				// nothing if we haven't changed anything yet
				if($('img', $zoom).attr('src') == badge.src){
					return false;
				}
				// otherwise, put the badge back
				reset();
				$('p', $zoom).fadeOut(function(){
					var toheight = $info.height() - badge.height;
					switch(true){
						// new image is taller than badge
						case toheight < 0:
							$self.animate({'height': badge.height + 'px'});
							break;
						// new image is shorter than badge
						case toheight > 0:
							$self.animate({'height': $info.height() + 'px'});
							break;
						// new image is exactly the same or something weird happened
						default:
							$self.css('height', 'auto');
					}
					$('img', $zoom).attr({'width': badge.width, 'height': badge.height, 'src': badge.src});
					$('p', $zoom).fadeIn();
				});
				return false;
			});

			// handle clicks on the anchors in the list
			$('a', $list).click(function(e){
				if($(this).is('.here')) return false;
				reset();
				$(this).addClass('here');
				// switch zoom url with @rel from clicked thumbnail
				var src = $(e.target).attr('rel').split('|');
				// something weird here -- clicking on a very particular spot results in an error
				if(!src[1]) return false;
				// split the dimensions piece of text
				var dim = src[1].split('x');
				// throw in a waiter
				$('<div class="waiter">&#160;</div>').css('height', $('img', $zoom).height() + 'px').appendTo($zoom);
				// fade out what's already there
				$('p', $zoom).fadeOut(function(){
					var $img = $('img', this);
					$img.load(function(){
						$(this).parent().fadeIn();
						$('div.waiter', $zoom).remove();
					});
					// set the height of the whole block to what it currently is
					$self.css('height', $self.height() + 'px');
					var toheight = $info.height() - dim[1];
					switch(true){
						// new image is taller than info
						case toheight < 0:
							$self.animate({'height': dim[1] + 'px'});
							break;
						// new image is shorter than info
						case toheight > 0:
							$self.animate({'height': $info.height() + 'px'});
							break;
						// new image is exactly the same or something weird happened
						default:
							$self.css('height', 'auto');
					}
					$img.attr({'width': dim[0], 'height': dim[1], 'src': src[0]});
				});
				// tell analytics someone submitted the Contact form
				if( window.pageTracker ){
					var piece = $('img', this).attr('alt').split('.')[0].split('-');
					window.pageTracker._trackPageview(document.location.href + piece[0] + '/' + piece[1]);
				}
				return false;
			});
		});
	}
})(jQuery);

(function($){
	$.fn.ie6png = function(){
		return this.each(function(){
			// get src
			var $self = $(this);
			var src = $self[0].src;
			var blank = new Image();
			blank.src = 'media/ui/blank.gif';

			// fix function
			var fix = function(){
				// set width and height
				if (!$self[0].style.width) { $self[0].style.width = $self.width(); }
				if (!$self[0].style.height) { $self[0].style.height = $self.height(); }
				// replace by blank image
				$self[0].onload = function(){};
				$self[0].src = blank.src;
				// set filter (display original image)
				$self[0].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
			}

			// run the fix function when the image has loaded
			if (!this.complete) {
				this.onload = function() {fix};
			}else{
				fix();
			}

			//$self.load(fix);
		});
	}
})(jQuery);
