/**************************************************************************************************
* 	MXBG Class "imgParser", v280910.2151
* 	Управлява структурирани имена на файлове на изображения.
* 	Прави автоматичен разбор на името с цел автоматична обработка: индексиране и групиране
***************************************************************************************************/

var imgParser = new Object();
	
	imgParser.preloadError = false;	
	
	imgParser.imgTarget = null;	
	
	imgParser.group = "";
	imgParser.index = 0;
	imgParser.size = "";
	imgParser.width = 0;
	imgParser.height = 0;
	imgParser.filePath = "";	
	imgParser.fileName = "";
	imgParser.fileExt = "";
	
	//imgParser.indexMax = 0;
	//imgParser.indexMin = 0;	
	
	imgParser.decodeFilePath = 
		function(imgPath) {
			this.filePath = getFilePath(imgPath);
			this.fileName = getFileName(imgPath);
			this.fileExt = getFileExt(imgPath);
			this.group = this.fileName.substring(1, 4);
			this.index = parseInt( this.fileName.substring(4, 7) ,10);
			this.size = this.fileName.substring(7, 8);		
			this.width = parseInt( this.fileName.substring(8, 11) ,10);
			this.height = parseInt( this.fileName.substring(11, 14) ,10);
		} // imgParser.decodeFilePath()

	imgParser.encodeFilePath = 
		function() {
			this.fileName = ( 
				"P" + 
				this.group + 
				pad( parseInt(this.index), 3) +
				this.size +
				pad( parseInt(this.width), 3) +
				pad( parseInt(this.height), 3) 
			);
			return this.filePath + "/" + 
				this.fileName + "." +	this.fileExt ;
		}; // imgParser.encodeFilePath()

	imgParser.alertProperties =
		function() {
			alert( 
				"filePath=" + this.filePath +				
				"; fileName=" + this.fileName + 
				"; fileExt=" + this.fileExt +				
				"; group=" + this.group +
				"; index=" + this.index +
				"; size=" + this.size +			
				"; width=" + this.width +				
				"; height=" + this.height 
			);
		}; // alertProperties()

	imgParser.indexReset =
		function() {
			this.index = 0;
			return this.encodeFilePath();
		}; // indexReset()
		
	imgParser.indexNext =
		function() {
			this.index ++ ;
			return this.encodeFilePath();
		}; // indexNext()
		
	imgParser.indexPrev =
		function() {
			if (this.index > 0) { this.index -- };
			return this.encodeFilePath();
		}; // indexPrev()
		
	imgParser.toggleSize =
		function() {
			if (this.size == "S") {
				this.size = "B" 
				} else {
				this.size = "S" 
			};
			return this.encodeFilePath();
		}; // toggleSize()

	imgParser.decodeImageObj = 
		function(imgObj) {
			this.imgTarget = imgObj;
			this.decodeFilePath( this.imgTarget.attr('src') );	// взема файловия път на изображението
		}; // decodeImageObj()

		
	imgParser.swapLoadImage =
		function() {
			var imgObj = new Image();		

			imgObj.onerror = function () { 
				imgParser.imgTarget.attr('src', imgParser.indexReset() ) ;
				imgParser.imgTarget.animate({opacity: 1}, 'slow', function() { });
  			} //event: missing image source

			imgObj.onload = function(){
				imgParser.imgTarget.attr('src', imgObj.src) ;
				imgParser.imgTarget.animate({opacity: 1}, 'slow', function() { });
			} //event: preloading Ok

			imgParser.imgTarget.animate({opacity: 0}, 'slow', function() {
				imgObj.src = imgParser.encodeFilePath();
			}); //try change image src and animate
			
		}; // swapLoadImage()
		
		
		
		
		
		


