/**
 Common JavaScript
**/

$(document).ready(function() {
	$('#main div.photos ul').addClass('java')
	$('#main div.photos ul li div.image img').load(function() {
		var $this = $(this);
		var height = $this.height();
		if (height < 390) {
			var postop = (390 - height) / 2;
			$this.css('top', postop + 'px');
		}
	});
	
	if ($('#main div.photos ul li').length > 1) {
		$('#main div.photos ul').addClass('scrollable');
		$('#main div.photos ul').after('<span class="overlay"><span class="left-arrow"></span><span class="right-arrow"></span></span>');
		$('#main div.photos ul li div.image, #main div.photos span.overlay').hover(function() {
	    	$(this).parents('div.photos').addClass('photo-hover');
	    }, function () {
	    	$(this).parents('div.photos').removeClass('photo-hover');
	    });
	    $('#main div.photos span.overlay span').bind('click', photoClickHandler);

		if ($.browser.msie && $.browser.version == 6) {
			$('#main div.photos span.left-arrow').hover(function() {
		    	$(this).addClass('left-arrow-hover');
				$(this).css('border','solid 1px grey');
				$(this).css('border','none');
		    }, function () {
		    	$(this).removeClass('left-arrow-hover');
				$(this).css('border','solid 1px grey');
				$(this).css('border','none');
		    });
			$('#main div.photos span.right-arrow').hover(function() {
		    	$(this).addClass('right-arrow-hover');
				$(this).css('border','solid 1px grey');
				$(this).css('border','none');
		    }, function () {
		    	$(this).removeClass('right-arrow-hover');
				$(this).css('border','solid 1px grey');
				$(this).css('border','none');
			});
		}
	}
});

function photoClickHandler() {
	$('#main div.photos span.overlay span').unbind('click', photoClickHandler);
	if ($(this).hasClass('left-arrow')) {
    		$('#main div.photos ul li:last').prependTo('#main div.photos ul');
			$('#main div.photos ul').animate({
				left: '-=540'
			}, 0, function() {
			});
			$('#main div.photos ul').animate({
				left: '+=540'
			}, 500, function() {
				$('#main div.photos span.overlay span').bind('click', photoClickHandler);
			});
    	} else {
    		$('#main div.photos ul').animate({
				left: '-=540'
			}, 500, function() {
				$('#main div.photos ul li:first').appendTo('#main div.photos ul');
				$('#main div.photos ul').animate({
					left: '+=540'
				}, 0, function() {
					$('#main div.photos span.overlay span').bind('click', photoClickHandler);
				});
			});
    	}
}
