

var days = ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'];
var month = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];
var minDate = '';
var darr = [];
var c_flag = false;
var calend = {};
calend.fc = true;
calend.startDay = false;
calend.endDay = false;

function resizer(){
    $('h_catalog').setStyle('left', ((window.getSize()).x % 2 == 0) ? 473 : 474); 
}

function onload(){
    if($('rhid0')) preloadImg();
}

function start(){
    if((window.getSize()).x % 2 == 0) $('h_catalog').setStyle('left', 473);
    for(i=1; i<3; i++) if($('splash'+i)) new elSelect( {container : 'splash'+i} );
    letBeLight();
    if($('roomsm')) actRoomsM();
    
    //alert((window.getSize()).x)
    
    
    if($$('.fl_list li')) actList();
    
    initMultiBox = new multiBox({
        mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
        container: $(document.body),//where to inject multiBox
        descClassName: false,//the class name of the description divs
        path: './Files/',//path to mp3 and flv players
        useOverlay: true,//use a semi-transparent background. default: false;
        addDownload: false,//do you want the files to be downloadable?
        pathToDownloadScript: false,//if above is true, specify path to download script (classicASP and ASP.NET versions included)
        addRollover: false,//add rollover fade to each multibox link
        addOverlayIcon: true,//adds overlay icons to images within multibox links
        showControls: true,//adds overlay icons to images within multibox links
        showNumbers: false,
        addChain: false,//cycle through all images fading them out then in
        recalcTop: false,//subtract the height of controls panel from top position
        addTips: false,//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
        clos: 'close2'
    });
    
    if($$('.rr_in')) actMSearch();
    
    if($('inp0')) {
        actSearch();
        
        $('dat0').value = '';
        $('dat1').value = '';
	
        for(i=0; i<2; i++){
		var pos = i ? {x: 0, y: 45} : {x: 187, y: 45};
            darr[i] = new DatePicker('.date'+i, {
                pickerClass: 'datepicker_dashboard',
                allowEmpty: true,
                days: days,
                months: month,
                yearPicker: false,
                positionOffset: pos,
                animationDuration: 0,
                allowArrows: i,
                secondCalendar: '',
		        useFadeInOut: false,
  		        inputOutputFormat: 'Y.m.d',
                minDate: { date: minDate, format: 'd-m-Y' },
                toggleElements: '.date_toggler'+i,
                onSelect: function(d){
                   // alert(d)
                }     
            });               
        }
        
       
         
	
	$('date0').addEvent('click', function(){
        $('dat0').value = '';
        $('dat1').value = '';
        
		var st = $('callhide').getStyle('display');
		if(st=='block'){
			closeCalend();
            $each($$('#splash1 div'), function(div){ 
                div.removeClass('startDay');
            });
		}else{
			$('callhide').setStyle('display', 'block');
			$('gdown').setStyle('margin', '755px 0 0 0');
			$('ccorn').src = 'img/cal_b_o.jpg';
            
            if(!calend.startDay){
                darr[0].next();
                darr[0].picker.getElement('.previous').setStyle('visibility', 'hidden');
                c_flag = true;     
            }else if(darr[0].d.getMonth() == darr[1].d.getMonth()){
                darr[0].next();     
            }
		}
        
       
            
       
	});
    
   
    }
  
}

function actMSearch(){
     $each($$('.rr_in'), function(box){ 
        var arr = box.getElements('a');
        $each(arr, function(element){ 
            var par = element.parentNode;
            element.addEvent('click', function(){
                   if(par.hasClass('radio')){
                       if(par.hasClass('radioact'))
                            par.removeClass('radioact');
                       else par.addClass('radioact');
                   }else{
                        arr.removeClass('act'); 
                        this.addClass('act'); 
                   }
                   return false; 
            });
        });
     });        
}  

function goSearchM(){
    var location = '';
    var complex = '';
    
    $each($$('.rr_in'), function(box){ 
        var comp = false;
        if(!box.hasClass('radio')) location += location ? '&' : 'apartment?';
        if(box.hasClass('fst_reg')) location += 'pl=';
        else if(box.hasClass('fst_room')) location += 'room=';
        else comp=true; 
        
        var arr = box.getElements('a');
        
        if(comp){
            if(box.hasClass('radioact')) complex += arr[0].href.replace(new RegExp('[^\\d+]*'), '')+',';
        }else{
            $each(arr, function(element){ 
                if(element.hasClass('act')) {
                    //var patt = new RegExp('[^\\d+]*');
                    location += element.href.replace(new RegExp('[^\\d+]*'), '');  
                }
            });  
        }
     });
     
     if(complex) location+='&complex='+complex;        
   
   // alert(location)
   document.location.href = location;
                              
}  

