/**
* @author BahuL
* @copyright Goresh
*/
function goreshScrollBar(params){

	this._params = new Array();
	this._errors = false;
	this.pressed = false;
	this.position = 0;
	this.setParams(params);
	this.init();
};

goreshScrollBar.prototype.setParams = function(params){
	this.type = (params.type == 'vertical' ? 'top' : 'left');
	this.sizeType = (params.type == 'vertical' ? 'height' : 'width');
	this.scroll = $(params.divScroll);
	/*this.scrollLine = $(params.divScroll + ' ' + params.divScrollLine);*/
	//if Scroll line need smaller than scrall div
	this.scroller = $(params.divScroll + ' ' + params.divScroller);
	this.onStart = (typeof params.onChange == 'function' ? params.onChange : function(position){
	});
	this.onChange = (typeof params.onChange == 'function' ? params.onChange : function(position){
	});
	this.onStop = (typeof params.onChange == 'function' ? params.onChange : function(position){
	});
	this.position = params.position;
	if (!this.scroll.size() && !this.scroller.size() /*&& !this.scrollLine.size()*/) {
		this._errors = true;
	}
};

goreshScrollBar.prototype.setPosition = function(position){
	this.position = (parseInt(position) > 0 ? (position > this.srcollAreaSize ? this.srcollAreaSize : position) : 0);
	this.position = Math.round((this.position * this.srcollAreaSize) / 100);
	this.scroller.css(this.type, this.position + 'px');
	this.onChange(this.getPosition());
};
goreshScrollBar.prototype.getPosition = function(position){
	return Math.round((this.position * 100) / this.srcollAreaSize);
};

goreshScrollBar.prototype.scrollTo = function(to){
	to = (parseInt(to) > 0 ? (to > 100 ? 100 : to) : 0);
	//to = Math.round((this.to * this.srcollAreaSize) / 100);
	var k = Math.round(this.srcollAreaSize / 50);
	var obj = this;
	clearInterval(this.timerId);
	this.timerId = setInterval(function(){
		cPos = obj.getPosition();
		if (to > obj.getPosition(cPos)) {
			obj.setPosition(cPos + k);
			cPos = obj.getPosition();
			if (to < obj.getPosition()) {
				clearInterval(obj.timerId);
			}
		}
		else {
			obj.setPosition(cPos - k);
			cPos = obj.getPosition();
			if (to > obj.getPosition()) {
				clearInterval(obj.timerId);
			}
		}
	}, 100);

	// this.onChange(this.getPosition());
}

goreshScrollBar.prototype.init = function(){
	var obj = this;
	this.scroller.css('position', 'absolute');
	this.scrollSize = (this.sizeType == 'width' ? this.scroll.width() : this.scroll.height());
	//this.scrollLineSize = (this.sizeType == 'width' ? this.scrollLine.width() : this.scrollLine.height()); //if Scroll line need smaller than scrall div
	this.scrollerSize = (this.sizeType == 'width' ? this.scroller.width() : this.scroller.height());
	//this.srcollAreaSize = this.scrollSize - this.scrollLineSize; //if Scroll line need smaller than scrall div
	this.srcollAreaSize = this.scrollSize - this.scrollerSize;
	this.setPosition(this.position);

	this.scroller.draggable({
		cursor: "move",
		drag: function(event, ui){
			obj.drag((obj.type == 'top' ? ui.position.top : ui.position.left));
			obj.onChange(obj.getPosition());
		},
		stop: function(event, ui){
			obj.onStop(obj.getPosition());
		},
		start: function(event, ui){
			obj.onStart(obj.getPosition());
		},
		axis: (obj.type == 'top' ? 'y' : 'x'),
		containment: 'parent'
	});
};

goreshScrollBar.prototype.drag = function(position){
	this.position = position;
};

/* **************************************************** */

function goreshScrollGallery(params){
	this.position = 0;
	this.elements = new Array();
	this.activeElemet = 0;
	this.elemetnList = {};
	if (this.setParams(params)) {
		this._init();
	}
	this.opened = false;

};

