var Gallery = {	
	currentImage: 0,
	totalCount: 0,
	totalWidth: 0,
	wrapperWidth: 0,
	leftmargin: 0,
		
	initialize: function() {
		this.frame = jQuery('#image_wrap');
		this.slider = jQuery('#image_slider');
		this.totalWidth = this.slider.width();
		this.wrapperWidth = this.frame.width();
		this.leftmargin = 0;
		//this.arrow = jQuery('#hmodel-arrow');
		
		var arr = [];
		for(var i = 0; i < jQuery(".gallery_review a").length; i++)
			arr.push(jQuery(".gallery_review a").eq(i).attr('href'));			
		jQuery.imgpreload(arr,
		{
			each: function()
			{			
			},
			all: function()
			{
				Gallery.scrollTo(0, 0);
			}
		});         
		
		var g = '';
        for(var i = 0; i < jQuery(".gallery_review a").length; i++){
			this.totalCount++;
            g = g + '<a href="javascript:void(0)" rel="#overlay" onclick="Gallery.scrollTo('+i+');"><img src="' + jQuery(".gallery_review a").eq(i).attr('href') +'" alt=""></a>';
        }
        this.slider.append('<nobr>'+g+'</nobr>');		
		this.addHandlers();
	},

	addHandlers: function() {
		//if (!isIOS && !isMobile) {				
			var handler;
			handler = jQuery('#image_handler');
			handler.addClass("image_handler_grab");
			handler.click(function(event) {
				event.preventDefault();
			});
			handler.mousedown(jQuery.proxy(this.onMouseDown, this));
			this.overHandler = false;
			handler.hover(jQuery.proxy(function() {
				this.overHandler = true;
				//if (!this.inProcess) this.arrow.show();
			}, this), jQuery.proxy(function() {
				this.overHandler = false;
				//if (!this.inProcess) this.arrow.hide();
			}, this));
			jQuery(window).bind('resize', function()
			{
				Gallery.Resize();
			});
			this.frame.resize(jQuery.proxy(this.Resize, this));
		//}
	},

	onMouseDown: function(e) {
		jQuery(document).mouseup(jQuery.proxy(this.onMouseUp, this));
		jQuery(document).mousemove(jQuery.proxy(this.onMouseMove, this));

		e.preventDefault();

		this.startX = e.pageX;
		this.startScroll = this.slider.position().left;

        //console.log(this.startX + ", " + this.startScroll); 
        
		this.startTime = new Date().getTime();
	},

	onMouseUp: function(e) {
		jQuery(document).unbind('mousemove');
		jQuery(document).unbind('mouseup');

        //console.log('mu 1 css:' + this.slider.css('left'));
		e.preventDefault();
		
		this.move = (this.startX != e.pageX);
		var move = Math.floor(-(this.startX - e.pageX));        
		
		if(new Date().getTime() - this.startTime < 150){
			//click?
			var scrollto = 0;
			var imgs = jQuery('#image_slider a');
			for(var i = 0; i < this.totalCount; i++){
				if((this.startScroll+scrollto < e.pageX && this.startScroll+scrollto + imgs.eq(i).width() > e.pageX) || 
					(this.startScroll+scrollto+this.totalWidth < e.pageX && this.startScroll+scrollto+this.totalWidth + imgs.eq(i).width() > e.pageX)){
					this.scrollTo(i);
					break;
				}
				scrollto = scrollto + imgs.eq(i).width();
			}			
		}else		
		if(Math.abs(move) > 150){
			/*if(move > 0){
				this.scrollTo(Math.max(0, this.currentImage - 1));
			}else{
				this.scrollTo(Math.min(this.totalCount - 1, this.currentImage + 1));
			}*/
			var scrollto = 0;
			var imgs = jQuery('#image_slider a');
			for(var i = 0; i < this.totalCount; i++){
				if((this.startScroll+move+scrollto < this.wrapperWidth/2 && this.startScroll+move+scrollto + imgs.eq(i).width() > this.wrapperWidth/2) ||
					this.startScroll+move+scrollto+this.totalWidth < this.wrapperWidth/2 && this.startScroll+move+scrollto+this.totalWidth + imgs.eq(i).width() > this.wrapperWidth/2){
					this.scrollTo(i);
					break;
				}
				scrollto = scrollto + imgs.eq(i).width();
			}	
		}else{
			this.scrollTo(this.currentImage);
		}
        
        //console.log('mu2 css:' + this.slider.css('left'));
		
		this.startX = e.pageX;
		this.inProcess = false;
	},

	onMouseMove: function(e) {
        //console.log('mm1 css:' + this.slider.css('left'));
		this.move = (this.startX != e.pageX);
		var move = Math.floor(-(this.startX - e.pageX));

		if (this.move) {
			var newleft = this.startScroll+move;
			newleft = Math.max(this.wrapperWidth-this.totalWidth - this.leftmargin, newleft);
			newleft = Math.min(this.leftmargin, newleft);	
            //console.log('css:' + newleft);
			this.slider.css({left: newleft});			
			this.inProcess = true;		
		}
        //console.log('mm2 css:' + this.slider.css('left'));

		e.preventDefault();
	},
	
	scrollTo: function(e, speed) {        
        if(typeof speed == "undefined")
            speed = 100;
        //console.log('st1 css:' + this.slider.css('left'));
		var scrollto = this.leftmargin;
        var imgs = jQuery('#image_slider img');
		this.totalWidth = jQuery('#image_slider').width();          

		if(e >= 0 && e < imgs.length){
			for(var i = 0; i < imgs.length; i++){
				if(i == e){
					scrollto = scrollto + (imgs.eq(i).width() - this.wrapperWidth)/2;
					break;
				}
				scrollto = scrollto + imgs.eq(i).width();
			}
			scrollto = Math.min(this.totalWidth - this.wrapperWidth + this.leftmargin, scrollto);
			scrollto = Math.max(-this.leftmargin, scrollto);
			//console.log('css:' + this.slider.css('left') + " st:" + scrollto);
			this.slider.stop().animate({left: -scrollto}, speed);	
			this.currentImage = e;
			jQuery(".gallery_review a").removeClass("active");
            jQuery(".gallery_review a").eq(e).addClass("active");
		}
        //console.log('st2 css:' + this.slider.css('left'));
	},
	
	Resize: function(e) {
		if(this.frame.width() != this.wrapperWidth){
			this.wrapperWidth = this.frame.width();
			this.leftmargin = 0;            
            this.frame = jQuery('#image_wrap');
    		this.slider = jQuery('#image_slider');
    		this.totalWidth = this.slider.width();    		
                
			this.scrollTo(this.currentImage, 0);
		}		
	}
};

jQuery(document).ready(function(){

//jQuery('#portfolio_area path').CreateBubblePopup({themePath: 	'/wp-content/themes/informa/img/jquerybubblepopup-theme/'});
	//добавить класс
	jQuery('.about a').click(function(){
		jQuery('.about a').removeClass('active');
		jQuery(this).addClass('active');
	});
	if(jQuery(".gallery_review a").length){
		Gallery.initialize();
	} 

	jQuery(".gallery_review a").click(function() {
		if (jQuery(this).hasClass("active")) { return false; }
		Gallery.scrollTo(jQuery(".gallery_review a").index(jQuery(this)));
		return false;
	}).filter(":first").click(); 

}); 