function closeCalend(){
     $('callhide').setStyle('display', 'none');
            $('gdown').setStyle('margin', '555px 0 0 0;');
            $('ccorn').src = 'img/cal_b_c.jpg';
            darr[0].close(null, true);
            darr[1].close(null, true);
            
     c_flag = false;
}

function preloadImg(){
    $each($$('#rhid0 img'), function(element, i){ 
        var img = new Image;
        img.src = element.src.replace('small/', '');
    });      
    
    if($('okr_img').value) {
        var img = new Image;
        img.src = 'user.img/'+$('okr_img').value;
    }
}

function setImg(src){
    $('imgsrc').src = 'user.img/'+src;
}

var Url = {
 
    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
 
    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
 
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
 
        for (var n = 0; n < string.length; n++) {
 
            var c = string.charCodeAt(n);
 
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
 
        }
 
        return utftext;
    },
 
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
 
        while ( i < utftext.length ) {
 
            c = utftext.charCodeAt(i);
 
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
 
        }
 
        return string;
    }
 
}
function slideImg(d){ 
    var src = '';
    var t = $('imgsrc').src.split('/');
    var old = t[t.length-1];
    var oldd = old.replace(/-thumb/g, '');   
    var set = false; var f_r = ''; var f_l = '';  
    var imgarr = $$('#rhid0 img');
    
    $each(imgarr, function(el, i){ 
        var e = el.src.split('/');
        var img = e[e.length-1]; 
        img = img.replace(/-thumb/g, '');       
        
        if(d=='r'){
            if(!i) f_r = img;
            if(set){src=img; set = false;}
            if(old==img) set = true;
        }else{
            if(old==img) src=set;           
            if(i==imgarr.length-1) f_l = img;
            set = img;
        }
        
    });
    
    if(!src){

        src = d=='r' ? f_r : f_l;
    }
     
     var n = Url.decode(src);  
       
     var csrc = n.replace(/-thumb/g, '');   
     var tt = new Image;
     tt.src = 'user.img/'+csrc;
    // alert(tt.src+'aaaaaaaaa')
    $('imgsrc').set('src', tt.src);
  //  alert(Url.decode($('imgsrc').src))
}

function corrA(){
    var pos = ($('r_main').getHeight() / 2).round() - 60;

    $('g_butt_l').setStyle('top', pos);
    $('g_butt_r').setStyle('top', pos);    
}

function actRoomsM(){
    var li = false;
	$each($$('#roomsm li'), function(element, i){ 
        if(actTab && actTab==i) li=element;
		element.addEvent('click', function(){
			$$('#roomsm li').removeClass('on');
			this.addClass('on');
			$$('.rhids').setStyle('display', 'none');
			$('rhid'+i).setStyle('display', 'block');
             if(i=='2') {
                if($('okr_img').value) $('imgsrc').src = 'user.img/'+$('okr_img').value;
             }
		});
	})   
    
    if(actTab){
         $$('#roomsm li').removeClass('on');
         li.addClass('on');
         $$('.rhids').setStyle('display', 'none');
         $('rhid'+actTab).setStyle('display', 'block');
    }          
}


function actSearch(){
    $each($$('#inp0 a'), function(element){ 
            if(!element.hasClass('link')) 
                element.addEvent('click', function(){
                // if(this.hasClass('act')){ 
                   $$('#inp0 a').removeClass('act'); 
                // }else
                    this.addClass('act');
                    
                return false;
            });
    })             
}

function goSearch(){
   var opt = $('s_region');
   reg = opt.value;
   
   var room = '';
   $each($$('#inp0 a'), function(element){
          if(element.hasClass('act')) room += element.innerHTML+',';
   })
   
   if($('compldate').value){
        document.location.href = 'apartment?pl='+reg+'&'+'room='+room+'&f_date='+$('compldate').value  
   }else{
        alert('Укажите пожалуйста дату заезда и выезда.')
   }
                              
}

