function isOver(el, e) {
	el = $(el);
	var offset = el.offset();
	return (	offset.top  < e.pageY 
			 && offset.left < e.pageX 
			 && offset.top  + el.height() > e.pageY 
			 && offset.left + el.width()  > e.pageX);
}

var login = function(form) {
	form = this.form = $(form);
	this.slideDown = function(speed) {
		if (form.data === true) return;
		
		form.data = true;
		speed = speed instanceof Object ? 256 : speed;
		form.stop(true).animate( {top: 0}, speed, 'swing', function() { form.data = null; } ).find('input:submit').stop(true).animate( {opacity: 1}, speed );
	}
	this.slideUp   = function(speed) {
		if (form.data === false || form.find('input').is(':focus')) return;
		
		form.data = false;
		speed = speed instanceof Object ? 256 : speed;
		form.stop(true).animate( {top: - form.find('h6').position().top}, speed, 'swing', function() { form.data = null; } ).find('input:submit').stop(true).animate( {opacity: 0}, speed );
	}
	
	this.slideUp(0);
	this.form.live('mouseover', this.slideDown).live('mouseleave', this.slideUp);
}

var slideshow = function(elem) {
	elem = this.sshow = $(elem);
	this.beforeFn = function(currSlideElement, nextSlideElement, options, forwardFlag) {
		$(elem.find('.list').children().removeClass('current')[$(nextSlideElement).index()]).addClass('current');
	}
	this.showNav = function(event) {
		var e = $(this);
		if (e.data('phase') === true) return;
		
		e.data('phase', true);
		e.closest('.slideshow').find('.nav').stop()
			.css('filter', 'alpha(opacity=100)')
			.css('display', 'block')
			.animate( {opacity: 1}, 'fast', 'swing', function() { e.data('phase', null); } );
	}
	this.hideNav = function(event) {
		var e = $(this);
		if (e.data('phase') === false || isOver(e, event)) return;
		
		e.data('phase', true);
		e.closest('.slideshow').find('.nav').stop()
			.css('filter', 'alpha(opacity=0)')
			.animate( {opacity: 0}, 'fast', 'swing', function() { e.data('phase', null); } );
	}
	
	elem.find('.content').cycle({
		fx: 'scrollHorz',
		speed: 1850,
		slideResize: 0,
		easing: 'easeInOutCubic',
		next: elem.find('.nav.rarrow'),
		prev: elem.find('.nav.larrow'),
		pager: elem.find('.list'),
		pagerAnchorBuilder: function(index, DOMelement) { return '<a href="#"' + (index ? '' : 'class="current"') + '></a>'; },
		before: this.beforeFn
	});
	elem.find('.sshow-wrapper').bind('mouseover', this.showNav).bind('mouseleave', this.hideNav);
}

var positionSwitcher = function() {
	var switcher = this;
	this.position = 0;
	this.block = false;
	
	var button = this.button = $('<div></div>')
		.appendTo('body')
		.css({
			position: 'fixed',
			top: 0,
			left: 0,
			height: '100%',
			cursor: 'pointer',
			zIndex: 111
		})
		.bind('click', function() {
			switcher.block = true;
			$(window).scrollTop( $(window).scrollTop() == 0 ? switcher.position : 0 );
		});
	
	$(window)
		.bind('resize', function() {
			button.css('width', $('.cwrapper').offset().left + 'px');
		})
		.bind('scroll', function() {
			if (switcher.block) {
				switcher.block = false;
				return;
			}
			switcher.position = $(this).scrollTop();
		})
		.trigger('resize');
}

$(document).ready(function () {
	new login('#login');
	new slideshow('.slideshow');
	new positionSwitcher();
	
	heartBeat();
	
	$('a[href$=".jpg"], a[href$=".jpeg"], a[href$=".gif"], a[href$=".png"], a[href$=".bmp"]')
		.colorbox({transition: 'elastic', maxHeight : '95%', maxWidth : '95%'});
	$('a.colorbox')
	.colorbox({transition: 'elastic', maxHeight : '95%', maxWidth : '95%'});

});

function heartBeat() {
	var dot = $('.header .pulse span');
	dot.addClass('beat');
	setTimeout(function() { dot.removeClass('beat'); }, 140);
	setTimeout(heartBeat, Math.random() * 4000 + 1000);
}
