
/* Photo tape */
function initPhotoTape($pt, step) {
	var pt = $pt.get(0);
	if(!step) step=109;
	$pt
		.removeClass('no-js')
		.hoverScroll({
			fps: 60,
			step: step,
			minSpeed: 100,
			maxSpeed: 1000
		});
	$pt.parent().children('.forward')
		.hover(function() {
			$(this).addClass('hover');
			pt.hoverScroll.forward();
		}, function () {
			$(this).removeClass('hover');
			pt.hoverScroll.brake();
		});
	$pt.parent().children('.backward')
		.hover(function() {
			$(this).addClass('hover');
			pt.hoverScroll.backward();
		}, function () {
			$(this).removeClass('hover');
			pt.hoverScroll.brake();
		});
}
$(function() {
	initPhotoTape($('.phototape'));
});

/* Slide gallery */
function initSlideGallery($li, $gal) {
	var li = $li;
	var gal = $gal;
	
	if(!li.length || !gal.length) return;

	$('a', gal)
		.click(function() {

			if($(this).is('.active')) return false;
			$('.gallery a.active img')
				.animate({top: 0}, 500)
				.parent().removeClass('active');

			$('img', this)
			.animate({top: '-81px'}, 500)

			$(this).addClass('active');

			var href = $(this).attr('href');

			$('img', li)
				.animate({top: '375px'}, 500)
				.queue(function() {
					var tempimg = new Image();
					$(tempimg)
						.load(function() {
							$('.large-image img')
								.attr('src', this.src)
								.animate({top: 0}, 500)
						})
						.attr('src', href);
					$(this).dequeue();
				})
			
			return false;
		})
}

/* Star photo gallery */
$(function() {
	$('.l-star-gallery').click(function(e) {
		if(window.$sgf) return false;
		
		var $sgfw = $('<div class="star-gallery-frame-wrapper"><div class="star-gallery star-gallery-frame"></div></div>').prependTo('#main');
		var $sgf = $('.star-gallery-frame', $sgfw);
		window.$sgf = $sgf;
		
		$sgf.hide();
		$sgf.fadeIn('normal');
		
		$sgf.close = function() {
			window.$sgf.fadeOut('normal', function() {
				if(window.$sgf) {
					window.$sgf.parent().remove();
					window.$sgf = undefined;
				}
			});
		}
		
		$sgf.load($(this).attr('href'), {}, function() {
			initPhotoTape($('.phototape', this), 127);
			initSlideGallery($('.large-image', this), $('.gallery', this));
			$(this).click(function(e) {
				e = e || window.event;
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
			});
			$('.b-close').click(function(e) {
				if(window.$sgf) window.$sgf.close();
			});
		});
		
		return false;
	});
	
	$('body').click(function(e) {
		e = e || window.event;
		if(window.$sgf) window.$sgf.close();
	});
	
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
});

