(function(){

	var wrap,
		tick = setInterval(function(){
			if (!document.getElementById('wrap')) return;
			clearInterval(tick);
			wrap = $('#wrap');
			updateWrapPos();
			$(window).resize(updateWrapPos);
		}, 1);

	function updateWrapPos() {
		wrap.css('marginTop', ($(document).height() - wrap.height()) / 2);
	}

})();

$(function(){

	// player
	swfobject.embedSWF(
		'player.swf',
		'player', 179, 33, '9.0.0', null, null, {wmode: 'transparent'}
		);


	// Img Corner
	if (!$.browser.msie && !$.browser.webkit) {
		var ROOT = '';

		$('link').each(function(){
			var p = this.href;
			if (p.indexOf('css/common.css') > -1) {
				ROOT = p.replace('css/common.css', '');
				return false;
			}
		});

		function setImgCorner() {
			var t = this;
			setTimeout(function(){
				if (t.height < 20) return;
				if (t.src.indexOf('0.gif') > -1) return;
				$(t).css({
					background: 'url('+t.src+')',
					width: $(t).width(),
					height: $(t).height()
				});
				t.src = ROOT+'img/0.gif';
			}, 0);
		}

		$('img.pic').each(setImgCorner).load(setImgCorner);
	}

	// body scroll
	(function(){
		var cntr = $('#body .scroll');
		cntrHeight = $('#body').height();
		$('#body').children().not('.scroll').each(function(){
			cntrHeight -= $(this).outerHeight(true);
		});
		cntr.height(cntrHeight)
			.jScrollPane({showArrows: true});
		
		if ($.browser.webkit) {
			setInterval(function(){
				cntr.data('jsp').reinitialise();
			}, 1000);
		}
		
	})();

	// side scroll
	(function(){

		var cntr = $('#side .scroll');

		if (!cntr.length) return;


		var visible = false,
			layer = cntr[0],
			dir = 0,
			tick,
			cntrHeight = $('#side').height();

		$('#side').children().not('.scroll').each(function(){
			cntrHeight -= $(this).outerHeight(true);
		});

		cntr.height(cntrHeight);

		$('<a id="side-scroll-up" class="scroll-arrow" href="#"/>').insertBefore(cntr);
		$('<a id="side-scroll-down" class="scroll-arrow" href="#"/>').insertAfter(cntr);

		$('#side').mousewheel(function(event, delta, deltaX, deltaY) {
			dir = -delta;
			scroll();
		});

		function scroll() {
			var s = layer.scrollTop;
			layer.scrollTop += dir * 20;
			up.removeClass('disabled');
			down.removeClass('disabled');
			if (layer.scrollTop == 0) {
				up.addClass('disabled');
				stop();
			}
			if ((s == layer.scrollTop && s > 0) || cntr.height() >= layer.scrollHeight) {
				down.addClass('disabled');
				stop();
			}
		}

		function start(d) {
			dir = d;
			scroll();
			tick = setInterval(scroll, 60);
		}

		function stop(d) {
			clearInterval(tick);
		}

		var up = $('#side-scroll-up')
			.mousedown(function(){ start(-1); })
			.mouseup(stop)
			.click(function(){return false;});

		var down = $('#side-scroll-down')
			.mousedown(function(){ start(1); })
			.mouseup(stop)
			.click(function(){return false;});

		function show() {
			if (visible) return;
			visible = true;
			cntr.height(cntrHeight - up.outerHeight(true) - down.outerHeight(true));
			up.css('display', 'block');
			down.css('display', 'block');
		}

		function hide() {
			if (!visible) return;
			visible = false;
			cntr.height(cntrHeight);
			up.hide();
			down.hide();
		}

		function check() {
			if (cntrHeight < layer.scrollHeight) show();
			else hide();
		}

		check();
		setInterval(check, 500);
	})();


	///
	$('#gallery-list .item').click(function(){
		$('#gallery-photo img').attr('src', $(this).attr('href'));
		return false;
	});

});
