activePhotoElement = undefined;
var curentGallery = 0;
var curentTag = '';
var curentLink = '';
var curentState = new Array;
var initialCount = 9;
 
jsGalleryScrolling = function(delta, scrollTable){
	if ( delta > 0 ){
		if( parseInt(scrollTable.css('left')) >= -50 ){
			scrollTable.css({'left': '0px'});
		}else{
			scrollTable.css({'left':  ( parseInt(scrollTable.css('left')) + 50 ) + 'px' });
		}
	}else{
		initialCount++;
		var cimg = $('div.gallery-preview').find('img').get(initialCount);
		if ( cimg.src == '' ) {
			$(cimg).attr('src', $(cimg).attr('src1'));
		}

		
		if( parseInt(scrollTable.css('left')) <= scrollTable.parent().width() - scrollTable.width() + 50 ){
			scrollTable.css({'left': (scrollTable.parent().width() - scrollTable.width())+'px'});
		}else{
			scrollTable.css({'left':  ( parseInt(scrollTable.css('left')) - 50 ) + 'px' });
		}
	}
	return false;
}
jsGalleryScroll = function(){
	var scrollPane = $('.gallery-prewiev-wrap');
	var jScrollPaneContainer = scrollPane.parent();
	var scrollTable = scrollPane.find('table.img-collection');
	jScrollPaneContainer.find('div.arrow-left a').click(function(){ jsGalleryScrolling(1, scrollTable); return false; });
	jScrollPaneContainer.find('div.arrow-right a').click(function(){ jsGalleryScrolling(-1, scrollTable); return false; });
	scrollPane.bind('mousewheel', function(event, delta) {
		jsGalleryScrolling(delta, scrollTable);
		return false;
	});
}
jsGalleryAutoFocus = function(activeElement, scrollTable){
	var scrollPane = jQuery(scrollTable).parents('div.gallery-prewiev-wrap');
	var beforeWidth = 0;
	jQuery(activeElement).prevAll().each(function(){
		beforeWidth = beforeWidth + jQuery(this).width();
	});
	var afterWidth = 0;
	jQuery(activeElement).nextAll().each(function(){
		afterWidth = afterWidth + jQuery(this).width();
	});
	
	if ( ( scrollPane.width() - parseInt(scrollTable.css('left')) ) < ( beforeWidth + activeElement.width() ) ){
		scrollTable.css({'left': ( - ( beforeWidth + activeElement.width() + 15 - scrollPane.width() ) )+'px'});
	}
	if ( parseInt(scrollTable.css('left')) < -( beforeWidth ) ){
		scrollTable.css({'left': ( - ( beforeWidth ) )+'px'});
	}
}
jsToggleGray = function(image, toState){
	var regColorEx = /(.*)(p=[0-9]+$)/;
	var regGrayEx = /(.*)(\&gray{1})(.*)/;
	var regGrayTestEx = /(.*)(\&gray{1}$)/;
	var imageSrc = image.attr('src');
	var ppId = image.attr('id');
	//var result = regColorEx.exec(imageSrc);
	//alert(toState);
	switch ( toState ){
		case undefined:
			if(regGrayTestEx.test(imageSrc)){
				var result = regColorEx.exec(imageSrc);
				return result[1] + result[2];
			}else{
				var result = regGrayEx.exec(imageSrc);
				return result[1] + result[3];
			}
		break;
		case 'gray':
			if ( curentState[ppId] == false ) return image.attr('src');
			if(!regGrayTestEx.test(imageSrc)){
				result = regColorEx.exec(imageSrc);
				if ( result ) {
					curentState[ppId] = false;
					return result[1] + result[2] + '&gray' ;
				} else {
					if ( image.attr('rel') ) {
						rel = image.attr('rel');
						image.attr('rel', image.attr('src') );
						curentState[ppId] = false;
						return rel;
					} else {
						curentState[ppId] = false;
						return image.attr('src');
					}
				}
			}else{
				if ( image.attr('rel') ) {
					rel = image.attr('rel');
					image.attr('rel', image.attr('src') );
					curentState[ppId] = false;
					return rel;
				} else {
					curentState[ppId] = false;
					return image.attr('src');
				}
				//return image.attr('src');
			}
		break;
		case 'color':
			if ( curentState[ppId] == true ) return image.attr('src');
			if(regGrayTestEx.test(imageSrc)){
				var result = regGrayEx.exec(imageSrc);
				curentState[ppId] = true;
				return result[1] + result[3];
			}else{
				if ( image.attr('rel') ) {
					rel = image.attr('rel');
					image.attr('rel', image.attr('src') );
					curentState[ppId] = true;
					return rel;
				} else {
					curentState[ppId] = true;
					return image.attr('src');
				}
			}
		break;
		default:
		alert('error');
	}
}
jsGalleryChangePhoto = function(linkElem, bigPhoto){
	var tdImg = jQuery(linkElem).parents('td.img');
	if (!tdImg.hasClass('active')){
		var newHref = jQuery(linkElem).attr('href');
		if(tdImg.next('td.img').length){
			var nextElem = tdImg.next('td.img').find('a');
			var nextHref = nextElem.attr('href');
			jQuery(bigPhoto).find('div.arrow-right').css({'visibility': 'visible'}).find('a').unbind().click(function(){
				jsGalleryChangePhoto(nextElem, bigPhoto);
				return false;
			});
		}else{
			jQuery(bigPhoto).find('div.arrow-right').css({'visibility': 'hidden'});
		}
		if(tdImg.prev('td.img').length){
			var prevElem = tdImg.prev('td.img').find('a');
			var prevHref = prevElem.attr('href');
			jQuery(bigPhoto).find('div.arrow-left').css({'visibility': 'visible'}).find('a').unbind().click(function(){
				jsGalleryChangePhoto(prevElem, bigPhoto);
				return false;
			});
		}else{
			jQuery(bigPhoto).find('div.arrow-left').css({'visibility': 'hidden'});
		}
		var img = undefined;
		jQuery(linkElem).parents('table.img-collection').find('td.img.active').each(function(){
			jQuery(this).removeClass('active').find('table.frame-image').removeClass('hover');
			img = jQuery(this).find('img');
			img.attr({'src': jsToggleGray(img, 'gray') });
		});
		img = jQuery(linkElem).parents('td.img').addClass('active').find('table.frame-image').addClass('hover').find('img');
		img.attr({'src': jsToggleGray(img, 'color') });

		jQuery(bigPhoto).find('img.frame-image').animate({opacity: 0}, 200, function(){
			jQuery(this).attr('src','/skins/new/images/000.gif');
			jQuery(this).attr({'src': newHref}).animate({opacity: 1}, 100);
		});
		/*jQuery(bigPhoto).find('img.frame-image').animate({'opacity': 0}, 200).attr({'src': newHref}).animate({'opacity': 1}, 100);
		jQuery(bigPhoto).find('img.frame-image').animate({'opacity': 0}, 200);
		jQuery.get ( newHref, {}, function(data) {
			var tmpImage = new Image;
			tmpImage.src = newHref;
			$(tmpImage).load (function() {
				jQuery(bigPhoto).find('img.frame-image').attr('src','/skins/new/images/000.gif');
				jQuery(bigPhoto).find('img.frame-image').attr({'src': tmpImage.src});
				jQuery(bigPhoto).find('img.frame-image').animate({'opacity': 1}, 100)
			});
		});*/
		//attr({'src': newHref}).animate({'opacity': 1}, 100);
		
		newHrefSrc = newHref.match (/&s=(.+)$/gi);
		newHrefSrc = newHrefSrc[0].replace ( '&s=', '' );
		jQuery(bigPhoto).find('a[rel=fancy]').attr({'href': '/admin/include/images/gallery/'+newHrefSrc});

		var alt = jQuery(linkElem).find('img').attr('alt');
		jQuery(bigPhoto).find('a[rel=fancy]').attr({'title': alt});
		
		jsGalleryAutoFocus(tdImg, jQuery(linkElem).parents('table.img-collection'));
		
		activePhotoElement = jQuery(linkElem).attr('id');
		jQuery('#full-photo-info').html('<img src="/skins/new/images/26-0.gif" alt="Загружается..." alt="Загружается...">');
		jQuery('#full-photo-info').load('/include/backend/get_gallery.php?p='+activePhotoElement+'&g='+curentGallery+'&tag='+curentTag+'&l='+curentLink, {}, callbackImageInfo);
	}
	return false;
}
callbackImageInfo = function(responseText, textStatus, XMLHttpRequest){
	if (textStatus == 'success'){
		jQuery('#full-photo-info div.related-photos table.frame-image').hover(function(){
			jQuery(this).addClass('hover');
			var img = jQuery(this).find('img');
			img.attr({'src': jsToggleGray(img, 'color') });
		}, function(){
			jQuery(this).removeClass('hover');
			var img = jQuery(this).find('img');
			img.attr({'src': jsToggleGray(img, 'gray') });
		});
	}
}
jsGalleryInit = function(){
	var galleryPreviewCol = jQuery('div.gallery-preview');
	var bigPhoto = galleryPreviewCol.next('div.gallery-bigphoto');
	var tdImg = jQuery(galleryPreviewCol).find('td.img.active');
	if(tdImg.next('td.img').length){
		var nextElem = tdImg.next('td.img').find('a');
		var nextHref = nextElem.attr('href');
		jQuery(bigPhoto).find('div.arrow-right').css({'visibility': 'visible'}).find('a').unbind().click(function(){
			jsGalleryChangePhoto(nextElem, bigPhoto);
			return false;
		});
	}else{
		jQuery(bigPhoto).find('div.arrow-right').css({'visibility': 'hidden'});
	}
	if(tdImg.prev('td.img').length){
		var prevElem = tdImg.prev('td.img').find('a');
		var prevHref = prevElem.attr('href');
		jQuery(bigPhoto).find('div.arrow-left').css({'visibility': 'visible'}).find('a').unbind().click(function(){
			jsGalleryChangePhoto(prevElem, bigPhoto);
			return false;
		});
	}else{
		jQuery(bigPhoto).find('div.arrow-left').css({'visibility': 'hidden'});
	}
	
	jQuery(galleryPreviewCol).find('td.img').find('table.frame-image').hover(function(){
		if (!jQuery(this).parents('td.img').hasClass('active')){
			jQuery(this).addClass('hover');
			var img = jQuery(this).find('img');
			img.attr({'src': jsToggleGray(img, 'color') });
		}
	}, function(){
		if (!jQuery(this).parents('td.img').hasClass('active')){
			jQuery(this).removeClass('hover');
			var img = jQuery(this).find('img');
			img.attr({'src': jsToggleGray(img, 'gray') });
		}
	});
	activePhotoElement = jQuery(galleryPreviewCol).find('td.img.active table.frame-image a').attr('id');
	callbackImageInfo('', 'success', '');
	
	jQuery(galleryPreviewCol).find('a[rel=ajax]').click(function(){
		return jsGalleryChangePhoto(jQuery(this), bigPhoto);
	});
}
jQuery(document).ready(jsGalleryInit);
jQuery(document).ready(jsGalleryScroll);