// JavaScript Document
var wwwbase = '';
var MAG = Object.extend({}, MAG || {});

Event.observe(window, 'load', function() {

});


Event.observe(window, 'load', function() {
	
	if ( $('send_to_friend_button') ) {
		Event.observe('send_to_friend_button', 'click', function() {
			toggle_form('send_to_friend');
			Event.observe('fog', 'click', function(e) {
				Event.stop(e)
			})						
		});
	}
	if ( $('send_to_friend_button2') ) {
		Event.observe('send_to_friend_button2', 'click', function() {
			toggle_form('send_to_friend');
			Event.observe('fog', 'click', function(e) {
				Event.stop(e)
			})			
		});
	}	
	
	if ( $('ask_the_question') ) {		
		Event.observe('ask_the_question', 'click', function() {
			show_question('send_question');
			Event.observe('fog', 'click', function(e) {
				Event.stop(e)
			})
		});
	}
	if ( $('ask_the_question2') ) {
		Event.observe('ask_the_question2', 'click', function() {
			show_question('send_question');
			Event.observe('fog', 'click', function(e) {
				Event.stop(e)
			})			
		});		
	}	
	
	if ( $('fog') ) {
	
		Event.observe(document, 'keypress', function(e){
			if(Event.KEY_ESC == e.keyCode) {
				if ($('send_to_friend').style.display != "none") {
					toggle_form('send_to_friend');
				} else if ( $('send_question').style.display != "none" ) {
					show_question('send_question');		
				}				
			}
		});	
	
		$('fog').style.width = ( navigator.userAgent.indexOf ( "MSIE" ) == -1 ? document.documentElement.scrollWidth : document.body.clientWidth ) + "px";
		$('fog').style.height = ( navigator.userAgent.indexOf ( "MSIE" ) == -1 ? document.documentElement.scrollHeight : document.body.clientHeight ) + "px" ;
		Event.observe('fog', 'click', function() {
			if ($('send_to_friend').style.display != "none") {
				toggle_form('send_to_friend');
			} else if ( $('send_question').style.display != "none" ) {
				show_question('send_question');		
			}
		});
	}
	
	if ( $('submitbutton') ) {
		Event.observe('submitbutton', 'click', function() {
			validation();
		});
	}
	
	if ( $('submitbutton2') ) {
		Event.observe('submitbutton2', 'click', function() {
			validation2();
		});
	}
	
	var scripttag = document.getElementsByTagName('script')[0];
	wwwbase = scripttag.src.match(/(.*)public\/js.*/)[1];
});


MAG.openWindow = function(theURL,winName,features) {
	// get the file id from the url
	try {
		var projectid = theURL.match(/\/Estate([\d]+)_([\d]+)_large_thumb\.jpg/)[1];
		var fileid = theURL.match(/\/Estate([\d]+)_([\d]+)_large_thumb\.jpg/)[2];
		var url = wwwbase + 'offers/imagepopup/' +  projectid + '?file_id=' + fileid;
	} 
	catch (oError) {
		var projectid = theURL.match(/\/Project([\d]+)_([\d]+)_large_thumb\.jpg/)[1];
		var fileid = theURL.match(/\/Project([\d]+)_([\d]+)_large_thumb\.jpg/)[2];
		var url = wwwbase + 'offers/imagepopup/' +  projectid + '?file_id=' + fileid + '&estate=1';
	}
	
	
	var popupWindow = window.open(url,winName,features);
	popupWindow.focus();
}

// Calculator

function show_calculator(el) {	
	if ( $(el).style.display == "none" ) {
		var Scroll = getScroll();
		var Size = getSize();
		var styletop = ( Size[1] / 2 ) + Scroll[1] ;
		$(el).style.top = styletop + "px";
		
		Effect.Appear($(el), { duration: 0.5 });
		$('fog').toggle();
		return;
	} else {
		Effect.Fade($(el), { duration: 0.5 });
		$('fog').toggle();
		return;
	}

}

function calculate(result_text){
	y = 1*$('period').value;
	l = $('interest').value/100;
	var loan = 1*parseInt($('loan').value);

	months = y*12;
	monthlyInterest = l/12;

	discontFactor = 0;
	
	for (j=1; j<=months; j++)
		discontFactor += 1/Math.pow((1+monthlyInterest),j);
	
	monthlyPayment = loan / discontFactor;
	vnoska = monthlyPayment.toFixed(2);
	
	if(isNaN(vnoska))
		vnoska = 0;
		
	$('result').innerHTML = result_text + ': ' + vnoska + ' &euro;';
	return ;
}


// Send to friend

