function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

	var windowWidth, windowHeight;

	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

	objPageSize = {page_width: pageWidth, page_height: pageHeight, window_width: windowWidth, window_height: windowHeight};

	return objPageSize;
}

function validate_fields(fields, event, path) {
	fields.each(function(field) {
		if($F(field).blank()) {
			event.stop();
			$(field).up(path).addClassName('error');
		} else if($F(field) == $(field).title) {
			event.stop();
			$(field).up(path).addClassName('error');
		} else {
			$(field).up(path).removeClassName('error');
			return true;
		}
	});
}

function validate_email(fields, event, path) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	fields.each(function(field) {
		if($F(field).blank()) {
			event.stop();
			$(field).up(path).addClassName('error');
		} else if(!filter.test($F(field))) {
			event.stop();
			$(field).up(path).addClassName('error');
		} else {
			$(field).up(path).removeClassName('error');
			return true;
		}
	});
}

function genMap(elm) {
  if (GBrowserIsCompatible()) {
	var map = new GMap2($(elm));
	var latlng = new GLatLng(44.464401, 26.090357);
	//var blueIcon = new GIcon(G_DEFAULT_ICON);
	//blueIcon.image		= "http://www.tecadra.ro/favicon.gif";
	//blueIcon.shadow		= "";
	//blueIcon.iconSize	= GSize(16, 16);
		//markerOptions = { icon:blueIcon };
	
	map.setMapType(G_MAPMAKER_NORMAL_MAP);
	map.setCenter(latlng, 17);
	map.setUIToDefault();
	map.addOverlay(new GMarker(latlng));
  }
}

function doScroll (event) {
	var page			= getPageSize();
	var scroll			= document.viewport.getScrollOffsets();
	var currentScroll	= parseInt($('content-sidebar').getStyle('top'));

	var val = eval(scroll.top + 50);
	val = Math.max(val, 560);
	val = Math.min(val, page.page_height - $('content-sidebar').getHeight() - 220);
	currentScroll += (val - currentScroll) / 8;
	val = currentScroll;

	$('content-sidebar').setStyle({'top':  val + 'px'});
}

function isScrolled(elm) {
	var scroll			= document.viewport.getScrollOffsets();

	var view_top		= scroll.top;
    var view_bottom		= scroll.top;

	var elm_top		= elm.positionedOffset().top - 200;
	var elm_bottom	= elm_top + elm.getHeight();

	return ((elm_bottom >= view_top) && (elm_top <= view_bottom));
}

document.observe('dom:loaded', function() {
	$$('.toggle').each(function (elm) {
		elm.observe('focus', function (event) {
			elm.up('div').removeClassName('error');
			if (elm.value == elm.title) {
				elm.up('div').addClassName('focus');
				elm.value = '';
			}
		}).observe('blur', function (event) {
			if (elm.value == '') {
				elm.up('div').removeClassName('focus');
				elm.value = elm.title;
			}
		});
	});

	$$('a.back-top').each(function(elm) {
		elm.observe('click', function(event) {
			event.stop();
			Effect.ScrollTo(elm.rel);
		});
	});

	$$('.do-hover').each(function(elm) {
		elm.observe('mouseover', function() {
			elm.addClassName('hover');
		}).observe('mouseout', function() {
			elm.removeClassName('hover');
		});
	});

	$('content-main').setStyle({'minHeight': eval($('content-sidebar').getHeight() + 10) + 'px'});
	setInterval("doScroll()", 25);
	document.observe('scroll', function() {
		$$('.prices-box').each(function(elm){
			if(isScrolled(elm)) { 
				$$('a[rel=' + elm.id + ']').each(function(link) {
					$$('#content-sidebar ul.navigation li a').invoke('removeClassName', 'selected');
					link.addClassName('selected');
				});
			} else {
				
			}
		});
	});
	document.documentElement.onscroll = doScroll;
});