var flScrollEnabled;
var scrollPixels;        
var bd_district;
var bd_offset;
var bd_maxOffset;
var previewWidth = 0;
var ajaxFormPostReturn = null;
var t;
var timer_is_on=0;

function lang (id) {		
		var curr;
	
	if ((document.location.hostname).search(/\.ru/) > 0)
		curr='ru';
	else
		curr='en';
	
	
	if(curr!=id)	
		if(id=="en")	
			window.location = 'http://www.odessarentaflat.com' + document.location.pathname;		
		else
			window.location = 'http://www.odessarentaflat.ru' + document.location.pathname;
			
			
			
}

/* thumbnail navigation functions */

function previewPrevious (delta) {

    margin = Number((document.getElementById("thumbnails_tape").style.marginLeft).replace('px',''));    
    
    setMargin( Math.min( (margin + delta), 0));

}

function previewNext (delta) {

    margin = Number((document.getElementById("thumbnails_tape").style.marginLeft).replace('px',''));
    
    setMargin ( Math.max( (margin - delta), previewWidth ));

}

function showPhoto (url, element) {
    
    document.getElementById("photo_container").innerHTML = '<img src="' + url + '" />';
    
    var list = document.getElementById('thumbnails_tape');
    var i = 0;

	while ( (list.getElementsByTagName('li')[i]!=undefined) || (list.getElementsByTagName('li')[i]!= null) ){
	    
	    list.getElementsByTagName('li')[i++].className = '';
	    
	}
    
    element.className = 'active';
    
}

function scrollBack () {
    
    
    if(flScrollEnabled == 1){
        previewPrevious(Number(scrollPixels/2));
        //scrollPixels++;
        setTimeout("scrollBack()", 10);        
    }
    
}

function scrollForward () {

    if(flScrollEnabled == 1){
        previewNext(Number(scrollPixels/2));
        //scrollPixels++;
        setTimeout("scrollForward()", 10);        
    }
    

}

function scrollStart (direction) {
    
    flScrollEnabled = 1;
    scrollPixels = 5;
    if (direction=='back')
        scrollBack();
        
    else if (direction=='fwd')
        scrollForward();

}

function scrollStop () {
    flScrollEnabled = 0;
}

function setMargin (margin) {
    document.getElementById("thumbnails_tape").style.marginLeft = margin.toString() + 'px';
}

/* END thumbnail navigation functions */

/* best deals navigation functions */

function bdShift (param) {


   switch (param){
   
    case 'prev':
    if (bd_offset==0)
        return false;
        
    bd_offset = Math.max( 0, bd_offset-3);    
    break;
  
    case 'next':    
    bd_offset += 3;
    
    if ( bd_maxOffset > 0 ){
       
       if ( bd_offset >= bd_maxOffset ){
            bd_offset -=3;
            return false;
       }
        
    }
    
    break;
   
       
    case 'specials':
    bd_offset = 0;
    bd_maxOffset = 0;
    bd_district = 0;    
    break;
   
    case 'centre':
    bd_offset = 0;
    bd_maxOffset = 0;
    bd_district = 1;
    break;
    
    case 'arcadia':
    bd_offset = 0;
    bd_maxOffset = 0;
    bd_district = 2;    
    break;
    
    default:
    return false;
    
   }

   bdSetActive ();
   
   var url      = "includes/scripts/ajax.php";
   var params   = "act=gbd&offset=" + bd_offset + "&district=" + bd_district;

   request.open("POST", url, true);	
    
   request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   request.onreadystatechange = updateBestDeals;
                    
   request.send(params);

   
}

function updateBestDeals () {
		 
	 if (request.readyState == 4){

       
		if (request.status == 200){
			         	
			 if(isNaN(request.responseText)){
				
				document.getElementById('bd_contents').innerHTML = request.responseText;
																	
			} 	
			else {
			    bd_maxOffset = parseInt(request.responseText);			    
			}
		} 
    }		
}

function bdSetActive () {
    
    var list = document.getElementById('bd_menu');

	for ( i = 0; i < list.getElementsByTagName('li').length ; i++ ){
	
	  list.getElementsByTagName('li')[i].className = ( bd_district == i )? 'active': '';
	
	}
   
}

/* END best deals functions */


/* feedback functions */

function feedbackSubmit (lang) {

    var errors = feedbackVerify();
    
    if ( errors.length == 0){
        
       //send AJAX request
       var url      = "includes/scripts/ajax.php";
       var params   = "act=fdk&lang=" + lang;
       
       for (var c = 0; c < document.forms[0].elements.length; ++c ){
       
           if ( !isEmpty(document.forms[0].elements[c].value) ){
               params +=  "&" + document.forms[0].elements[c].name + "=" + document.forms[0].elements[c].value;
           }
        
       }
       
       document.getElementById('ajax_loader').style.display='block';
       
       request.open("POST", url, true);	
        
       request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");   
       request.onreadystatechange = feedbackUpdate;                        
       request.send(params); 
        
    }
    else{
    
        while ( el = errors.pop()){
                    
            el.style.backgroundColor = "#FCC";
                  
        }
        return false;
    }

}

function feedbackUpdate () {
		 
	 if (request.readyState == 4){
       
		if (request.status == 200){
			 if (request.responseText != 0 ){
			    document.getElementById('feedback').innerHTML = request.responseText;		
			 }   
		} 
    }		
}

