if(typeof console == "undefined" || typeof console.log  == "undefined"){
	console = {
		log:function(){}
	};
}

var g = {
	galleryindex:0,
	conf:{
		assetWidth:223,
		galleryWidth:null,
		screenWidth:1102,
		maxScroll:null,
		minScroll:0,
		tx:0
	},
	
	init:function( _config ){
		var cc = this;
		if(_config)
			$.extend(this,_config);
			
		//$(".assetImg").hide();
		
		if($("body").hasClass("browse")){
			cc.setUpGallery();
			$("#gallery").imagesLoaded( function( $images ){ cc.enterGallery($images); } );
		}else if($("body").hasClass("view")){
			cc.view(); 
		}
	},
	
	view:function(){
		var cc = this;
		var spinner = new Spinner({
		  lines: 12, // The number of lines to draw
		  length: 4, // The length of each line
		  width: 2, // The line thickness
		  radius: 10, // The radius of the inner circle
		  color: '#666', // #rgb or #rrggbb
		  speed: 1, // Rounds per second
		  trail: 60, // Afterglow percentage
		  shadow: false // Whether to render a shadow
		}).spin();
		
		$view = $(".galleryInterface:first");
		$view.find(".artworkWrapper:first").css({ "width": 	$view.find("img.artworkimg:first")[0].width + 370 } );
		$view.find(".artworkWrapper:first").imagesLoaded( function($images){
			spinner.stop();
			$view.children().fadeIn(400);
			$("body>footer").fadeIn(400);
		});
		$view.children().hide();
		$("body>footer").hide();
		$view.append(spinner.el);
		/*find("img.artworkimg:first").bind({ load:function(){
			spinner.stop();
			$view.children().fadeIn(400);
		} });*/
		//$view.append(spinner.el);
		$(spinner.el).css({"position":"absolute", "top":100, "left":(1176/2)});

		if(__next > -1) cc.bindArtworkEvnts($("#scrollLeft"));
		if(__prev > -1) cc.bindArtworkEvnts($("#scrollRight"));
	},
	
	bindArtworkEvnts:function(obj){
		var cc= this;
		if(obj.selector == "#scrollRight"){
			obj.unbind().bind({
				mousedown:function(event){ cc.prevArtwork(event);  },
				mouseover:function(event){ $(this).attr("src", "/assets/img/left_arrow_o.gif")},
				mouseout: function(event){ $(this).attr("src", "/assets/img/left_arrow.gif")}
				}).attr("src", "/assets/img/left_arrow.gif");
		}else{
			obj.unbind().bind({
				mousedown:function(event){ cc.nextArtwork(event);	},
				mouseover:function(event){ $(this).attr("src", "/assets/img/right_arrow_o.gif")},
				mouseout: function(event){ $(this).attr("src", "/assets/img/right_arrow.gif")}
				}).attr("src", "/assets/img/right_arrow.gif");
		}
	},
	
	nextArtwork:function(e){
		if(__next > -1)
			document.location.replace("/art/" + (__next) + "/" + (__nextName));
	},
	
	prevArtwork: function(e){
		if(__prev > -1)
			document.location.replace("/art/" + (__prev) + "/" + (__prevName));
	},
	
	setUpGallery: function(){
		var cc = this;
		$assets = $("#gallery").children();
		$assets.each(function(index,target){
			var pos = cc.conf.assetWidth * index;
			$(target).css({top:0,left:pos});
			var opts = {
				  lines: 12, // The number of lines to draw
				  length: 4, // The length of each line
				  width: 2, // The line thickness
				  radius: 10, // The radius of the inner circle
				  color: '#666', // #rgb or #rrggbb
				  speed: 1, // Rounds per second
				  trail: 60, // Afterglow percentage
				  shadow: false // Whether to render a shadow
				};
			var spinner = new Spinner(opts).spin();
			$(target).children().first().append(spinner.el);
			$(spinner.el).css({"position":"absolute", "top":100, "left":100});
			$(target).find("img").data("spinner", spinner);
		});
		// set #gallery width for scroll
		$("#gallery").width( (cc.conf.assetWidth * $("#gallery").children().length) - 15 );
		cc.getBrowseProperties();
		cc.bindScrollEvnts( $("#scrollLeft") );
	},
	
	enterGallery: function($images){
		var cc = this;
		var $img = $images.eq(cc.galleryindex);
		$img.data("spinner").stop();
		$img.parent().fadeIn(400);
		$img.bind({
			"mouseover":function(){
			$(this).data("title", $(this).attr("title"));
			$(this).removeAttr("title");
			},
			"mouseout": function(){	
				$(this).attr("title", $(this).data("title"));
			}
		});
		console.log(cc.galleryindex, $images.length);
		cc.galleryindex++;
		if(cc.galleryindex < $images.length){
			setTimeout(function(){ g.enterGallery($images); }, 400);
		}
	},
	
	// not used anymore
	browse:function($images){
		//console.log("browse");
		var cc = this;
		$images.each(function(index,target){
			var pos = cc.conf.assetWidth * index;
			$(target).css({top:0,left:pos});
			$(target)
				.find("img:first")
				.bind({
					"load": function(){  
						console.log(this);
						$(this).parent().fadeIn(500);
					},
					"mouseover":function(){
						$(this).data("title", $(this).attr("title"));//.removeAttr("title");
						$(this).removeAttr("title");
					}
				});
		});
		cc.bindScrollEvnts( $("#scrollLeft") );
	},
	
	bindScrollEvnts: function( obj ){
		// they scroll the opposite way
		var cc = this;
		if(obj.selector == "#scrollRight"){
			obj.unbind().bind( {
				mousedown:function(event){ cc.scrollLeft(event);  },
				mouseover:function(event){ $(this).attr("src", "/assets/img/left_arrow_o.gif")},
				mouseout: function(event){ $(this).attr("src", "/assets/img/left_arrow.gif")}
			}).attr("src", "/assets/img/left_arrow.gif").addClass("pointer");
		}else{
			obj.unbind().bind( { 
				mousedown:function(event){ cc.scrollRight(event); },
				mouseover:function(event){ $(this).attr("src", "/assets/img/right_arrow_o.gif")},
				mouseout: function(event){ $(this).attr("src", "/assets/img/right_arrow.gif")}
			}).attr("src", "/assets/img/right_arrow.gif").addClass("pointer");
		}
	},
	
	getBrowseProperties:function(){
		var cc = this;
		cc.conf.galleryWidth = $("#gallery").width();
		cc.conf.maxScroll = 3 - ( cc.conf.galleryWidth - cc.conf.screenWidth );
	},
	
	scrollLeft:function(e){
		var cc = this;
		
		cc.conf.tx += cc.conf.assetWidth * 5 ;
		if(cc.conf.tx > cc.conf.minScroll) cc.conf.tx = cc.conf.minScroll;
		$("#gallery").animate({left:cc.conf.tx}, {duration:1000, easing:'easeOutExpo', queue:false});
		
		cc.bindScrollEvnts($("#scrollLeft"));
		
		if(cc.conf.tx >= cc.conf.minScroll){ 
			$("#scrollRight").mouseout().unbind().attr("src", "/assets/img/left_arrow_d.gif").removeClass("pointer");
			return;
		}
	},
	
	scrollRight:function(e){
		var cc = this;
		
		cc.conf.tx -=  cc.conf.assetWidth * 5;
		if(cc.conf.tx < cc.conf.maxScroll) cc.conf.tx = cc.conf.maxScroll;
		$("#gallery").animate({left:cc.conf.tx}, {duration:1000, easing:'easeOutExpo', queue:false});
		
		cc.bindScrollEvnts($("#scrollRight"));
		
		if(cc.conf.tx <= cc.conf.maxScroll){ 
			$("#scrollLeft").mouseout().unbind().attr("src", "/assets/img/right_arrow_d.gif").removeClass("pointer");
			return;
		}
	}
	
};

$(function(){ g.init();});
