var SelectedItem;
var SelectedX, SelectedY;
var MouseX, MouseY;
calcFunction = false;
currentScroller = false;
secondScroller = false;
fDragStart = false;
elBegin = 0 ;
mPosX = 0;
mPosY = 0;
$(document).ready(function(){

});

function getScreenX (event) {
	return event.clientX ? event.clientX : event.screenX;
}
	
function getLayerX (event) {
	return event.layerX ? event.layerX : event.offsetX;
}


scroller = function (container, minRange) {
	this.container = container;
	this.leftController = $(container).find('.left');	
	this.rightController = $(container).find('.right');
	this.leftKnob = $(container).find('.left-knob')
	this.rightKnob = $(container).find('.right-knob')
	this.range = $(container).find('.range');
	this.minRange = minRange;
	this.isDragging = false;
	this.ctlOffset = 0;
	this.calcFunction = false;
	this.currentCtl = false;
	this.secondCtl = false;
	this.handlers = new Array();
	this.currentEvent = false;
	
	this.dragStart = function (){
		this.isDragging = true;
	}
	
	this.dragStop = function (){
		this.isDragging = false;
	}
	
	this.addHandler = function(event, func){
		this.handlers[event] = func;
	}
	
	this.triggerEvent= function(event) {
		if (this.handlers[event]) {
			this.handlers[event]();
		}
	}
	
	this.stopPropagation = function(event){
		if(event.stopPropagation) {
  			event.stopPropagation();
  		} else {
  			event.cancelBubble = true;
  		}	
	}
	
	this.resizeControllers = function(calcFunction, currentCtl, secondCtl, screenOffsetX, cltGlobalOffset){			
	
		if (calcFunction) {
			currentCtlWidth = calcFunction (screenOffsetX, cltGlobalOffset);
		}
		secondCtlWidth = $(secondCtl).width();
		rangeWidth = $(this.container).width() - currentCtlWidth - secondCtlWidth;
		
		if (rangeWidth < this.minRange) {
			currentCtlWidth = $(this.container).width() - secondCtlWidth - this.minRange;
			rangeWidth = $(this.container).width() - currentCtlWidth - secondCtlWidth;
		}	
		
		
		
		if (secondCtlWidth + currentCtlWidth + rangeWidth > this.container.width()) {
			
		}
		
		if ($(currentCtl).hasClass('left')) {
			
			this.leftKnob.css('left', cltGlobalOffset + currentCtlWidth);
		}
		
		if ($(currentCtl).hasClass('right')) {
			this.rightKnob.css('left', cltGlobalOffset - currentCtlWidth - this.rightKnob.width());
		}
		$(currentCtl).width(currentCtlWidth);		
		$(this.range).width(rangeWidth);
		
		this.triggerEvent(this.currentEvent);	
	}
	
	this.calculateLeftCtlSize = function(screenOffsetX, ctlGlobalOffset){
		rezult = screenOffsetX - ctlGlobalOffset; 
		return rezult <= 0 ? 0 : rezult;
	}
	
	this.calculateRightCtlSize = function(screenOffsetX, ctlGlobalOffset){
		rezult = ctlGlobalOffset - screenOffsetX;
		return rezult <= 0 ? 0 : rezult;
	}
	
	this.linkScroller = function(jElements){
		elements = jElements.get();
		for (i in elements) {
			element = elements[i];
			element.scroller = this;
		}
	}
	
	this.linkScroller(this.leftKnob);
	this.linkScroller(this.rightKnob);
	
	$(this.leftKnob).mousedown(function (event) {
		scl = this.scroller;	
		scl.stopPropagation(event);									
		scl.dragStart();			
		
		scl.ctlOffset = event.screenX - scl.leftController.width() - $(this).width(); 
		
		scl.calcFunction = scl.calculateLeftCtlSize;
		scl.currentCtl = scl.leftController;
		scl.secondCtl = scl.rightController;
		scl.currentEvent = 'leftSizeChange';	
	});
	
	$(this.rightKnob).mousedown(function(event){
		scl = this.scroller;
		scl.stopPropagation(event);
		scl.dragStart();
		scl.ctlOffset = event.screenX  + scl.rightController.width() + $(this).width();
		scl.calcFunction = scl.calculateRightCtlSize;
		scl.currentCtl = scl.rightController;
		scl.secondCtl = scl.leftController;
		scl.currentEvent = 'rightSizeChange';
	});
	
	$(this.leftController).mousedown(function (event) {
		scl = this.scroller;	
		scl.stopPropagation(event);									
		scl.dragStart();			
		
		scl.ctlOffset = event.screenX - (event.layerX ? event.layerX : event.offsetX);
		
		scl.calcFunction = scl.calculateLeftCtlSize;
		scl.currentCtl = this;
		scl.secondCtl = scl.rightController;
		scl.currentEvent = 'leftSizeChange';
	}).click(function(event){	
		scl = this.scroller;		
		screenX = getScreenX(event);
		layerX = getLayerX(event);		
		scl.currentEvent = 'leftSizeChange';
		scl.resizeControllers(scl.calculateLeftCtlSize, this, scl.rightController, screenX, screenX - layerX); 
	});
	
	this.linkScroller(this.leftController);
	
	
	
	
	$(this.rightController).mousedown(function(event){
		scl = this.scroller;
		scl.stopPropagation(event);
		scl.dragStart();
		scl.ctlOffset = event.screenX + $(this).width() - (event.layerX ? event.layerX : event.offsetX) ;
		scl.calcFunction = scl.calculateRightCtlSize;
		scl.currentCtl = this;
		scl.secondCtl = scl.leftController;
		scl.currentEvent = 'rightSizeChange';
	}).click(function(event){
		scl = this.scroller;		
		offset = event.screenX + $(this).width() - (event.layerX ? event.layerX : event.offsetX) ;
		scl.currentEvent = 'rightSizeChange';		
		scl.resizeControllers(scl.calculateRightCtlSize, this, scl.leftController, event.screenX, offset);
	});
	this.linkScroller(this.rightController);
	
	$(this.range).click(function(event){
		scl = this.scroller;
		layerOffset = event.layerX ? event.layerX : event.offsetX; 
		if (layerOffset > $(this).width() / 2) {		
			elementOffset = event.screenX + $(scl.rightController).width() + $(this).width() - layerOffset;
			scl.currentEvent = 'rightSizeChange';			
			scl.resizeControllers(scl.calculateRightCtlSize, scl.rightController, scl.leftController, event.screenX, elementOffset);	
		} else {		
			elementOffset = event.screenX - layerOffset - $(scl.leftController).width();
			scl.currentEvent = 'leftSizeChange';			
			scl.resizeControllers(scl.calculateLeftCtlSize, scl.leftController, scl.rightController, event.screenX, elementOffset);
		}
	});
	this.linkScroller(this.range);
		
	$(this.container).mousemove(function(event){
		
		scl = this.scroller;	
		scl.stopPropagation(event);	
		if (scl.isDragging) {
			
			scl.resizeControllers(scl.calcFunction, scl.currentCtl, scl.secondCtl, event.screenX, scl.ctlOffset);
		}
	}).mouseup(function(){	
		scl = this.scroller;		
		scl.dragStop();
	}).attr('scroller', this);
	this.linkScroller(this.container);
	
	if (typeof($(document).attr('scrollers')) == 'undefined') {	
		$(document).attr('scrollers', new Array());
		isDocumentInitialized = false;
	} else {
		isDocumentInitialized = true;
	}
	
	arScrollers = $(document).attr('scrollers');
	
	arScrollers[arScrollers.length] = this;
	
	if (!isDocumentInitialized) {
		$(document).mouseup(function(){
			scrollers = $(document).attr('scrollers');			
			$.each(scrollers, function(key){
				scrollers[key].dragStop();
			});
		});
	}
	
	
}