goreshScrollGallery.prototype.setParams = function(params){
	var obj = this;
	this.mask = $(params.mask);
	this.list = $(params.list);
	this.bigImg = $(params.bigImg);
	this.shadow = $(params.shadow);
	$(window).resize(function(){
		obj.shadow.height(obj.shadow.parent().height()+50);
		obj.shadow.width(obj.shadow.parent().width()+10);
	});
	this.closeButton = $(params.closeButton);
	this.elementList = this.list.find(params.element);
	//alert(this.elementList.size());
	if (!this.elementList.size()) {
		return false;
	}

	this.activeElement = $(this.elementList[0]);
	this.activeElement.parent().attr('class', 'image_l_a');
	this.type = (params.type == 'vertical' ? 'top' : 'left');
	this.sizeType = (params.type == 'vertical' ? 'height' : 'width');
	this.params = params;
	this.image = this.bigImg.find('#picture').find('img');
	return true;
};

goreshScrollGallery.prototype._init = function(params){
	obj = this;

	this.maskSize = (this.sizeType == 'width' ? this.mask.width() : this.mask.height());
	this.listSize = (this.sizeType == 'width' ? this.list.width() : this.list.height());
	this.srcollAreaSize = this.listSize - this.maskSize;
	if (this.srcollAreaSize < 0) {
		this.srcollAreaSize = 0;
	}
	this.scroll = new goreshScrollBar({
		divScroll: this.params.divScroll,
		divScrollLine: this.params.divScrollLine,
		divScroller: this.params.divScroller,
		type: this.params.type,
		position: 0,
		onChange: function(position){
			obj.setPosition(position);
		}
	});
	this.elementList.click(function(){
		obj.activeElement.parent().attr('class', 'image_l');
		obj.activeElement = $(this);
		obj.activeElement.parent().attr('class', 'image_l_a');
		obj.loadBigImg();
		return false;
	});
};


goreshScrollGallery.prototype.setPosition = function(position){
	var obj = this;
	this.position = Math.round((this.srcollAreaSize * position / 100));
	params = {};
	params[this.type] = -1 * this.position;
	this.list.css(params);
};

goreshScrollGallery.prototype.getPosition = function(position){
	if (!position) {
		return Math.round((this.position * 100) / this.srcollAreaSize);
	}else{
		return Math.round((position * 100) / this.srcollAreaSize);
	}
};

goreshScrollGallery.prototype.loadBigImg = function(){
	var obj=this;
	var loader = obj.bigImg.find('#gal_loader');
	if (!obj.opened) {
		obj.opened = true;
		obj.image.hide();
		obj.bigImg.show();
		loader.show();
		obj.shadow.show();
		obj.shadow.height(obj.shadow.parent().height()+50);
		obj.shadow.width(obj.shadow.parent().width());
		obj.image[0].onload = function(){
			loader.hide();
			obj.image.show();
		}
		if ($.browser.msie) {
			obj.image[0].onreadystatechange = function(){
				loader.hide();
				obj.image.show();
			}

		}
		obj.image[0].src = obj.activeElement[0].href;
	}
	else {
		obj.bigImg.show();
		obj.image.hide();
		loader.show();
		
		obj.image[0].onload = function(){
			loader.hide();
			obj.image.show();
		}
		if ($.browser.msie) {
			obj.image[0].onreadystatechange = function(){
				loader.hide();
				obj.image.show();
			}
		}
		obj.image[0].src = obj.activeElement[0].href;
	}
	this.closeButton.click(function(){
		obj.bigImg.hide();
		loader.hide();
		obj.image.hide();
		obj.shadow.hide();
		obj.opened = false;
		return false;
	});
};
/*
if ($.browser.msie) {
img[0].onreadystatechange = function(){
loader.hide();
img.show();
img.fadeTo(100, 1);
}
}
else {
img[0].onload = function(){
loader.hide();
img.show();
img.fadeTo(100, 1);
}
}
*/
