document.observe("dom:loaded", function () {
	$$('.toggle').each(function (elm) {
		elm.observe('focus', function (event) {
			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;
            }
        });
    });

	$$('.do-hover').each(function(elm) {
		elm.observe('mouseover', function() {
			elm.addClassName('hover');
		}).observe('mouseout', function() {
			elm.removeClassName('hover');
		});
	});
	
	$$('#partners-list div#carousel ul li a').each( function(elm) {
		elm.observe('mouseover', function(event) {
			elm.down('img.no-color').hide();
		}).observe('mouseout', function(event) {
			elm.down('img.no-color').show();
		});
	});
});

Event.isEsc = function (event) {
	return event.keyCode == Event.KEY_ESC;
}

Element.addMethods({
	setWidth: function (element, width) {
		$(element).setStyle({width: width + 'px'});
		return Element.extend(element);
    },

	setHeight: function (element, height) {
		$(element).setStyle({height: height + 'px'});
		return Element.extend(element);
    },

	setTop: function (element, top) {
		$(element).setStyle({top: top + 'px'});
		return Element.extend(element);
    },

	getTop: function (element, top) {
		return parseInt($(element).getStyle('top'));
    },

	setLeft: function (element, left) {
		$(element).setStyle({left: left + 'px'});
		return Element.extend(element);
    },

	center: function (element) {
		element.setStyle({marginLeft: -(Math.round(element.getWidth()) / 2) + 'px'});
		return Element.extend(element);
    }
});

APP.getPageSize = function () {
	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;
}

APP.getPageScroll = function () {
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {
        yScroll = document.body.scrollTop;
    }

	return yScroll;
}

APP.userAgent = function () {
	var return_, UA;
	return_ = 'unknown';
	UA = navigator.userAgent;

	if ((/Konqueror|Safari|KHTML/.test(UA))) {
		return_ = 'KHTML';
    }
	if ((/Gecko/.test(UA)) && (return_ != 'KHTML')) {
		return_ = 'Gecko';
    }
	if ((/Opera/.test(UA))) {
		return_ = 'Opera';
    }
	if ((/MSIE/.test(UA)) && (return_ != 'Opera')) {
		return_ = 'MSIE';
    }
	if ((return_ == 'MSIE') && !((/MSIE 6\./.test(UA)) && (return_ != 'Opera'))) {
		return_ = 'MSIE7';
    }
	return return_;
}

APP.keypress = function (event) {
	if (Event.isEsc(event)) {
		event.stop();
		$('overlay').hide();
		$('modal-box').hide();
		$('modal-info').down('tbody').descendants().invoke('remove');
	}
}

APP.populated = false;
function openLightbox(id_county) {
	var select_judet = $('select-judet');
	var select_oras = $('select-oras');
	$('modal-info').down('tbody').insert('<tr class="error"><td><p>' + APP.locationErrors.noCity  + '</p></td></tr>');

	if (!APP.populated) {
		APP.counties.each(function(county) {
			select_judet.insert('<option value="' + county.id + '">' + county.name + '</option>');
		});

		APP.populated = true;
	}

	select_judet.selectedIndex = select_judet.select('option').indexOf(select_judet.select('option').find(function (option) { return option.value == id_county; }));

	select_oras.descendants().invoke('remove');
	select_oras.insert('<option value="">' + APP.locationLabels.city + '</option>');

	APP.cities.findAll(function(city) { return city.id_county == id_county; }).each(function(city) {
        var location = APP.locations.find(function (location) {
            return location.id_city == city.id;
        });
        if (location) {
            select_oras.insert('<option value="' + city.id + '">' + city.name + '</option>');
        }
	});

	select_oras.selectedIndex = 0;

	this.page = {size: APP.getPageSize(), scroll: APP.getPageScroll()};

	$('overlay').setHeight(this.page.size.page_height).appear({duration: 0.1, to: 0.5}).observe('click', function (event) {
		$('overlay').hide();
		$('modal-box').hide();
		$('modal-info').down('tbody').descendants().invoke('remove');
	});

	$('modal-box').center().setTop(this.page.scroll + 117).appear({duration: 0.1}).down('.modal-close a').observe('click', function (event) {
		event.stop();
		$('overlay').hide();
		$('modal-box').hide();
		$('modal-info').down('tbody').descendants().invoke('remove');
	});
}


function LightboxDelegate(url,caption,rel) {

    myLightWindow.activateWindow({
        href: url,
        title: caption,
        rel: 'project_['+rel+']',
        caption: caption
    });
}