var $p = 0;
var url = String(window.location);
if(url.indexOf('#')){
	hash = url.split('#');
	if(hash[1]) $p = parseInt(hash[1].substr(0,2));
}
	$(document).ready(function(){
		$(".lightbox").lightbox();
		var cIcon = 1;
		var nIcon = 4;
		
		$("#gallery_icon img").click(function(){
			if(this.src.indexOf('no-image')==-1){
				tsrc = this.src;
				title = this.title;
				msrc = tsrc.replace('S-out','M');
				lsrc = tsrc.replace('S-out','L');
				cIcon = this.id.replace('icon','');
				cIcon = parseInt(cIcon);
				
				$("#gallery_full div").animate({height: 196,opacity: 0.6},500,"linear",function(){
					for(var icon = 1;icon<=nIcon;icon++){
						var iconsrc = $("#icon"+icon).attr('src');
						if(icon!=cIcon){
							iconsrc = iconsrc.replace('click','out');
							$("#icon"+icon).attr('src',iconsrc).animate({opacity: 1},500);
						}
					}
					$("#gallery_full img").attr('src',msrc);
					$("#gallery_full a").attr('href',lsrc);
					$("#gallery_full a").attr('title',title);
					$('#gallery_title_title').html(title);
					$('#gallery_title_link a').attr('href',lsrc).attr('title',title);
				}).animate({height: 0,opacity: 0},500);
			}
			//$("#gallery_full div").animate({height: 0,opacity: 0},500);
		}).hover(function(){
			//this.src = this.src.replace('out','click');//'store/web_2S-click.jpg';
			$(this).animate({opacity: 0.5},300);
			//var pos = $(this).offset();
			//$("#gallery_mask").css('top',pos.top+30).animate({height: '60px',top: pos.top,opacity: 1},1000).animate({height: '0px',top: pos.top+30,opacity: 0},400);
			
		},function(){
			//if(this.src.indexOf('out')!=-1){
				if(this.id!='icon'+cIcon){
					//this.src = this.src.replace('click','out');//'store/web_2S-out.jpg';
					$(this).animate({opacity: 1},300);
				}
				//$("#gallery_mask").animate('height',60)
				//$("#gallery_mask").stop();
			//}
		});
		//alert('adfa');
		loadPage($p);
	});
	function loadPage($p){
		var $start = $p*4;
		var src = aImage[$start][0];
		$("#gallery_full div").animate({height: 196,opacity: 0.6},500,"linear",function(){
			$('#gallery_full img').attr('src',src.replace('S-out','M'));
			$('#gallery_full a').attr('href',src.replace('S-out','L')).attr('title',aImage[$start][1]);
			$('#gallery_title_title').html(aImage[$start][1]);
			$('#gallery_title_link a').attr('href',src.replace('S-out','L')).attr('title',aImage[$start][1]);
		}).animate({height: 0,opacity: 0},500);
		var $n = Math.ceil(aImage.length/4);
		var sPage = '<ul>';
		for(var $j=0;$j<14;$j++) if($j<$n) sPage += '<li'+($j==$p?' class="current"':'')+'><a href="#0'+$j+'1"  onclick="loadPage('+$j+');">'+($j+1)+'</a></li>';
		else sPage += '<li class="nonumber"><img src="images/no-number.jpg" /></li>';
		sPage += '</ul>';
		$('#gallery_page').html(sPage);
	$('#gallery_icon img').animate({opacity: 0.2},400,'linear',function(){
		for(var icon =1;icon<=4;icon++){
			var src = aImage[$start+icon-1]?aImage[$start+icon-1][0]:'images/no-image.jpg';
			var title = aImage[$start+icon-1]?aImage[$start+icon-1][1]:'No image';
			//if(icon==1){
				//src = src.replace('out','click');
			//}
			$('#icon'+icon).attr('src',src);
			$('#icon'+icon).attr('title',title);
		}
	}).animate({opacity: 1},800);
	}

