(function ($) {
	$(document).ready(function () {

		if (!$.browser.msie) {			
			$('#Gallery').livequery(function () {
				var sidebar= $('#Sidebar');
				var l=sidebar.css('left');
				$(this).hover(function () {
					sidebar.stop().animate({opacity:0.3},950)
				},function () {
					sidebar.stop().animate({opacity:1},550)
				})
			})	
		} else {
			//alert($('#PageContent div.text').length);
			$('#PageContent div.text').fadeTo(300,1);
		}

		
		$('#tb-dim').livequery(function(){
			$(this).css({
				opacity: "0"
			});
			$(this).fadeTo({
				duration: 2100
			}, 0.75);
		})
	
		var images = {
	        delay: 300,
	        actions:[],
	        run: function() {
                if (images.actions.length) {
                        images.actions.shift()();
                        setTimeout(images.run, images.delay);
                }
	        }
		};
//		images.actions.push(function () {
//			//log('running '+images.actions.length);
//		})
		/*
		 $('#Gallery li').livequery(function () {
			$(this).each(function() {
				var that=$(this);							
				var atag=$(this).find('a:first');
				
				var link=atag.attr('href');
				var title=atag.attr('title');				
				var url=atag.attr('rel');				
				//console.log(link+" u:" +url+" "+title);
				
				that.find('noscript').remove();
				$(function () {
			        var img = new Image();
			        $(img).load(function () {
			            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
			            $(this).hide();
			            that.removeClass('loading').css({opacity:"1"}).append(this);
			            $(this).wrap("<a class='thickbox' rel='gallery' href='"+link+"' title='"+title+"'></a>").fadeIn(1500);
			        }).error(function () {
			            //console.log("wtf");
			        }).attr('src', url);
			    });
			})
		})
		 */
		
		$('#Gallery li').livequery(function () {
			$(this).each(function() {
				var that=$(this);							
				var atag=$(this).find('a:first');
				
				var link=atag.attr('href');
				var title=atag.attr('title');				
				var url=atag.attr('rel');				
				//console.log(link+" u:" +url+" "+title);
				
				that.find('noscript').remove();
				$(function () {
			        var img = new Image();
			        $(img).load(function () {
			            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
			            $(this).hide();
			            that.removeClass('loading').css({opacity:"1"}).append(this);
			            $(this).wrap("<a class='thickbox' rel='gallery' href='"+link+"' title='"+title+"'></a>");
													
						if (!$.browser.msie) {
							images.actions.push(function () {
								//alert($(this).text());
								$(img).fadeIn(400);
//								log(images.actions.length);
							})	
						} else {
							if (that.siblings().length < 1 || that.prev().length < 1) {
								$(img).show();
								//alert('mkay');
							} else {
								images.actions.push(function () {
									//alert($(this).text());
									$(img).fadeIn(400);
									//alert(images.actions.length);
								})
							}
						}

						if (that.siblings().length == 0 || that.siblings('.loading').length < 1  ) {

							images.actions.push(function () {
								//alert($(this).text());
								setTimeout(function () {
									that.find('li img').css({opacity:1});
								},200);
								//log(images.actions.length);
							})
							setTimeout(images.run,100);
						}

			        }).error(function () {
			            //console.log("wtf");
			        }).attr('src', url);
			    });
			})
		})
		
		

		$('#Gallery a').livequery(function () {
			$(this).each(function () {
				$(this).thickbox();
			})
		})	


	})
})(jQuery);