function feedbackVerify () {

    var errors = new Array();

    var userName = document.forms[0].name;
    var userMail = document.forms[0].mail;
    var userCity = document.forms[0].city;
    var userSubj = document.forms[0].subject;    
    var userMsg  = document.forms[0].message;    
    
    
    if ( isEmpty(userName.value) ){
        errors.push(userName);
    }
    else{
        userName.style.backgroundColor = "#FFF";  
    }

    if ( isEmpty(userMail.value) || !isEmail(userMail.value) ){
        errors.push(userMail);
    }else{
        userMail.style.backgroundColor = "#FFF";  
    }

    if ( isEmpty(userMsg.value) ){
        errors.push(userMsg);
    }else{
        userMsg.style.backgroundColor = "#FFF";  
    }    
    
    return errors;

}

/* END feedback functions */

/* reservation functions */
function showReservationFaq (id) {

    var list = document.getElementById('reservation_faq');
    
    for ( i = 0; i < list.getElementsByTagName('div').length ; i++ ){
	
	  list.getElementsByTagName('div')[i].className = 'faq';
	
	}
	
	document.getElementById( 'faq' + id ).className += ' active';


}

/* END reservation functions */

/* verification functions */

function isEmpty (val) {

    if (val == null || val == ''){
        return true;
    }

    return false;
    
}

function isEmail (mail) {    
    
    var atpos=mail.indexOf("@");
    var dotpos=mail.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=mail.length)
    {      
      return false;
    }
    
    return true;          
}

/* END verification functions */

/* google maps functions */

function initializeMap ( lat, lng, el_id, isORF ) {
    
    var ORFLat = 46.4817;
    var ORFLng = 30.7413;    
    
    var ORFpos = new google.maps.LatLng(ORFLat, ORFLng);
    var ORFimg = '../images/map_pointer.gif';    
    
    var latlng = new google.maps.LatLng(lat, lng);
   
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false,
        streetViewControl: false
    };
    
    var map    = new google.maps.Map(document.getElementById(el_id), myOptions);

    ORFmarker = new google.maps.Marker({
       
        map:map,
        draggable:false,
        animation: google.maps.Animation.DROP,
        position: ORFpos,
        icon: ORFimg,
        zIndex: 1
        
    });
    
    google.maps.event.addListener(ORFmarker, 'click', toggleBounceORF);
   
    
    if ( isORF == 0 ) {        
        
        marker = new google.maps.Marker({
            
            map:map,
            draggable:false,
            animation: google.maps.Animation.BOUNCE,
            position: latlng,
            zIndex: 2
            
        });
        
        infoWindow = new google.maps.InfoWindow({
            
            content: '',
            position: latlng,
            mazWidth: 10,
            zIndex: -1
        
        });
        
        //infoWindow.open(map, marker);  
        google.maps.event.addListener(marker, 'click', toggleBounce);  
    
    }else{
    
        ORFmarker.setAnimation(google.maps.Animation.BOUNCE);        
    }
    
    
    //show Deribasovskaya str.
    var deribasovskayaCoordinates = [   
        new google.maps.LatLng(46.483678, 30.745035),
        new google.maps.LatLng(46.483967, 30.74089),
        new google.maps.LatLng(46.484181, 30.73876),
        new google.maps.LatLng(46.484565, 30.73270) ];

    var deribasovskayaStr = new google.maps.Polyline({
        path: deribasovskayaCoordinates,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
      });

  deribasovskayaStr.setMap(map);
    
}
  
function toggleBounce () {

  if (marker.getAnimation() != null) {
    marker.setAnimation(null);
  } else {
    marker.setAnimation(google.maps.Animation.BOUNCE);
  }
}

function toggleBounceORF () {

  if (ORFmarker.getAnimation() != null) {
    ORFmarker.setAnimation(null);
  } else {
    ORFmarker.setAnimation(google.maps.Animation.BOUNCE);
  }
}



/* END google maps functions */


/* timer functions */
function timedCount(delay){	
	

	if ( ajaxFormPostReturn != null ){
				
		stopTimer();
		PayPalSubmit();
		
	} 
	else{
		
		t = setTimeout("timedCount("+delay+")", delay);
	}
	
}

function startTimer(delay){
	
	if (!timer_is_on){
		timer_is_on=1;
		timedCount(delay);
	}
}

function stopTimer(){
	
	clearTimeout(t);
	timer_is_on=0;
	
}

/* END timer functions */

window.onload = function() {

    if (window.location.pathname == '/' || window.location.pathname == '/en/' ){
        
        bd_district  = 0;
        bd_offset    = 0;
        bd_maxOffset = 0;
        
        bdSetActive();
        
    } else if(window.location.pathname == '/guestbook.html') {
    
        var RecaptchaOptions = {
            theme : 'custom',
            custom_theme_widget: 'recaptcha_widget'
        }
        
    } else if( (document.getElementById("thumbnails_tape") != 'undefined') && (document.getElementById("thumbnails_tape") != null ) ) {
            
        previewWidth -= document.getElementById("thumbnails_tape").clientWidth;
        previewWidth += document.getElementById("flat_previews").clientWidth;
    
    }

     
}