function changeWidth(type_line){
}
$(document).ready(function(){
	scroller1 = new scroller($('#line_price'), 5);
	scroller1.addHandler('leftSizeChange', function(){
	  oldValue = $('input[id="TotalPrice[min]"]').attr('value');
		$('input[id="TotalPrice[min]"]').attr('value', ($('#line_price .left').width() / 2).toFixed(1));
		//YFilter.onRefreshFilter("TotalPrice[min]", oldValue, $('input[id="TotalPrice[min]"]').attr('value'));
	});
	scroller1.addHandler('rightSizeChange', function(){
	  oldValue = $('input[id="TotalPrice[max]"]').attr('value');
		$('input[id="TotalPrice[max]"]').attr('value',(($('#line_price .left').width() + $('#line_price .range').width()) / 2).toFixed(1));
		//YFilter.onRefreshFilter("TotalPrice[max]", oldValue, $('input[id="TotalPrice[max]"]').attr('value'));
	});
	scroller2 = new scroller($('#line_area'), 5);
	scroller2.addHandler('leftSizeChange', function(){
	  oldValue = $('input[id="SAll[min]"]').attr('value');
		$('input[id="SAll[min]"]').attr('value', ($('#line_area .left').width() * 2).toFixed(1));
		//YFilter.onRefreshFilter("SAll[min]", oldValue, $('input[id="SAll[min]"]').attr('value'));
	});
	scroller2.addHandler('rightSizeChange', function(){
	  oldValue = $('input[id="SAll[max]"]').attr('value');
		$('input[id="SAll[max]"]').attr('value',(($('#line_area .left').width() + $('#line_area .range').width()) * 2).toFixed(1));
		//YFilter.onRefreshFilter("SAll[max]", oldValue, $('input[id="SAll[max]"]').attr('value'));
	});
	
	scroller3 = new scroller($('#line_count_rooms'), 20);
	scroller3.addHandler('leftSizeChange', function(){
	  oldValue = $('input[id="Rooms[min]"]').attr('value');
		$('input[id="Rooms[min]"]').attr('value', ($('#line_count_rooms .left').width() / 20).toFixed(0));
		//YFilter.onRefreshFilter("Rooms[min]", oldValue, $('input[id="Rooms[min]"]').attr('value'));
	});
	scroller3.addHandler('rightSizeChange', function(){
	  oldValue = $('input[id="Rooms[max]"]').attr('value');
		$('input[id="Rooms[max]"]').attr('value',((($('#line_count_rooms .left').width() + $('#line_count_rooms .range').width()) / 20).toFixed(0)));
		//YFilter.onRefreshFilter("Rooms[max]", oldValue, $('input[id="Rooms[max]"]').attr('value'));
	});
	
});


