function isiDevice(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
        (navigator.platform.indexOf("iPad") != -1)
    );
}



// configs
var facebooktabw = 120;
substabw = 0;
var screenw = $('#bg').width();
var screenh = $('#bg').height();
var	screenwh = screenw/screenh;



// Scale
function rescale(){
	substabw = $('#substab .label').width();
	screenw = $('#bg').width();
	screenh = $('#bg').height();
	screenwh = screenw/screenh;
	$('#nav').width(screenw-15*2-10*4-facebooktabw-substabw);
	$('#facebooktab').width(facebooktabw);

	if($('.type_gallery').length > 0){
		$('.type_gallery').css({'width':(screenw-180)+'px'});
	}
	
	if($('.type_mainpage').length > 0){
		$('#content').css({'margin-top':screenh-$('#content').height()-50+'px'});
		$('#content').css({'margin-right':screenw-$('#content').width()-15+'px'});
		$('#footer').css({'margin-right':screenw-$('#content').width()-15+'px'});
	}else{
		$('#content').height('auto');
		var contenth = $('#content').height();

		if(contenth < screenh-50){
			$('#content').height(screenh-50);
		}

		if(contenth > screenh-50){
			$('#content').height('auto');
		}	
	}
}


$(window).resize(function(){ rescale(); });



//WindowLoaded
$(window).load(function(){
	rescale();
});

// DocumentReady
$(document).ready(function(){
	
	rescale();

	setInterval(function () {
		rescale();
	}, 500);
	
	if(!isiDevice()){
		$('.ifixed').css({position:'fixed'});
	}
	
	$('#facebooktab .label').click(function(){
		if($('#facebooktab').css('bottom') == '0px') $('#facebooktab').animate({'bottom':'365px'},500);
		else  $('#facebooktab').animate({'bottom':'0'},500);
	});
	
	$('#substab .label').click(function(){
		if($('#substab').css('bottom') == '0px') $('#substab').animate({'bottom':$('#substab .list').height()},500);
		else  $('#substab').animate({'bottom':'0'},500);
	});
	
	$('.subnav').each(function(){
		$(this).css({marginRight: (($(this).parent().width() - $(this).width())/2)+'px'});
	});
	
	$('#nav .subs').hover(function(){
		if($('.subnav',this).length > 0){
			$('.subnav',this).stop(true, true).delay(100).animate({bottom: '55px',opacity: 1},300);
		}
	},function(){
		if($('.subnav',this).length > 0){
			$('.subnav',this).stop(true, true).delay(100).animate({bottom: '-'+$('.subnav',this).height()+'px',opacity: 0},300);
		}
	});
	
	$(".toggle").click(function(){
		$(".desc_long").toggleClass("closed");
		rescale();
	});
	
	$(".maintoggle").click(function(){
		$(".desc_long").toggleClass("closed");
		rescale();
	});
	
});

if(isiDevice()){
	
	var myScroll,
		hoverClassRegEx = new RegExp('(^|\\s)iScrollHover(\\s|$)'),
		removeClass = function () {
			if (this.hoverTarget) {
				clearTimeout(this.hoverTimeout);
				this.hoverTarget.className = this.hoverTarget.className.replace(hoverClassRegEx, '');
				this.target = null;
			}
		};

	function loaded() {
		myScroll = new iScroll('wrapper', {
			onBeforeScrollStart: function (e) {
				var target = e.target;

				clearTimeout(this.hoverTimeout);

				while (target.nodeType != 1) target = target.parentNode;

				this.hoverTimeout = setTimeout(function () {
					if (!hoverClassRegEx.test(target.className)) target.className = target.className ? target.className + ' iScrollHover' : 'iScrollHover';
				}, 80);

				this.hoverTarget = target;

				e.preventDefault();
			},
			onScrollMove: removeClass,
			onBeforeScrollEnd: removeClass
		});
	}

	document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
	document.addEventListener('DOMContentLoaded', loaded, false);
}
