/*class*/
function CollectionSlider() {
	/*HTML element - "UP" button*/
	this.buttonUp;
	/*HTML element - "DOWN" button*/
	this.buttonDown;
	/*list of items for slide*/
	this.items = new Array();
	/*count of items*/
	this.itemsSize = 0;
	/*index of current slide*/
	this.currIndex = 0;
	/*index of current slide*/
	this.currListIndex = 0;
	/*HZ that is it:)*/
	this.step = 0;//liquidate
	/*container of slider*/
	this.cont;
	/**/
	this.isDisabled = false;
	/*ID of current collection*/
	this.currentCollection;
	/*ID of current switch*/
	this.currentSwitch;
	/*ID of current frame*/
	this.currentFrame;
	/*ID of current frame*/
	this.currentSelector;
	/*actions that will do after default actions on click to slide*/
	this.slideActs;
	/*actions that will do after default actions on click to "UP" button*/
	this.butUpActs;
	/*actions that will do after default actions on click to "DOWN" button*/
	this.butDownActs;
	/*function for select items from DB*/
	this.selectMethod;
	/*vertical step of slide*/
	this.stepVert = 0;//liquidate
	/*horizontal step of slide*/
	this.stepHor = 0;//liquidate
	/*Effect of image change*/
	this.slideEffect = null;
	/*Effect of image change*/
	this.ID = null;
	/**/
	this.dir = '';
	
	/*clone of object*/
	var This = this;
	//butUpActs, /*object*/ butDownActs, /*object*/ slideActs
	
	this.isAlreadySet = function() {
		if( typeof this.cont != 'undefined' ) {
			return true;
		}
		return false;
	}
	
	this.mount = function( /*string*/ id, /*int*/set, /*object*/optionsList, /*string*/slideEffect, /*object*/funcAfterMount, firstIndex ) {
		this.cont = $('#'+id);
		this.ID = id.substr(id.indexOf('_')+1);
		
		$(this.cont).parent().find('.loader').fadeIn();	
		
		this.getSerieImagesCount();
		this.getSerieImageUploadDir();
		
		This.butUpActs = optionsList['butUpActs'];
		This.butDownActs = optionsList['butDownActs'];
		This.slideActs = optionsList['slideActs'];
		This.selectMethod = optionsList['selectMethod'];
		This.slideEffect = slideEffect;
		
		This.buttonUp = This.cont.find('.button-up');
		This.buttonDown = This.cont.find('.button-down');
		
		This.cont.height('0px');
		This.cont.show();
		
		This.startDraw(set);
		
		switch( This.slideEffect ) {
			case 'fade':
				if( firstIndex != null && This.itemsSize && This.itemsSize > firstIndex ) {
					This.currIndex = firstIndex;
				}
				else if( This.itemsSize && This.itemsSize > 2 ) {
					This.currIndex = 1;
				}
				_imageIsReady()
				break;
			default:
				break;
		}
		
		$(This.buttonUp).click(function(event) {
			event.stopPropagation();
			
			if( This.isDisabled ) {
				return false;
			}
			This.isDisabled = true;			
			switch( This.slideEffect ) {
				case 'fade':
					slideToVert( -1 )
					break;
				default:
					break;
			}
			This.currIndex--;
			if( This.currIndex == 0 ) {
				if( This.items.length != This.itemsSize ) {
					This.getPrevItem( This.itemsSize-This.step-1, set );
					This.step++;
					This.currIndex++;
				}
			}
			This.setCurrentConfig();			
			if( !empty( This.butUpActs ) ) {
				for( var i in This.butUpActs ) {
					window[i](This.butUpActs[i]);
				}
			}
		});
		
		This.buttonDown.click(function(event) {
			event.stopPropagation();
			
			if( This.isDisabled ) {
				return false;
			}
			This.isDisabled = true;			
			switch( This.slideEffect ) {
				case 'fade':
					slideToVert( 1 )
					break;
				default:
					break;
			}
			if( This.currIndex == This.items.length - 1 ) {
				if( This.items.length != This.itemsSize ) {
					This.getNextItem(This.items.length, set);
				}
			}
			
			This.setCurrentConfig();
			
			if( !empty( This.butDownActs ) ) {
				for( var i in This.butDownActs ) {
					window[i](This.butDownActs[i]);
				}
			}
		});	
		
		This.setCurrentConfig();
		
		if( !empty( funcAfterMount ) ) {
			for( var i in funcAfterMount ) {
				window[i](funcAfterMount[i]);
			}
		}
	}
	
	function slideToVert( direction ) {
		var newIndex = 0;
		if( direction > 0 ) {
			newIndex = This.currIndex < This.itemsSize-1 ? This.currIndex + 1 : 0;
		}
		else {
			newIndex = This.currIndex > 0 ? This.currIndex-1 : This.itemsSize - 1;
		}
		var parentHeight = $(This.items[This.currIndex]['block']).height();
		
		var botElem = direction > 0 ? $(This.items[This.currIndex]['block']) : $(This.items[newIndex]['block']);
		var topElem = direction > 0 ? $(This.items[newIndex]['block']) : $(This.items[This.currIndex]['block']);
					
		topElem.css({'top': '0px', 'bottom': 'auto'});
		botElem.css({'bottom': '0px', 'top': 'auto'});
		
		botElem.find('img').each(function() {
			var height = parentHeight - $(this).height();
			height -= height%2 != 0 ? 1 : 0;
			$(this).css({'bottom': height/2+'px', 'top': 'auto'});
		});
		
		topElem.find('img').each(function() {
			var height = parentHeight - $(this).height();
			height += height%2 != 0 ? 1 : 0;
			$(this).css({'top': height/2+'px', 'bottom': 'auto'});
		});
		
		$(This.items[This.currIndex]['block']).animate({'height': '0px'});
		$(This.items[newIndex]['block']).animate({'height': This.stepVert+'px'}, function(){This.isDisabled = false;});
		This.currIndex = direction > 0 ? newIndex : newIndex+1;
	}
	
	this.getSerieImageUploadDir = function() {
		$.ajax({
			url: 'ajax_request/getUploadDirFor/serie',
			type: "POST",
			async: false,
			success: function(result) {
				This.dir = result;
			}
		});
	}
	
	this.getSerieImagesCount = function() {
		$.ajax({
			url: 'ajax_request/getSerieImagesNumber/'+This.ID,
			type: "POST",
			async: false,
			success: function(result) {
				if( parseInt(result) == 0 ) {
					$(This.cont).parent().find('.loader').fadeOut();
					return;
				}
				This.itemsSize = parseInt(result);
			}
		});
	}
	
	/*set current configuration to object*/
	this.setCurrentConfig = function( ) {
		var currentConfig = this.items[this.currIndex];
		this.currentSwitch = currentConfig['switch'];
		this.currentFrame = currentConfig['frame'];
		this.currentSelector = currentConfig['selector_id'];
	}
	
	/*get ID of current switch*/
	this.getCurrentCollection = function() {
		return this.ID;
	}
	
	/*get ID of current switch*/
	this.getCurrentOffset = function() {
		return (this.currIndex-this.step);
	}
	
	/*get ID of current switch*/
	this.getCurrentSwitch = function() {
		return this.currentSwitch;
	}
	
	/*get ID of current frame*/
	this.getCurrentFrame = function() {
		return this.currentFrame;
	}
	
	/*get ID of current frame*/
	this.getSelectorId = function() {
		return this.currentSelector;
	}
	
	function _imageIsReady() {
		var img = $(This.items[This.currIndex]['block']).find('img')[0];
		if( img.width > 100 && img.style.marginLeft != '' ) {
			$(This.items[This.currIndex]['block']).height('100%');
			setOffset(img);
			
			This.stepHor = $(This.items[This.currIndex]['block']).width()/$(This.items[This.currIndex]['block']).find('img').length;
			This.stepVert = $(This.items[This.currIndex]['block']).height();
			$(This.cont).parent().find('.loader').fadeOut();
			setContOffset();
		}
		else {
			setTimeout(_imageIsReady, 100);
		}
	}
	
	/*set to default state*/
	this.restart = function( static ) {
		switch( This.slideEffect ) {
			case 'fade':			
				if( static ) {
					This.currIndex = parseInt($('input[name="selectedItem"]').val());
				}
				else if( This.itemsSize && This.itemsSize > 2 ) {
					This.currIndex = 1;
				} else {
					This.currIndex = 0;
				}
				for( var i in This.items ) {
					$(This.items[i]['block']).css({'bottom': '0px', 'height':'0px'});
				}
				$(This.items[This.currIndex]['block']).css({'top': '0px', 'height': This.stepVert+'px'});				
				
				This.cont
					.height('0px')
					.show();
				var img = $(This.items[This.currIndex]['block']).find('img')[0];				
				setOffset(img);
				setContOffset();
				This.isDisabled = false;
				This.currListIndex = 0;
				break;
			default:
			break;
		}
	}
	
	function setContOffset() {
		This.setCurrentConfig();
		This.cont.css({'backgroundPosition': '0px 100%'});
		This.cont.find('.overflow').css( 'margin-left', '0px' );
		This.cont.height('auto').hide();
		This.cont.fadeIn();
	}
	
	function setOffset( img) {
		var height = $(This.items[This.currIndex]['block']).height()-$(img).height();
		height += height%2 != 0 ? 1 : 0;
		$(img).css('top', height/2+'px');
		$(img).css('bottom', 'auto');
	}
	
	/*draw element on mount()*/
	this.startDraw = function() {
		window[This.selectMethod['firstSelectMethod']]({}, This);
	}
	
	/*select elements from DB or another place*/
	this.getItemFromDB = function( index, position ) {
		window[This.selectMethod['selectMethod']](This.selectMethod['params'], This, position, This.items[This.currIndex]['block']);
	}
	
	/*load new element following current*/
	this.getNextItem = function( index ) {
		this.getItemFromDB(index, 1);
	}
	
	/*load new element previous current*/
	this.getPrevItem = function( index ) {
		this.getItemFromDB(index, -1);
	}
	
	/*if more that 1 element in row function slide to right element*/
	this.getRightItem = function() {
		switch( This.slideEffect ) {
			case 'fade':				
				slideToSide( 1 );				
				break;
			default:
				break;
		}		
	}
	
	/*if more that 1 element in row function slide to left element*/
	this.getLeftItem = function() {
		switch( This.slideEffect ) {
			case 'fade':
				slideToSide( -1 );
				break;
			default:
				break;
		}
	}
	
	function slideToSide( direction ) {		
		var firstCollId = $('#PnMain').find('#PnCn').find('.slider')[0].id;
		firstCollId = firstCollId.substr(firstCollId.indexOf('_')+1);
		
		var backSlideHor = This.ID.toLowerCase() == firstCollId ? 775 : 762;
		var backSlideVert = This.ID.toLowerCase() == firstCollId ? '190%' : '100%';
		var backSlide = direction > 0 ? '-'+backSlideHor+'px '+backSlideVert : '0px 100%';
		var listSlides = $(This.items[This.currIndex]['block']).find('.collection-item');
		if( direction > 0 ) {
			if( This.isDisabled || This.currListIndex+1 == listSlides.length ) {
				return false;
			}
			$(listSlides[This.currListIndex]).css('margin-right', '-'+This.stepHor+'px');
		}
		else {
			if( This.isDisabled || This.currListIndex-1 < 0 ) {
				return false;
			}
			This.cont.find('.overflow').css('margin-left', '0px');
		}
		This.isDisabled = true;			
				
		if( !browserIsNotIELower9() ) {
			$(listSlides[This.currListIndex]).hide();
		}
		else {
			$(listSlides[This.currListIndex]).fadeOut();
		}
		This.currListIndex += direction;
		
		if( direction < 0 ) {		
			$(listSlides[This.currListIndex]).css('margin-right', '-'+This.stepHor+'px')
		}
		
		$(listSlides[This.currListIndex])
			.hide()
			.fadeIn(function() {
				if( direction > 0 ) {
					This.cont.find('.overflow').css('margin-left', '-'+This.stepHor+'px');
					$(listSlides[This.currListIndex-1])
						.css('margin-right', '0px');
						This.cont.find('.collection-item').each(function(){ $(this).show() });
				}
				else {
					$(this).css('margin-right', '0px');					
					This.cont.find('.collection-item').each(function(){ $(this).show() });
				}
					This.isDisabled = false;
			});
		
		This.cont.css({'backgroundPosition': backSlide});
	}
}