function toggle_form( el ) {
	
	if ( $(el).style.display == "none" ) {
	
		var Scroll = getScroll();
		var Size = getSize();
		var styletop = ( Size[1] / 2 ) + Scroll[1] ;
		$(el).style.top = styletop + "px";
		
		Effect.Appear($(el), { duration: 0.5 });
		$('fog').toggle();
		return;
	} else {
		
		Effect.Fade($(el), { duration: 0.5 });
		$('fog').toggle();
		return;
	}
}

function validation() {
	var error = 0;
	$('sname', 'rname', 'descript').each(function(el) { 
		if ( el.value == "" ) 
			{ el.style.border = "1px solid #FF9797"; error = 1; }
			else 
			{ el.style.border = "1px solid #66809E"; } 
	});
	
	if ( $('rmail').value != "" ) {
		var regex = /^[a-zA-Z][\w\.-_]*[a-zA-Z0-9_]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if ( $F('rmail').match(regex) ) {
			$('rmail').style.border = "1px solid #66809E";	
		} else {
			error = 1;
			$('rmail').style.border = "1px solid #FF9797";
		}
	} else {
		error = 1;
		$('rmail').style.border = "1px solid #FF9797";
	}
	
	if (error) return;
	
	$('submitbutton').toggle_form('send_to_friend');
	$('status').toggle_form('send_to_friend');
	var url = wwwbase + 'offers/sendtofriend';
	var pars = 'from=' + $F('sname') + '&to=' + $F('rname') + '&tomail=' + $F('rmail') + '&url=' + $F('url') + '&descript=' + $F('descript');
	var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: sendtofriendcomplete() } );
}

function sendtofriendcomplete() {	
	$('status').toggle_form('send_to_friend');
	$('ready').toggle_form('send_to_friend');
}

// Send question
function validation2() {
	var error = 0;
	$('usernames', 'phone', 'question').each(function(el) { 
		if ( el.value == "" ) 
			{ el.style.border = "1px solid #FF9797"; error = 1; }
			else 
			{ el.style.border = "1px solid #66809E"; } 
	});
	
	
	if ( $('question_email').value != "" ) {
		var regex = /^[a-zA-Z][\w\.-_]*[a-zA-Z0-9_]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if ( $F('question_email').match(regex) ) {
			$('question_email').style.border = "1px solid #66809E";	
		} else {
			error = 1;
			$('question_email').style.border = "1px solid #FF9797";
		}
	} else {
		error = 1;
		$('question_email').style.border = "1px solid #FF9797";
	}
	
	
	if (error) return;
	
	$('submitbutton2').toggle_form('send_question');
	$('status_question').toggle_form('send_question');
	var url = wwwbase + 'offers/comment';
	var pars = 'username=' + $F('usernames') + '&email=' + $F('question_email') + '&phone=' + $F('phone') + '&question=' + $F('question') + '&is_offer=' + $F('is_offer') + '&offer=' + $F('offer');
	var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: sendquestioncomplete() } );
}

function sendquestioncomplete() {
	$('status_question').toggle();
	$('ready_question').toggle();
}

function show_question( el ) {
		
	if ( $(el).style.display == "none" ) {
		
		var Scroll = getScroll();
		var Size = getSize();
		var styletop = ( Size[1] / 2 ) + Scroll[1] ;
		$(el).style.top = styletop + "px";
		
		Effect.Appear($(el), { duration: 0.3 });
		$('fog').toggle();
		return;
	} else {
		Effect.Fade($(el), { duration: 0.3 });
		$('fog').toggle();
		return;
	}
}




function getScroll() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

MAG.openEstateWindow = function(image, event)
{
	Event.stop(event);
	
	var width = 642;
	var height = 700;
	var left = (screen.width - width)/2;
	var top = (screen.height - height)/2;

	// get the file id from the url
	var fileid = image.src.match(/\/([\d]+)_medium_thumb\.jpg/)[1];
	var url = wwwbase + 'offers/imagepopupestate/' + fileid;
	
	var popupWindow = window.open(url, 'Preview', 'top='+top+',left='+left+',width='+width+',height='+height+',toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
	popupWindow.focus();
}


function print_selected(location){
	if($$('.print_this')){
		var ids = '';
		$$('.print_this').each(function(elem){
			if(elem.checked){
				ids = ids + elem.value + ',';
			}
		});
		if(ids.length > 1){
			ids = ids.substr(0,(ids.length-1));
		}
		window.location = wwwbase + 'ru/' + location + '/print_offers/'+ids;
	}
}


function mark_all(){
	if($$('.print_this')){
		$$('.print_this').each(function(elem){
			elem.checked  = true;
		});
	}
}

