/*
	Gallery by Yan
	2008-07-04 v 001
*/
				var agt=navigator.userAgent.toLowerCase();
				var is_major = parseInt(navigator.appVersion);
				var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
				var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
$(document).ready(function() {
var preloadImage=new Image();
preloadImage.src='/js/ajax-loader.gif';
	function getPos(el){
		var top=0,left=0;
		do{
			top += el.offsetTop;
			left += el.offsetLeft;
		}while (el=el.offsetParent);
		return {left:left, top:top};
	};
	function getPageSize(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		return {width:w,height:h};
	}
	function getGalleryName(){
		return $('#category_name img').attr('alt');
	}
	function closeLoaded(){
		var $div=$('.shadow[opened="true"]');
		var $img=$('img',$div);
		var div=$div.get(0);
		var $imgbox=$('#bp_window');
		var $container=$('#bp_box_for_window');
		var $bigimg=$('#bp_box_for_window img');
		var $backphone=$('#bp_overlay');
		

		
		var scroll=document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
		var pos=$div.offset();
		var left=pos.left+2;
		var top=pos.top-scroll+2;
		//alert([left,top])

		$('#bp_hint, #bp_close').css('display','none'); //, #bp_back, #bp_next

		
		
		
		var duration=300;
		$backphone.hide();
		$bigimg.animate(
			{ 
				width: $img.width()+'px',
				height: $img.height()+'px'/*,
				opacity: 0.3*/
			},
			duration,
			function(){
				//$backphone.hide();
			}
		);
		//$('#bp_hint').css();
		
		$imgbox.animate(
			{ 
				left: left+'px',
				top: top+'px'
			},
			duration,
			function(){
				$container.hide();
				$div.attr('opened','false');
				loading_div=false;
				if (is_ie6){
					//alert('jjj');
					//$('html').css('overflow','auto');
				}
				//div.zoomOut.apply(div);
				//а тут надо вставить ревалидацию положения мышки над элементом!!!
			}
		);
		return false;
	};
	function next(){
		alert('next');
		return false;
	};	
	function prev(){
		alert('prev');
		return false;
	};
	function showLoaded($div,url,w,h){
	//try{
		$img=$('img',$div);
		var $backphone=$('#bp_overlay');
		//alert([w,h]);
		if ($backphone.length==0){
			$('body').append('<div id="bp_overlay"></div><div id="bp_box_for_window"><div id="bp_window"><a href="#" id="bp_close" ></a><a href="#" id="bp_back" ></a><a href="#" id="bp_next" ></a><div id="bp_hint"></div><img src="#" alt="" width="900" height="600" /></div></div>');
			$backphone=$('#bp_overlay');
			$backphone.add('#bp_window').click(closeLoaded);
			//$('#bp_back').click(prev);
			//$('#bp_next').click(next);
		}
		var $bigimg=$('#bp_box_for_window img');
		var $container=$('#bp_box_for_window');		
		var container=$container.get(0);
		var $imgbox=$('#bp_window');		
		var div=$div.get(0);
		$div.attr('opened','true');
		var scroll=document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
		//var pos=getPos(div);
		var pos=$div.offset();
		//		alert(pos.top);
		//return;
		var left=pos.left+2;
		var top=pos.top-scroll+2;
		//alert([$img.width(),$img.height(),$img.get(0).src]);
//		alert([left,top]);
		$bigimg.get(0).src=url;
		$bigimg.css('width',$img.width()+'px');//w
		$bigimg.css('height',$img.height()+'px');//h
		$imgbox.css('left',left+'px');
		$imgbox.css('top',top+'px');
		//$bigimg.css('opacity',0.3);
		//$imgbox.width(200);
		
		var title=$('a',$div).attr('title')||false;
//alert(2);		
		if (title){
		
			//var x=getGalleryName();
			//alert(x);
			//$('#bp_hint').html('<span>123</span>');
//alert(23);					
			$('#bp_hint').html('<strong>'+getGalleryName()+'</strong>'+(title));
		}else{		
//alert(24);							
			$('#bp_hint').html('');
		}
//alert(25);									
	//}catch(e){
	//	alert(e.message);
	//};
//alert(26);										
		$container.show();
		

		// Resizing large images - orginal by Christian Montoya edited by me.
		var pageSize = getPageSize();
//alert(262);								
		var x = pageSize.width - 150;
		var y = pageSize.height - 150;
		//alert([w,h]);
		var imageWidth = w;
		var imageHeight = h;
		if (imageWidth > x) {
			imageHeight = imageHeight * (x / imageWidth); 
			imageWidth = x; 
			if (imageHeight > y) { 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
			}
		} else if (imageHeight > y) { 
			imageWidth = imageWidth * (y / imageHeight); 
			imageHeight = y; 
			if (imageWidth > x) { 
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x;
			}
		}
//alert(27);												
//alert(4);								
		imageWidth=Math.round(imageWidth);
		imageHeight=Math.round(imageHeight);
		var l=(pageSize.width-imageWidth)/2;
		//l=Math.round(l);
		//t=Math.round(t);
		var t=(pageSize.height-imageHeight)/2;
		//alert([imageWidth,imageHeight,l,t]);
		// End Resizing

		
//alert(5);										
		
		var duration=300;
//alert(28);												
		$bigimg.animate(
			{ 
				width: imageWidth+'px',
				height: imageHeight+'px'/*,
				opacity: 0.99*/
			},
			duration
		);
		//$('#bp_hint').css();
//alert(6);		
//alert(29);
		if (is_ie6){
			//$('html').css('overflow','hidden');
		}
		$imgbox.animate(
			{ 
				left: l+'px',
				top: t+'px'
			},
			duration,
			function(){
				$backphone.show();

				
				setTimeout(function(){
					//alert('jjk');
					$('#bp_close').css('display','block');//fadeIn(); //#bp_back, #bp_next
					
					var $hint=$('#bp_hint');
					if ($hint.html()!=''){
						$hint.show();
					}
					
					//$backphone.get(0).focus();
				},100);
				//alert($('#bp_hint').css('display'));
			}
		);		
		
//alert(30);		
//alert(7);														
		
		
		
		
		
		
	}
	function load(url,listener){
		var loader=new Image();
		loader.onload=function(){
			loader.onload=null;
			var w=loader.width;
			var h=loader.height;
			//alert([w,h]);
			listener(w,h);
		};
		loader.src=url;
	};
	var $load_image=$('<img style="display:none; border:none;z-index:150;position:absolute;" src="/js/ajax-loader.gif" style="border:none;" class="ooop"/>');
	$('#g_centered_box').append($load_image);
	function show_loader(top,left){
		$load_image.css('top',top+'px');
		$load_image.css('left',left+'px');
		$load_image.show();
	}
	function hide_loader(){
		$load_image.hide();
	}		
	function showDiv($div){
	//try{
		var $a=$('a',$div);
		var $img=$('img',$div);		
		var url=$a.attr('href');
		$div.attr('zoomlock','true');
		var div=$div.get(0);
		var t=5+Math.ceil(1*div.end.image.height/2)-6+parseInt($div.css('top'));
		var l=5+Math.ceil(1*div.end.image.width/2)-22+parseInt($div.css('left'));
		//alert(l);
		show_loader(t,l);
		loading_div=div;
		div.loading_cancel=false;
		load(url,function(){
			if (div.loading_cancel==true){
				div.loading_cancel=false;
				//alert('p');
				return;
			}
			hide_loader();
				showLoaded($div,url,arguments[0],arguments[1]);
			//alert('y3');
			$div.attr('zoomlock','false');
			//div.zoomOut.apply(div); ???
		});
		//}catch(e){
		//	alert('showDiv'+e.message);
		//}					
		return false;		
	}
	function cancel_loading($div){
		hide_loader();
		div=$div.get(0);
		div.loading_cancel=true;
		$div.attr('zoomlock','false');
		loading_div=false;
	}
	$('.shadow a').click(function(e){
	//try{
		e.stopPropagation();
		var $div=$(this.parentNode);
		var div=$div.get(0);
		if (loading_div!=false){
			$loading_div=$(loading_div);
			if (loading_div==div){
				cancel_loading($loading_div);
				return false;
			};
			cancel_loading($loading_div);
			hide.apply($loading_div.get(0));
			show.apply(div);
		};	
		showDiv($div);		
		return false;		
	//}catch(e){
	//		alert('shadow a click'+e.message)
	//}
	});
});
