var U_gallery =
{
	prefix	: '',
	json	: '/?ZRP=gallery.ajax&args='+json_encode({"aid":2,"list":1},1),
	
	animate_interval	: 500,
	animate_opacity		: 0.8,
	
	initialized			: false,
	source				: {},
	imgNow				: 0,
	rhandler			: false,
	imgW				: 1600,
	imgH				: 1024,
	stop				: 1000,
	first 				: true,
	
	/*
	balancePicture 		: function(offset)
	{
		var now  = $q('body').width();
		var nowh = $q('body').height();
		var left = this.imgW - now >= offset.left ? offset.left : this.imgW - now;
		var top  = this.imgH - nowh >= offset.top ? offset.top : this.imgH - nowh; 
		$q('#gallery-ver .main').css({top: - top + 'px', left: - left + 'px'});
	},
	centerPicture		: function()
	{
		var l = 1600 - $q('body').width();
		var t = 1024 - $q('body').height();
		
		l = (l > 0 ? Math.round(l/2) : 0);
		t = (t > 0 ? Math.round(t/2) : 0);
		
		$q('.main_content:last .main').css({left: -l+'px', top: -t+'px'});
	},*/
	scalePicture		: function(imgEl, mainEl)
	{
        var rootEl = $q('.main_content:last').first();
        var
			//mainEl = rootEl.find('.main'),
			//imgEl = mainEl.find('.picture img'),
	
			lastX = 0,
			lastY = 0,
	
			originalWidth = parseInt(imgEl.css('width')),
			isBig = false,
			update = function()
			{
				var
					left = 0,
					top = 0,				
					rootWidth = rootEl.width(),
					rootHeight = rootEl.height(),
					imgWidth = imgEl.width(),
					imgHeight = imgEl.height(),
					curLeft = parseInt(mainEl.css('left')) + left,
					curTop = parseInt(mainEl.css('top')) + top;
		
				if (rootWidth > originalWidth && isBig === false) {
					imgEl.removeAttr('width').removeAttr('height').css('width','100%');
					curLeft = 0;
					isBig = true;
				}
				else if (rootWidth < originalWidth && isBig) {
					imgEl.removeAttr('style').css('width', U_gallery.stop + 'px');
					isBig = false;
				}
		        
				if (curLeft < rootWidth - imgWidth) {
					curLeft = rootWidth - imgWidth;
				}
				if (0 < curLeft || isBig) {
					curLeft = 0;
				}
				if (curTop < rootHeight - imgHeight) {
					curTop = rootHeight - imgHeight;
				}
				if (0 < curTop) {
					curTop = 0;
				}


                /*
                
				mainEl.css({
					'left': curLeft,
					'top': curTop
				});
                */			
			}
		
		if(U_gallery.first)
		{
			U_gallery.first = false;		
			imgEl.removeAttr('width').css('width', '100%');
		}

        update();
		 /*
        
		$q(window).resize
		(
			function()
			{
				update();	
			}
		)
        
        */
	},
	imgLoader			: function(i)
	{
		var tmp = new Image();
		tmp.src = U_gallery.prefix + U_gallery.source[i].images['crop_103_67'];
	    
		$q('.pagefader:last').show().animate
		(
			{opacity: U_gallery.animate_opacity},
			U_gallery.animate_interval,
			function()
			{
				var tmp = new Image();
				$q(tmp).load(function()
				{
					U_gallery.setNow(i);
					$q('.pagefader:last').animate({opacity: 0}, U_gallery.animate_interval, function(){ 
                        $q(this).hide(); 
                    });
				}).attr('src', U_gallery.prefix + U_gallery.source[i].images['crop_1600_1024']);
                
                
			}
		);
        
	},
	setNow				: function(i)	//показать элемент галереи
	{
		i = parseInt(i, 10);
		var source = U_gallery.source;
		
		if(i == 0) $q('.icon_square_left2').hide();
		else $q('.icon_square_left2').show();
		
		if(i + 1 == source.length){
            $q('.icon_square_right2').hide();    
        }else{
            $q('.icon_square_right2').show();
        } 
	
		//$q('#gallery-ver .picture img').remove();
		$q('#gallery-ver .description span').html(source[i].title);
		$q('#gallery-ver .picture img').attr('src', U_gallery.prefix + source[i].images['crop_1600_1024']);
		//mainPicture('.main_content:last');
		//U_gallery.balancePicture(source[i].offset);
		U_gallery.scalePicture($q('.picture img'), $q('.gallery-ver .main'));
        swfShowG(U_gallery.prefix + source[i].images['crop_1600_1024']);
		U_gallery.imgNow = i;
	},
	showDecorator		: function(i)
	{
		var r = $q('#gallery .wrapper span[rel="' + i + '"]:first');
		window.rhandler = r;
		
		var o = $q(r).offset();
		var p = $q(r).position();
	
		$q('#magnify').hide();
		$q('#magnify img').attr('src', U_gallery.prefix + U_gallery.source[i].images['crop_103_67']);
		$q('#magnify span').html(U_gallery.source[i].title);
		$q('#magnify').css({left: o.left - 20, top: p.top + 15}).show();
		$q('#magnify').attr('rel', i);
	},
	hideGal				: function()
	{	
		$q('#pagefader').css({opacity: 0}).show().animate
		(
			{opacity: U_gallery.animate_opacity}, 
			U_gallery.animate_interval,
			function()
			{  
				$q('.layout:last').hide();
				$q('.layout:first').show();
				
				U_gallery.imgNow = 0;
				U_gallery.setNow(0);
				
				$q('#magnify').hide();
				$q('#pagefader').animate({opacity: 0}, U_gallery.animate_interval, function() { $q(this).hide() })
			}
		);
	},
	showGal				: function()
	{
		$q('#pagefader').css({opacity: 0}).show().animate
		(
			{opacity: U_gallery.animate_opacity}, 
			U_gallery.animate_interval, 
			function()
			{
				$q('.layout:last').show();
				$q('.layout:first').hide();
				$q('#pagefader').animate({opacity: 0}, U_gallery.animate_interval, function() { $q(this).hide() })
			}
		);
		
		//mainPicture('.main_content:last');
		//U_gallery.centerPicture();
	},
	goGal	: function()
	{
		var width     = 71;
		var deftotal  = 13;
		var total     = 0;
		var now		  = deftotal;
		var rhandler  = null;
		
		U_gallery.imgNow	= 0;
        
        $q.getJSON(U_gallery.json, function(data)
		{
			total  		  		= data.length;
			U_gallery.source	= data;
            
            $q('#gallery .wrapper').empty();
			
			
            $q.each
			(
				U_gallery.source,
				function(i, item)
				{
					$q('#gallery .wrapper').append
					(
						'<span rel="' + i + '">' +
							'<img src="' + U_gallery.prefix + item.images.crop_69_45 + '" alt="' + item.title + '" />' +
						'</span>'
					);
				}
			);
			
            
			U_gallery.setNow(0);
            
		});
        
        
	
		U_gallery.showGal();
		
		if (U_gallery.initialized) return;
		
		U_gallery.initialized = true;
	
		$q('.icon_square_left2').hide();
		
		$q('.previews .control_right').click
		(
			function()
			{
				if(now + 1 > total)
				return false;
					
				var left = parseInt( $q('#gallery .wrapper').css('left'), 10);
				$q('#gallery .wrapper').animate({left: '-=' + 5*width + 'px'});
				
				now+=5;
				
				$q('#magnify').hide();
				
				return false;
			}
		);	
		
		$q('.previews .control_left').click
		(
			function()
			{
				if(now <= deftotal)
				return false;
					
				var left = parseInt( $q('#gallery .wrapper').css('left'), 10);
				$q('#gallery .wrapper').animate({left: '+=' + 5*width + 'px'});
				
				now-=5;
				
				$q('#magnify').hide();
			
				return false;
			}
		);
		
		$q('#gallery .wrapper span').live
		(
			'mouseover',
			function(event)
			{
				U_gallery.showDecorator($q(this).attr('rel'))
			}
		);
		
		$q('#magnify').live
		(
			'mouseout',
			function(event)
			{
				$q(this).hide();
			}
		);
		
		$q('#magnify').live
		(
			'click',
			function()
			{
				U_gallery.imgLoader($q(this).attr('rel'));
			}
		);
		
		$q('.icon_square_right2').live
		(
			'click',
			function()
			{
				$q('#magnify').hide();
				U_gallery.imgLoader(++U_gallery.imgNow);
				return false;
			}
		);
		
		$q('.icon_square_left2').live
		(
			'click',
			function()
			{
				$q('#magnify').hide();
				U_gallery.imgLoader(--U_gallery.imgNow);
				return false;
			}
		);
		
		$q(window).resize
		(
			function()
			{
				var rhandler = window.rhandler;
				var tmp = $q('#magnify').css('display');
				
				if(tmp == 'block')
				{
					var o = $q(rhandler).offset();
					var p = $q(rhandler).position();
					$q('#magnify').css({left: o.left - 20, top: p.top + 15});
				}
			}
		);

        
        function imgMoveG(y) {
            i = $q('.gallery-ver .main .picture img');
            boxHeight = $q('.gallery-ver .main .picture').height() - 144;
            percent = parseInt(y * 100 / boxHeight);
            amount = i.height() - boxHeight;
            var top = parseInt(amount * percent / 100 );
            i.css({'top': -top+'px'});
            
        }
        
        $q('#g_popup .cls').live('click', function(){
            $q('#g_popup').fadeOut();      
         });

        $q('.gallery-ver .main .picture').live('mousemove', function(e) {
                imgMoveG(e.clientY);
        });

        $q(window).resize(function() {
            i = $q('.gallery-ver .main .picture img');
            if(i.position().top != 0) {
                i.animate({top:0});
            }
        });

		$q('.gallery-ver .close').click(U_gallery.hideGal);
	}
}

$q(function()
{       
    $q('.layout .favorite').click(U_gallery.goGal);
});


function swfShowG(src) {

    var M =  navigator.appName.indexOf("Microsoft")!=-1;
    var swf = (M ? window : document)["bg_image_g"];
    if(M && swfobject.hasFlashPlayerVersion("9")){
        $q('#swf_pic_g').show();
        $q('#gallery-ver .main .picture').hide();
    }else{
        $q('#swf_pic_g').hide();
        $q('#gallery-ver .main .picture').show();
    }
    
    if (typeof swf.setImage == 'undefined') {
        window.setTimeout('swfShowG("' + src + '");', 100);
        return;
    }
    
    
    swf.setImage(src);                                     
    
 }