function letBeLight(){
     var l_l = $$('#h_mi a')[0];
     var l_r = $$('#h_catalog a')[0];
     
     var il = new Image;
     il.src = 'img/light_l.jpg';
     
     var ir = new Image;
     ir.src = 'img/light_r.jpg';
     
        l_l.addEvent('mouseenter', function(){
            $('h_mi').setStyle('background', 'url('+il.src+')');
        });
        l_l.addEvent('mouseleave', function(){
           $('h_mi').setStyle('background', 'none');
        });
        
        l_r.addEvent('mouseenter', function(){
            $('h_catalog').setStyle('background', 'url('+ir.src+')');
        });
        l_r.addEvent('mouseleave', function(){
           $('h_catalog').setStyle('background', 'none');
        });
        
     
}

function actList(){
    var act = new Image;
    act.src = 'img/fl_bg_act.gif';
    $each($$('.fl_list li'), function(element){
        element.addEvent('mouseenter', function(){
            this.setStyle('background', 'url('+act.src+')'); 
        });
        element.addEvent('mouseleave', function(){
           this.setStyle('background', 'url(img/fl_bg.gif)');
        });
	element.addEvent('click', function(){
		document.location.href = 'apartment/' + this.id.replace('ap_', '');
           
	});
    });
}

function getRoom(v){
	document.location.href = 'apartment?room='+v;
}

function getPlace(v){
	document.location.href = 'apartment?pl='+v;
}




















function footOver(){
    $each($$('.foot_links a'), function(element, i){
        element.addEvent('mouseenter', function(){
            var reg = /(.*?)\.gif/;
            var ima = this.getElement('img');
            ima.src = ima.src.replace(reg, "$1_.gif");
        });
        element.addEvent('mouseleave', function(){
            var reg = /(.*?)\_.gif/;
            var ima = this.getElement('img');
            ima.src = ima.src.replace(reg, "$1.gif");
        });
    });
}

function startTitls(){
    $each($$('.titls img'), function(element, i){
        element.addEvent('mouseenter', function(){
            var reg = /(.*?)\.gif/;
            this.src = this.src.replace(reg, "$1_.gif");
        });
        element.addEvent('mouseleave', function(){
            var reg = /(.*?)\_.gif/;
            this.src = this.src.replace(reg, "$1.gif");
        });
   });
}

function startDoska(){
    $each($$('.item_img'), function(element, i){
        element.addEvent('mouseenter', function(){
            this.style.backgroundColor = '#fffe8c';
        });
        element.addEvent('mouseleave', function(){
            this.style.backgroundColor = '';
        });
   });
   var rat = $$('.rat_in');
   
   $each($$('.vote'), function(el, z){
        el.addEvent('mouseenter', function(){
            var reg = /(.*?)\.gif/;
            if (Browser.Engine.trident){
                rat[z].style.backgroundColor = '#fffe8c';
                this.parentNode.parentNode.childNodes[0].style.backgroundColor = '#fffe8c';
            }else{
                //rat[z].style.backgroundColor = '#fffe8c';
                m[z].style.backgroundColor = '#fffe8c';
                this.parentNode.parentNode.childNodes[1].style.backgroundColor = '#fffe8c';
            }
            
            this.childNodes[0].childNodes[0].src = this.childNodes[0].childNodes[0].src.replace(reg, "$1_.gif");
            
        });
        el.addEvent('mouseleave', function(){
            var reg = /(.*?)\_.gif/;
            if (Browser.Engine.trident){
                this.parentNode.childNodes[0].style.backgroundColor = '';
                rat[z].style.backgroundColor = '';
            }else{
                rat[z].style.backgroundColor = '';
            }
            this.childNodes[0].childNodes[0].src = this.childNodes[0].childNodes[0].src.replace(reg, "$1.gif");
        });
   });
   
   $each($$('.rat_in'), function(elem, w){
        elem.addEvent('mouseenter', function(){
            this.style.backgroundColor = '#fffe8c';
            if (Browser.Engine.trident){
                this.parentNode.parentNode.childNodes[0].style.backgroundColor = '#fffe8c';
            }else{
                this.style.backgroundColor = '#fffe8c';
                //m[w].style.backgroundColor = '#fffe8c';
                this.parentNode.parentNode.childNodes[1].style.backgroundColor = '#fffe8c';
            }            
        });
        elem.addEvent('mouseleave', function(){
            this.style.backgroundColor = '';
            if (Browser.Engine.trident){
                this.parentNode.parentNode.childNodes[0].style.backgroundColor = '';
            }else{
                rat[w].style.backgroundColor = '';
                this.parentNode.parentNode.childNodes[1].style.backgroundColor = '';
            }
        });
   });
}

