$(document).ready(function(){
	var gridLayout = true;
						   
	$('div.gallery img').slidingGallery({
		'slideSpeed':450,
		'zoomSpeed':'fast',
		'Lheight': 365,
		'Lwidth': 550,
		'Pheight': 414,
		'Pwidth': 275
	});
	
	$(".galThumb img").fadeTo("slow", 0.6);
	$("#galleryGrid img").fadeTo("fast", 0.4);
	$(".category").animate({top:'-25px'}, "slow");
	$(".category span").fadeTo("slow", 0.6);
	$(".category p").fadeTo("slow", 0.6);
	$("a.gridLayout").fadeTo("slow", 0.6);
	$("a.flowLayout").fadeTo("slow", 0.6);
	$("#cont").find(".gallery").toggle();
	$("#galleryGrid").fadeTo("slow", 1);

	$(".galThumb").hover(function() {
		$(this).find("img").fadeTo("fast", 1.0);
		$(this).find(".category p").fadeTo("fast", 0.99);
		
	},function() {
		$(this).find("img").fadeTo("normal", 0.6);
		$(this).find(".category p").fadeTo("normal", 0.6);
	});
	
	$("#galleryGrid img").hover(function() {
		$(this).fadeTo("fast", 1.0);
		
	},function() {
		$(this).fadeTo("normal", 0.4);
	});
	
			
	$("a.gallery").click(function() {
		$("#subNav").animate({top:'-5px'}, "slow", function() {
				$("#subNav").css({'z-index':'1'});											
		});
	});
	
	$("a.gridLayout").hover(function() {
		$(this).fadeTo("normal", 1.0);
		
	},function() {
		$(this).fadeTo("normal", 0.6);
	});
	
	$("a.flowLayout").hover(function() {
		$(this).fadeTo("normal", 1.0);
		
	},function() {
		$(this).fadeTo("normal", 0.6);
	});
	
	$("a.gridLayout").click(function() {
		$('body').css('overflow', 'auto');
		
		if(!gridLayout) {
			gridLayout = true;
			$("#cont").find(".gallery").fadeTo(
				"slow", 0,
				
				function() {
					$("#cont").find(".gallery").toggle();
					$("#galleryGrid").fadeTo("slow", 1);
			});
		}
	});
	
	$("a.flowLayout").click(function() {
		$('body').css('overflow', 'hidden');
		
		if(gridLayout) {
			gridLayout = false;
			$("#galleryGrid").fadeTo(
				"slow", 0,
				
				function() {
					$("#galleryGrid").toggle();
					$("#cont").find(".gallery").fadeTo("slow", 1);
			});
		}
	});
	
	$("a[rel=gridGroup]").fancybox({
		'titleShow'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
	$('#nav a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function() {
			$(this).stop().animate({backgroundPosition:"(0 -250px)"}, {duration:300})
		})
		.mouseout(function() {
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:300})
	});
	
	$('#subNavList a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function() {
			if(!$(this).is(".active")) {
				$(this).stop().animate({backgroundPosition:"(0 -250px)"}, {duration:300})
			}
		})
		.mouseout(function() {
			if(!$(this).is(".active")) {
				$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:300})
			}
	});
		
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});

});
