/**
 * @author Oleg Korovin (http://olegrorovin.spb.ru/)
 * @include "./_Lib.js"
 * @include "/Job/job/seasons-realty/_java/jQuery.js"
 * @include "/sample/jQuery/jquery-doc.js"
 */

jQuery.noConflict();


var Nanobox = {
	elPreviews		: null,
	elCenterImage	: null,

	mImages	: [],

	sCurFilter	: '',

	init	: function(){
		var oThis = this;

		oThis.elPreviews = jQuery('#Previews');
		oThis.elCenterImage = jQuery('#CenterImage');
	},

	portfolio	: function(){
		var oThis = this;

		oThis.hide(function(){

			var sFilter = 'favorite';

			if(oThis.sCurFilter == sFilter){
				oThis.sCurFilter = '';
				return;
			}

			oThis.sCurFilter = sFilter;


			jQuery('#Previews img').each(function(i){
				oThis.mImages[i].bVisible = oThis.mImages[i].is_favorite;

				Styles.setClass(this,'hidden',!oThis.mImages[i].bVisible)

			})

			oThis.show();

		});
	},

	group	: function(iId){
		var oThis = this;



		oThis.hide(function(){
			var sFilter = 'group' + iId;

			if(oThis.sCurFilter == sFilter){
				oThis.sCurFilter = '';
				return;
			}

			oThis.sCurFilter = sFilter;

			jQuery('#Previews img').each(function(i){

				oThis.mImages[i].bVisible = (oThis.mImages[i].p_id == iId);

				Styles.setClass(this,'hidden', !oThis.mImages[i].bVisible )
			})
			oThis.show();

		});
	},

	next	: function(iCur){
		var oThis = this;

		for(var i=iCur+1,l=oThis.mImages.length;i<l;i++){
			if(oThis.mImages[i].bVisible) return i;
		}
		return null;
	},

	prev	: function(iCur){
		var oThis = this;

		for(var i=iCur-1;0 <= i;i--){
			if(oThis.mImages[i].bVisible) return i;
		}
		return null;
	},

	show	: function(){
		var oThis = this;

		jQuery('#PreviewsBox')
		.removeClass('Default')
		.css({
			display : 'block'
		});

		oThis.elCenterImage.addClass('Selected');

		oThis.elPreviews.slideDown('slow',function(){
		//	oThis.elPreviews[0].scrollIntoView();
		});

	},

	hide	: function(fFunc){
		var oThis = this;


		oThis.elPreviews.slideUp('fast',function(){
			oThis.elCenterImage.removeClass('Selected');
			jQuery('#PreviewsBox').css({
				display : 'none'
			});

			if(fFunc) fFunc();
		});

	},

	open	: function(i){
		var oThis = this;

		//winPopup.close();
//alert(i)

		jQuery('#Gallery div.PopupContent')
		//.fadeOut(100)
		.css({
			'visibility'	: 'hidden'
		})
		.show();

		oThis.arrow('#Gallery div.ArrowL',oThis.prev(i));
		oThis.arrow('#Gallery div.ArrowR',oThis.next(i));

		jQuery('#GalleryBig').attr({
			src : '/_images/x.gif',
			width	: '',
			height	: '',
			alt		: '',
			title	: ''
		})
		.attr({
			src : oThis.mImages[i].big,
			width	: oThis.mImages[i].w,
			height	: oThis.mImages[i].h,
			alt		: oThis.mImages[i].name,
			title	: oThis.mImages[i].name
		})
		//.one('load',function(e){
			Veil.show('Gallery',1);

			jQuery('#Gallery div.PopupContent')
			.hide()
			.css({
				'visibility'	: 'visible'
			})
			.fadeIn('slow');
		//});


	},

	arrow	: function(sNode,iPos){
		var oThis = this,
			elArrow = jQuery(sNode)[0];

		if(iPos != null){
			elArrow.style.display = 'block';
			elArrow.onclick = function(e){
				jQuery('#Gallery div.PopupContent')
				.fadeOut('fast',function(){
					oThis.open(iPos);
				});

			}
		}
		else {
			elArrow.style.display = 'none';
			elArrow.onclick = null;
		}

//		elArrow.title = iPos;
	}
};

jQuery(function(){
	Nanobox.init();
});