function startInSlide(){
    myAccordion = new Accordion($$('.toggle'), $$('.slide'), {
            onActive: function(toggler, slider){
                if (Browser.Engine.trident) slider.setStyle('display', 'block');
                //slider.setStyle('padding-bottom', '20px');
            },
            onBackground: function(toggler, slider){
                if (Browser.Engine.trident) slider.setStyle('display', 'none');
            },
            onComplete: function(){
                
            },

            opacity: 0,
            display: false,
            alwaysHide: true
    });
}
function re(){
    $each($$('.imagi img'), function(element, i){
        
        element.addEvent('mouseenter', function(){
            
            this.parentNode.parentNode.style.backgroundColor = '#fff';
            
        });
        element.addEvent('mouseleave', function(){
            this.parentNode.parentNode.style.backgroundColor = '';
            
        });
    });
}
function startImag(){
    $each($$('.imag img'), function(element, i){
        element.addEvent('mouseenter', function(){
            this.parentNode.parentNode.setStyle('background-color', '#fff');
            
        });
        element.addEvent('mouseleave', function(){
            if (!element.parentNode.parentNode.hasClass('cur_dom')) this.parentNode.parentNode.setStyle('background-color', '');
        });
   });
   
   
    if ($$('.imag_numb')){
        $each($$('.imag_numb'), function(element, i){
            element.addEvent('mouseenter', function(){
                $('im'+i).parentNode.setStyle('background-color', '#fff');
                this.parentNode.setStyle('background-color', '#fff');
            });
            element.addEvent('mouseleave', function(){
                if (!element.hasClass('cur_dom')) this.parentNode.parentNode.setStyle('background-color', '');
            });
       });
   }
}
function startDomik(){
    $each($$('.imag'), function(element, i){
        if (!element.hasClass('cur_dom') && element.hasClass('domiki')) {
             $('im'+i).setStyle('opacity', .5);
        }
        
        element.addEvent('mouseenter', function(){
            if (!element.hasClass('cur_dom')) {
                this.setStyle('background-color', '#fff');
                this.childNodes[0].style.backgroundColor = '#fff';
                this.childNodes[0].style.display = 'block';
                //alert(this.className)
            }
            if (element.hasClass('domiki')) $('im'+i).setStyle('opacity', 1);
        });
        element.addEvent('mouseleave', function(){
            if (!element.hasClass('cur_dom')){ 
                this.setStyle('background-color', '');
                this.childNodes[0].style.backgroundColor = '';
                this.childNodes[0].style.display = 'none';
                if (element.hasClass('domiki')) $('im'+i).setStyle('opacity', .5);
            }
            
        });
   });
   
   
    if ($$('.imag_numb')){
        $each($$('.imag_numb'), function(element, i){
            element.addEvent('mouseenter', function(){
                $('im'+i).parentNode.parentNode.setStyle('background-color', '#fff');
                this.parentNode.setStyle('background-color', '#fff');
            });
            element.addEvent('mouseleave', function(){
                if (!element.hasClass('cur_dom')) this.parentNode.parentNode.parentNode.setStyle('background-color', 'transparent');
            });
            element.addEvent('click', function(){
                var a = this.parentNode.getElement('a').href;
                document.location.href = a;
            });
       });
   }
}

function startNavPage(){
    $each($$('.pages2 a'), function(element, i){
        element.addEvent('click', function(){
            var ima = $('domik_images').getElement('img');
            var nim = $('img_name_'+i).value;
            ima.setAttribute('src', 'user.img/'+nim);
            remClass()
            this.addClass('page2');
            Cufon.replace('.pages2 a', {fontFamily: 'AGLettericaCondensed', hover: true});
        });
   });
}

function remClass(){
	$each($$('.pages2 a'), function(element){
		element.removeClass('page2');
		
	});
	return true;
}

