var pageX = pageY = 0
var curImgEl;
$(document).ready(function() {
	$('#contact-me > img').click(function() {
		showContactForm();
	});
	$('#contact-me').click(function() {
		showContactForm();
	});
	$(document).mousemove(function(e) {
		pageX = e.pageX;
		pageY = e.pageY;
	});
	$('#main-text').hover(function() {$(this).toggleClass("hover")});
	$('#place-holder.gallery-text').mouseout(function() {
		if (pageX - 5 < curImgEl.offset().left ||
			pageX + 5 > curImgEl.offset().left + curImgEl.width() ||
			pageY - 5 < curImgEl.offset().top ||
			pageY + 5 > curImgEl.offset().top + curImgEl.height()
		) {
			$('#place-holder.gallery-text')
				.stop(true, true);
			$('#place-holder.gallery-text')
				.css('paddingTop', '0')
				.hide();
			$('#place-holder.gallery-text a span')
				.text('')
		}
		if (curImgEl.attr('alt') != $('#place-holder.gallery-text a span').text()) {
			$(this)
				.css('paddingTop', '0')
				.hide();
			$('#place-holder.gallery-text a span')
				.text('');
		}
	});
	$("#gallery-preview ul li img").hover(
		function() {
			curImgEl = t = $(this)
			$('#place-holder.gallery-text').stop(true, true)
			if ($('#place-holder.gallery-text a span').text() == this.alt) {
				return;
			}
			$('#place-holder.gallery-text a')
				.attr('href', $(this).parent().attr('href'))
			$('#place-holder.gallery-text a span')
				.text(this.alt)
			$('#place-holder.gallery-text')
				.css('top', (t.offset().top + t.height() + 1)+'px')
				.css('height', 0)
				.css('left', (t.offset().left)+'px')
				.animate({height: '42px', top: '-=42px'});
		},
		function() {
			curImgEl = t = $(this)
			if (pageX - 5 < t.offset().left ||
				pageX + 5 > t.offset().left + t.width() ||
				pageY - 5 < t.offset().top ||
				pageY + 5 > t.offset().top + t.height()
			) {
				$('#place-holder.gallery-text')
					.stop(true, true);
				$('#place-holder.gallery-text')
					.css('paddingTop', '0')
					.hide();
				$('#place-holder.gallery-text a span')
					.text('');
			}
		}
	);
	
	$('#overview li').hover(function(t) {
		$(this).css({opacity: 0.3});
		if ($.browser.msie) {
			$(this).addClass('msie');
		}
		$(this).animate({ 
			opacity: 1
			}, 400, function() {
				$(this).css('filter', '')
			});
		},
		function (t) {}
	);
	
	t = $("#gallery-cont ul");
	if (t.length) {
		t.jqGalScroll({height: 390, width: 540, speed:1000});
	}
	t = $("#left-menu ul.menu.sub li");
	if (t.length) {
		$('body').append($('<img id="preview-img" alt="" src="static/images/left-menu-preview1.png" width="30" height="22" />'));
		t.each(function(j, el) {
			$(el).hover(function() {
				e = $(this);
				if (e.children('a').attr('rel') != null) {
					$("#preview-img")
						.attr('src', '/images/icons/' + e.children('a').attr('rel'))
						.css({left : e.offset()['left'] + e.width() -5 , top : e.offset()['top'] + 1})
						.show()
				}
			}, function(j, el) {
				$("#preview-img").hide();
			});
		});
	}
});
function showContactForm() {
	addOverlay()
	$('#contact-form').css('left', $('#header-cont').position().left + 'px');
	$('#contact-form').show();
	$('#contact-form .close-btn').click(function() {
		hideContactForm();
	});
	$('#overlay').click(function() {
		hideContactForm();
	});
	$(document).keypress(function(e) {
		if (e.keyCode == '27') {
			hideContactForm();
		}
	});
}
function hideContactForm() {
	$('#contact-form').hide();
	removeOverlay();
	return false;
}
function addOverlay() {
    var w = $('body').width()
    var h = $('body').height()
    var o = $('#overlay')
    if (o.width() != w) {
        o.width(w).height(h)
    }
    o.css('display', 'block')
}
function removeOverlay() {
	$(document).unbind('keypress');
    $('#overlay').css('display', 'none')
}
