var changesearch=false
var searchval=""
var scrollval=0
var scrollnow
var scrolltimer
var scrolldop
var scrolling = false
var SCROLLMINSPEED=1
var SCROLLMAXSPEED=10
$(document).ready(
  function() {
  		$('.select select').hide();
		$('.select').prepend('<div class="nowvalue" onmousedown="startcheck(this)"></div><div class="values"></div>');
		a=$('.select')
		for(i=0; i<a.length; i++) {
			a2=$(a[i]).find("option")
			$(a[i]).find('.nowvalue').text($(a[i]).find("option:selected").text())
			for(j=0; j<a2.length; j++) {
				$(a[i]).find('.values').append('<div onmouseover="setcolor(this)" onmouseout="clearcolor(this)" onclick="check(this, '+j+')">'+$(a2[j]).text()+'</div>').hide()
			}
		}
		document.body.onclick=function() {closecheck()}
		
		// поиск
		if(document.getElementById('search')) {
		document.getElementById('search').onfocus=function() {
			if(!changesearch) {
				searchval=document.getElementById('search').value
				document.getElementById('search').value=''	
			}
		}
		document.getElementById('search').onblur=function() {
			if(!changesearch) {
				document.getElementById('search').value=searchval	
			}
		}
		document.getElementById('search').onchange=function() {
			changesearch=true
		}
		}
		// /поиск
		
		// скролл
		if(document.getElementById('scroll'))
		{	
		scrollnow=document.getElementById('lenta').offsetLeft
		scrolldop=$('#lenta td').clone().appendTo('#lenta tr')
		scrolldop.hide()
		$('#scrollforward, #scrollback').hide()
		
		document.getElementById('scroll').style.overflow='hidden'
		document.getElementById('scrollforward').onmouseover = function () {
			scrollstop()
			scrollval=-SCROLLMINSPEED
			scrolltimer=window.setInterval(doscroll,40)			
		}
		document.getElementById('scrollforward').onmouseout = function () {
			scrollstop()
		}
		document.getElementById('scrollback').onmouseover = function () {
			scrollstop()
			scrollval=SCROLLMINSPEED
			scrolltimer=window.setInterval(doscroll,40)			
		}
		document.getElementById('scrollforward').onmousedown = function () {
			scrollval=-SCROLLMAXSPEED
		}
		document.getElementById('scrollforward').onmouseup = function () {
			scrollval=-SCROLLMINSPEED		
		}
		document.getElementById('scrollback').onmouseout = function () {
			scrollstop()
		}
		document.getElementById('scrollback').onmousedown = function () {
			scrollval=SCROLLMAXSPEED
		}
		document.getElementById('scrollback').onmouseup = function () {
			scrollval=SCROLLMINSPEED	
		}
		window.onload = function() {drawknop();}
		window.onresize = function() {drawknop();}
		}
		// /скролл
		
		//подстветка аренды
		// /подстветка аренды
		
		$('.top_menu li').hover(
		function() {
			$(this).find('ul').css('display','block')
			$(this).find('.f1').css('position','relative')
		}
		,
		function() {
			$(this).find('ul').css('display','none')
			$(this).find('.f1').css('position','static')
		}
		)
  }  
);

function drawknop() {
	scrollw=scrolling ? document.getElementById('lenta').clientWidth/2 : document.getElementById('lenta').clientWidth
	//alert(scrolling)
	if(scrollw>document.getElementById('scroll').clientWidth) {
		$('#scrollforward, #scrollback').show()
		scrolldop.show()
		scrolling=true
	} else {
		$('#scrollforward, #scrollback').hide()
		$('#lenta').css('margin-left','')
		scrolldop.hide()
		scrolling=false
	}
}
function scrollstop() {
	clearInterval(scrolltimer)
}
function doscroll() {
	scrollw=document.getElementById('scroll').clientWidth
	lentaw=document.getElementById('lenta').clientWidth
	if (scrollval < 0 && scrollnow+lentaw+scrollval<=scrollw)  {
		scrollnow+=lentaw/2	
	} else if (scrollval > 0 && scrollnow+scrollval>=0)  {
		scrollnow-=lentaw/2
	}
	scrollnow+=scrollval
	document.getElementById('lenta').style.marginLeft=scrollnow+'px'
}


function setcolor(e) {

	$(e).addClass('sel')	
}
function clearcolor(e) {
	$(e).removeClass('sel')	
}
var start=false
var now=false
function startcheck(e) {	
	if(now) {
		now =false
		closecheck()
	} else {
		start=true
		now=true
		$(e).parent().find('.values').show()
		$(e).parent().css('z-index','30')
	}
}

function check(e, n) {
	selectblock=$(e).parent().parent()	
	selectblock.find('select :nth('+ n + ')').attr('selected','selected')
	selectblock.find('.nowvalue').text($(e).text())
	selectblock.find('select').change()
}
function closecheck() {
   if(start==true) {
		start=false   
   } else {
   	$('.select .values').hide();	
	$('.select').css('z-index','0')
	now=false
   }
}

//календарь
function selected(cal, date) {
  cal.sel.value = date; 
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))   
  cal.callCloseHandler();
}
function closeHandler(cal) {
  cal.hide();                        

  _dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    
    _dynarch_popupCalendar.hide();                 
	alert('-')
  } else {
    
    var cal = new Calendar(1, null, selected, closeHandler);
    
    
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  
    cal.setRange(1900, 2070);        
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    
  _dynarch_popupCalendar.parseDate(el.value);      
  _dynarch_popupCalendar.sel = el;                 

  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");

  return false;
}
// /календарь