function showTab(url){
    var ob = $('over');
    var fla = $(url);
    ob.setStyles({
        'display': 'block', 
        'opacity': .8,
        height: window.getScrollSize().y
    });
    fla.setStyle('display', 'block');
    
    var w = $(window).getWidth();
    //w = w-400;
    w=900;
    fla.setStyles({'width': w+'px', 'height': 440, 'background': '#afd898'});
    
    var top = (($(window).getHeight() - fla.getHeight()) / 2) + window.getScroll().y;
    var left = ($(window).getWidth() - fla.getWidth()) / 2;
    
    fla.setStyles({
        top: top,
        left: left
    });
    
    ob.addEvent('click', function(){
        ob.setStyles({'display': 'none', 'opacity': 0});
        fla.setStyle('display', 'none');
    });
}
function closeBox(url){
    var ob = $('over');
    ob.setStyles({'display': 'none', 'opacity': 0});
    
    if(url=='all'){
    	var fla = $('tab0');
    	var fla1 = $('tab1');
    
	fla.setStyle('display', 'none');
    	fla1.setStyle('display', 'none');
    }else{
	var fla = $(url);
	fla.setStyle('display', 'none');
    }
    
    
}

function correctBrowser(){
   var ob = $('container');
   var ob1 = $('container2');
   var ob2 = $('container3');
   var ob3 = $('container4');
   var style = '0 0 0 1px';
   if(Browser.Engine.trident) style = '0 0 0 2px'; 
   if(Browser.Engine.gecko) style = '0 0 0 2px'; 
   if(Browser.Engine.webkit || Browser.Engine.presto) style = '0 0 0 3px'; 
   
   if ($('container')) if(style) ob.setStyle('padding',style); 
   if ($('container2')) if(style) ob1.setStyle('padding',style); 
   if ($('container3')) if(style) ob2.setStyle('padding',style); 
   if ($('container4')) if(style) ob3.setStyle('padding',style); 
    
}

function startMenu(){
   $each(menu_array, function(element, i){
        element.addEvent('mouseenter', function(){
             if(timeout) clearTimeout(timeout);
             hideAllS(i);
             
        });
        element.addEvent('mouseleave', function(){
             timeout = setTimeout('hideAllS();', 90);
             
        });
   });
   
   $each(menu_proj, function(element, i){ 
         element.addEvent('mouseenter', function(){
             if(timeout) clearTimeout(timeout);
             hideAllS(i);
         });
         element.addEvent('mouseleave', function(){
             timeout = setTimeout('hideAllS();', 40);
         });
             
   });
   
}
function hideAllS(act){
    $each(menu_array, function(element, i){
        if(menu_proj[i]) {
        if(act !== false && act==i) {
             menu_proj[i].setStyle('display', 'block');
            element.setStyle('background-color', '#2f6312');
        } else {
            menu_proj[i].setStyle('display', 'none');
            element.setStyle('background-color', '');
        } 
        }
    });
}


function startWeather(){
    $each($$('.foot_links a'), function(element, i){ 
         element.addEvent('mouseenter', function(){
            $('v'+i).setStyle('display', 'block');
            if(Browser.Engine.webkit) $('v'+i).setStyle('left', '615px');
            if(Browser.Engine.presto) $('v'+i).setStyle('left', '616px');
            if(Browser.Engine.trident5) $('v'+i).setStyle('left', '613px');
            if(Browser.Engine.trident4) $('v'+i).setStyles({'left': '613px', 'bottom': '159px'});
         });
         element.addEvent('mouseleave', function(){
            $('v'+i).setStyle('display', 'none');
         });
             
   });   
}

function hideAllW(act){
    $each($$('.foot_links a'), function(element, i){
        if(act !== false && act==i) {
            
            
        } else {
            
        } 
    });
}

function startPart(){
    $each($$('.partners img'), function(element, i){
        element.setStyle('opacity', .5);
        
        element.addEvent('mouseenter', function(){
            this.setStyle('opacity', 1);
        });
        element.addEvent('mouseleave', function(){
            this.setStyle('opacity', .5);
        });
    });
}

function startSlideComment(){
    myAccordion = new Accordion($$('.leave_comment'), $$('.comment_form'), {
            onActive: function(toggler, slider){
                if (Browser.Engine.trident) slider.setStyle('display', 'block');
                //slider.setStyle('padding-bottom', '20px');
            },
            onBackground: function(toggler, slider){
                if (Browser.Engine.trident) slider.setStyle('display', 'none');
            },
            onComplete: function(){
                
            },

            opacity: 0,
            display: false,
            alwaysHide: true
    });
}
