/**
 * @author 	Christopher Darling [www.christopherdarling.co.uk]
 * @date 	06/10/2009
 * 
 */

var opus = {
	common : {
		init : function() {
			$('#content').css("height", ($(window).height()-154));
		}
	},
	gallery : {
		thumbs: '',
		viewer: '',
		viewer_maxHeight : 0,
		viewer_maxWidth : 550,
		viewer_loading : '',
		
		init: function(){
			opus.gallery.thumbs = $('#content.GalleryPage #thumbs');
			opus.gallery.viewer = $('#content.GalleryPage #viewer');
			
			if( $('#thumbs .inner').height() > $('#thumbs').height() ) {
				opus.gallery.slider();
			}
			
			opus.gallery.viewer_maxHeight = $('#content').height();
			
			var activeThumb = opus.gallery.thumbs.find('a[href]').filter(".active").attr("href");
			opus.gallery.viewer.append('<div class="loading"></div><img class="active" src="'+activeThumb+'" />');
			opus.gallery.viewer_loading = opus.gallery.viewer.find('.loading');

			opus.gallery.thumbs.find("a[href]")
				.hover(function() {
					if( !$(this).hasClass("active") ) {
						$(this).find("img").fadeTo(100, 0.8);
					}
				}, function() {
					if( !$(this).hasClass("active") ) {
						$(this).find("img").fadeTo(100, 0.6);
					}
				}).click(function() {
					opus.gallery.change(this.href);
					opus.gallery.thumbs.find('a').removeClass("active");
					$(this).addClass('active');
					
					return false;
				});
		},
		slider_running : false,
		slider : function() {
			var maxTop = $('#thumbs .inner').height() - opus.gallery.thumbs.height();//$('#thumbs .inner a:last').height();
			var thumbsPos = opus.gallery.thumbs.offset();
			var inner = opus.gallery.thumbs.find(".inner");

			opus.gallery.thumbs
				.mouseleave(function(e) {
					opus.gallery.slider_running = false;
					
				})
				.mousemove(function(e) {
					var innerMarginTop = parseInt(opus.gallery.thumbs.find(".inner").css("marginTop"));
					var distance;
					
					if( (e.clientY - thumbsPos.top) < (opus.gallery.thumbs.height()*0.15) ) {
						// top 15%
						if(!opus.gallery.slider_running) {
							opus.gallery.slider_running = true;
							distance = Math.abs(0+innerMarginTop);
							//alert("maxTop = "+maxTop+"  |  innerMarginTop = " +innerMarginTop+"  |  distance = " +distance);
							inner.animate({marginTop:0}, distance*4.5, function() { opus.gallery.slider_running = false; });
							//opus.gallery.viewer.html("maxTop = "+opus.gallery.thumbs.height()+"<hr />Y poss = "+(e.clientY-thumbsPos.top)+"<hr />");
						}
					}
					else if( (e.clientY - thumbsPos.top) > (opus.gallery.thumbs.height()*0.85) ) {
						if(!opus.gallery.slider_running) {
							//bottom 15%
							opus.gallery.slider_running = true;
							distance = maxTop+innerMarginTop;
							//alert("maxTop = "+maxTop+"  |  innerMarginTop = " +innerMarginTop+"  |  distance = " +distance);
							inner.animate({marginTop:0-maxTop}, distance*4.5, function() { opus.gallery.slider_running = false; });
							//opus.gallery.viewer.html("bottom 35%");
						}
					}
					else {
						//OFF
						opus.gallery.slider_running = false;
						inner.stop(1);
						//opus.gallery.viewer.html("MIDDLE");
					}
					//$('#viewer').html( (e.clientX - thumbsPos.left) + ", " + (e.clientY - thumbsPos.top) );
				});
		},
		change : function( _new ) {
			var active = opus.gallery.viewer.find(".active");
			//var tempImage = new Image();
			//tempImage.src=_new;
			
			opus.gallery.viewer.find(".text").fadeOut(250, function() { $(this).remove(); });

			active.fadeOut(250, function() {
				opus.gallery.viewer_loading.fadeIn(100);
				active.attr("src", _new).load(function() {
					opus.gallery.viewer_loading.fadeOut(100, function() {
						active.fadeIn(800);
					});
				});
				//var new_ratio = tempImage.height / tempImage.width;

				//alert(tempImage.width);
				//if( tempImage.width >= opus.gallery.viewer_maxWidth ) {
				//	alert("height = "+(550*new_ratio));
				//}

				//if(tempImage.height >= opus.gallery.viewer_maxHeight) {
				//	active.css("height", opus.gallery.viewer_maxHeight);
				//} else {
				//	active.css("height", "auto");
				//}
				//active.fadeIn(800);
			});
		}
	}
};

document.getElementsByTagName("body")[0].setAttribute("class","js");
$(window).bind("load", function() {
	
	opus.common.init();
	
	if( $('#content').hasClass("GalleryPage") ) {
		opus.gallery.init();
	}
});