$(document).keyup(function(){
  price_from_value = (($('input[id="TotalPrice[min]"]').attr('value') ? $('input[id="TotalPrice[min]"]').attr('value') : 0) * 2);
  price_to_value = (($('input[id="TotalPrice[max]"]').attr('value') ? ($('input[id="TotalPrice[max]"]').attr('value') > 2 ? $('input[id="TotalPrice[max]"]').attr('value') : 2) : 0) * 2);
  if(price_to_value > 200){
    $('input[id="TotalPrice[max]"]').attr('value',100);
    price_to_value = 200;
  }else if(price_to_value ==0){
    $('#input[id="TotalPrice[max]"]').attr('value','');
    price_to_value = 4;
  }
  if((price_to_value - price_from_value) > 2){
    new_middle_width = $('#line_price').width() - ($('#line_price').width() - price_to_value) - price_from_value;
    range = $('#line_price').find('.range');
    $('#line_price .left').width(price_from_value);
    range.width(new_middle_width);
    new_right_width = $('#line_price').width() - range.width() - price_from_value;
    $('#line_price .right').width(new_right_width);
  }else{
    new_middle_width = 4;
    range = $('#line_price').find('.range');
    range.width(new_middle_width);
    $('#line_price .left').width(price_to_value - 4);
    new_price_from_value = (price_to_value / 2 - 2) > 0 ? (price_to_value / 2 - 2) : 0;
    $('input[id="TotalPrice[min]"]').attr('value',new_price_from_value);
    if(new_price_from_value == 0){
      new_price_right_width = $('#line_price').width() - range.width() - new_price_from_value;
      $('#line_price .right').width(new_price_right_width);
    }
  }
  
  area_from_value = (($('input[id="SAll[min]"]').attr('value') ? $('input[id="SAll[min]"]').attr('value') : 0) / 2);
  area_to_value = (($('input[id="SAll[max]"]').attr('value') ? ($('input[id="SAll[max]"]').attr('value') > 2 ? $('input[id="SAll[max]"]').attr('value') : 2) : 0) / 2);
  if(area_to_value > 200){
    $('input[id="SAll[max]"]').attr('value',400);
    area_to_value = 200;
  }else if(area_to_value == 0){
    area_from_value = 0;
    $('input[id="SAll[max]"]').attr('value','');
    $('input[id="SAll[min]"]').attr('value',0);
    area_to_value = 4;
  }
  if((area_to_value - area_from_value) > 0.5){
    new_middle_width = $('#line_area').width() - ($('#line_area').width() - area_to_value) - area_from_value;
    range = $('#line_area').find('.range');
    $('#line_area .left').width(area_from_value);
    range.width(new_middle_width);
    new_right_width = $('#line_area').width() - range.width() - area_from_value;
    $('#line_area .right').width(new_right_width);
  }else{
    new_middle_width = 4;
    range = $('#line_area').find('.range');
    range.width(new_middle_width);
    $('#line_area .left').width(area_to_value - 4 > 0 ? (area_to_value - 4) : 0);
    new_area_from_value = (area_to_value * 2 - 2) > 0 ? (area_to_value * 2 - 2) : 0;
    $('input[id="SAll[min]"]').attr('value',new_area_from_value);
    if(new_area_from_value == 0){
      new_area_right_width = $('#line_area').width() - range.width() - new_area_from_value;
      
      $('#line_area .right').width(new_area_right_width);
    }
  }
  
  count_rooms_from_value = (($('input[id="Rooms[min]"]').attr('value') ? $('input[id="Rooms[min]"]').attr('value') : 0) * 20);
  count_rooms_to_value = (($('input[id="Rooms[max]"]').attr('value') ? ($('input[id="Rooms[max]"]').attr('value') > 1 ? $('input[id="Rooms[max]"]').attr('value') : 1) : 0) * 20);
  if(count_rooms_to_value > 200){
    $('input[id="Rooms[max]"]').attr('value',10);
    count_rooms_to_value = 200;
  }else if(count_rooms_to_value == 0){
    $('input[id="Rooms[max]"]').attr('value','');
    count_rooms_to_value = 20;
  }
  if((count_rooms_to_value - count_rooms_from_value) > 20){
    new_middle_width = $('#line_count_rooms').width() - ($('#line_count_rooms').width() - count_rooms_to_value) - count_rooms_from_value;
    range = $('#line_count_rooms').find('.range');
    $('#line_count_rooms .left').width(count_rooms_from_value);
    range.width(new_middle_width);
    new_right_width = $('#line_count_rooms').width() - range.width() - count_rooms_from_value;
    $('#line_count_rooms .right').width(new_right_width);
  }else{
    //alert('tyt');
    new_middle_width = 20;
    range = $('#line_count_rooms').find('.range');
    range.width(new_middle_width);
    $('#line_count_rooms .left').width(count_rooms_to_value - 20);
    new_count_rooms_from_value = (count_rooms_to_value / 20 - 1) > 0 ? (count_rooms_to_value / 20 - 1) : 0;
    $('input[id="Rooms[min]"]').attr('value',new_count_rooms_from_value);
    if(new_count_rooms_from_value == 0){
      new_count_rooms_right_width = $('#line_count_rooms').width() - range.width() - new_count_rooms_from_value;
      $('#line_count_rooms .right').width(new_count_rooms_right_width);
    }
  }
  
});
