var ua = navigator.userAgent;
var ie = (function(){
	var undef,
		v = 3,
		div = document.createElement('div'),
		all = div.getElementsByTagName('i');
	while (
		div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', all[0]
	);
	return v > 4 ? v : undef;
}());


$(window).load(function(){
	$('#navibar').delay(500).animate({
		top: '50%'
	}, 700);
	$('#topbnr').delay(1000).animate({
		top: '50%'
	}, 500, 'easeOutBack');
	$('#content').delay(1000).animate({
		top: '50%'
	}, 700);
});

$(function(){

	var controller = (ie === 6) ? false : '#slidenum li a';
	var slideshow = (ie === 6) ? false : true;

	$('.bgmaximage').maxImage({
		isBackground: true,
		overflow: 'hidden',
		position: 'absolute',
		slideShow: slideshow,
		slideDelay: 6,
		slideShowTitle: false,
		controller: controller
//		,loadingGif: 'image/loading.gif',
//		loadingGifWidth: '48px',
//		loadingGifHeight: '48px'
	});

	var innerheight = ($(window).height() / 2) - 270;
	$('.scrollinner').css({height: innerheight});

	if (ua.indexOf('iPhone') > -1 || ua.indexOf('iPad') > -1 || ua.indexOf('iPod') > -1) {

		$('.scrollinner a').wrap(
			$('<div></div>').addClass('overanc').css({display: 'inline-block'})
		); 

		$('.overanc a').each(function(i){

			$(this).bind('touchstart', function(e){

				e.preventDefault();

				var $this = $(this);
				var anchor_url = $this.attr('href');

				window.open(anchor_url, '');
				return false;
			});
		});

		$('.scrollinner').overscroll();

		window.onorientationchange = function(){
			innerheight = ($(window).height() / 2) - 270;
			$('.scrollinner').css({height: innerheight});
		}

	} else {

		$('.scrollinner').jScrollPane();

		function resizeContainer(e) {
			var containerHeight = ($(window).height() / 2) - 270;
//			var maxHeight = $('.scrollinner').outerHeight();
//			var dragHeight = containerHeight * (containerHeight / maxHeight);
			(function(jq) {
				for(var i = 0; i < jq.length; i++) {
					jq[i].css({height: containerHeight});
				}
			})(new Array($('.jScrollPaneContainer')));
//			$('.jScrollPaneDrag').css({height: dragHeight});
		}
		$(window).bind('resize', resizeContainer);

//		$(window).resize(function(){
//			$('.scrollinner').jScrollPane();
//		});
	}

	$('a[href^="http"]').not('[href*="'+ location.hostname + '"]').click(function(){
		window.open(this.href, '');
		return false;
	});

	$('#panel1').biggerlink();

	$('#navi ul li a').click(function(){
		var href = $(this).attr('href');
		$('#topbnr').animate({top: '-10%'}, 1000);
		$('#navibar').animate({top: '-10%'}, 700);
		$('#content').animate({top: '100%'}, 700,function(){
			location.href = href;
		});
		return false;
	});
});

