
function onResize() {
	if(mobile) return;
	if($.browser.msie && $.browser.version <= 8) return;
	var nativeWidth = 1602;
	var nativeHeight = 728;
	
	var wScale = nativeWidth/innerWidth;
	var hScale = nativeHeight/innerHeight;
	
	$('#container').css('-moz-transform', 'scale(' + Math.min(1/wScale,1/hScale) + ')');
	$('#container').css('-webkit-transform', 'scale(' + Math.min(1/wScale,1/hScale) + ')');
	$('#container').css('-o-transform', 'scale(' + Math.min(1/wScale,1/hScale) + ')');
}

function bob() {
	var now = (new Date())/400;
	return;

	$('#scrat').css('top', Math.floor(89 + 2*Math.sin(now)));
	$('#flagCanvas').css('top', Math.floor(110 + 2*Math.sin(now)));


	var acornpos = 400 + 0.5*Math.sin(now*2 + 2);
	
	$('#acorn').css('top', Math.floor(acornpos));
	
	$('#btn_gallery').css('top', Math.floor(544 + 2*Math.sin(now + 2)));
	$('#btn_video').css('top', Math.floor(548 + 2*Math.sin(now*1.1 + 2.1)));
	$('#social').css('top', Math.floor(590 + 2*Math.sin(now + 3)));
	
}

function videoMode() {
	if($('#main').hasClass('video')) return;
	
	if($('#main').hasClass('gallery')) { $('#main').removeClass(); $('#gallery').hide(); setTimeout(videoMode, 100); return; }

	$('#video, #smalllogo').css('display', 'block');
	
	
	if($.support.opacity) {
		$('#logo').fadeOut(400);
		setTimeout(function(){
			$('#smalllogo').animate({opacity:1},400);
			}, 300);
		setTimeout(function(){
			$('#main').addClass('video');
			}, 500);
		setTimeout(function(){
			$('#video').animate({opacity:1});
			}, 900);
			
		
		
		setTimeout(function() { $('#logo').hide() }, 500);
	}
	else {
		$('#main').addClass('video');
		$('#logo').hide();
	}
	
	playVideo(1);

	$('#smalllogo').click(unVideoMode);
	
}

function galleryMode() {
	if($('#main').hasClass('gallery')) return;
	
	if($('#main').hasClass('video')) { $('#main').removeClass(); $('#video').hide(); setTimeout(galleryMode, 100); return; }
	
	$('#gallery, #smalllogo').css('display', 'block');
	$('#gallery-image').attr('src', galSrc[galIndex]);
	
	if($.support.opacity) {
		$('#logo').fadeOut(400);
		setTimeout(function(){
			$('#smalllogo').animate({opacity:1},400);
			}, 300);
		setTimeout(function(){
			$('#main').addClass('gallery');
			}, 500);
		setTimeout(function(){
			$('#gallery').animate({opacity:1});
			}, 900);
			
		
		
		setTimeout(function() { $('#logo').hide() }, 500);
	}
	else {
		$('#main').addClass('gallery');
		$('#logo').hide();
	}


	$('#smalllogo').click(unVideoMode);
	
}

function playVideo(n) {
	if(n == 1) {
		//$('#player').html('');
		//return;
		if(mobile)
			$('#player').html('<iframe width="518" height="216" src="http://www.youtube.com/embed/Lh7OV9NbcC4" frameborder="0" style="position:absolute;top:42px;left:48px"></iframe>');
		else
			$('#player').html('<embed src="http://www.foxcontent.com/player-1.2.swf?wpr=031143&s=ice-age-continental-drift-scrat-s-continental-crack-up-2-us&ty=scrat-s-continental-crack-up-2&te=us&o=1&w=518&h=216&fl=0&ap=1" width=518 height=216 style="position:absolute;top:42px;left:48px" wmode=direct allowFullScreen=true></embed>');
		
	}
else if(n == 2) {
	if(mobile)
		$('#player').html('<iframe width="518" height="216" src="http://www.youtube.com/embed/zocutif0cQY" frameborder="0" style="position:absolute;top:42px;left:48px"></iframe>');
	else
		$('#player').html('<embed src="http://www.foxcontent.com/player.swf?id=ice-age-4-scrat-s-continental-crack-up-us&o=1&w=518&h=216&fl=0" width=518 height=216 style="position:absolute;top:42px;left:48px" wmode=direct allowFullScreen=true></embed>');
}
else {
	if(mobile)
		$('#player').html('<iframe width="518" height="216" src="http://www.youtube.com/embed/zocutif0cQY" frameborder="0" style="position:absolute;top:42px;left:48px"></iframe>');
	else
		$('#player').html('<embed src="http://www.foxcontent.com/player.swf?id=ice-age-4-scrat-s-continental-crack-up-us&o=0&w=518&h=216&fl=0" width=518 height=216 style="position:absolute;top:42px;left:48px" wmode=direct allowFullScreen=true></embed>');
}
}

function unVideoMode() {
	location.hash = '';

	
	
	if($.support.opacity) {
		$('#gallery, #video, #smalllogo').css('opacity', '0');
		$('#gallery, #video, #smalllogo').css('display', 'none');
		$('#player').html('');
		setTimeout(function(){
			$('#logo').fadeIn(400);
		}, 200);
		setTimeout(function(){
		$('#main').removeClass();
		}, 300);
	}
	else {
		$('#main').removeClass();
		$('#gallery, #video, #smalllogo').css('display', 'none');
		$('#player').html('');
		$('#logo').show();
	}

	
}

function galleryFS() {
	$('#fs-image').attr('src', fsSrc[fsIndex]);
	if($.support.opacity)
		$('#fullscreen').fadeIn();
	else
		$('#fullscreen').show();
}

var galIndex = 0;
var fsIndex = 0;
var galSrc = ['photos/photo_SM_1.png','photos/photo_SM_2.png'];
var fsSrc = ['photos/photo_LRG_1.jpg','photos/photo_LRG_2.jpg'];

function galleryNav(n) {
	$('#gallery-image').fadeOut(400, function() {
		galIndex += n;
		if(galIndex < 0) galIndex = fsSrc.length-1;
		if(galIndex >= fsSrc.length) galIndex = 0;
		
		$('#gallery-image').load(function(){$(this).fadeIn(400);});
		
		$('#gallery-image').attr('src', galSrc[galIndex]);
		
	});
}

function fsNav(n) {
	$('#fs-image').fadeOut(400, function() {
		fsIndex += n;
		if(fsIndex < 0) fsIndex = fsSrc.length-1;
		if(fsIndex >= fsSrc.length) fsIndex = 0;
		
		$('#fs-image').load(function(){$(this).fadeIn(400);});
		
		$('#fs-image').attr('src', fsSrc[fsIndex]);
		
	});
}

function galleryClose() {
	if($.support.opacity)
		$('#fullscreen').fadeOut();
	else
		$('#fullscreen').hide();
}

function init() {
	mobile = false;
	if( navigator.userAgent.match(/Android/i) ||
	 navigator.userAgent.match(/webOS/i) ||
	 navigator.userAgent.match(/iPhone/i) ||
	 navigator.userAgent.match(/iP[ao]d/i) 
	 ){
		mobile = true;
		$('html').css('width', '1280px');
		//$('html').css('overflow', 'auto');
	}
		

	//$(window).resize(onResize);
	//onResize();
	
	
	createFlag();
	createWater();
	
	setInterval(bob, 100);
	
	
	$('#social a').mouseenter(socialOver);
	$('#social a').mouseleave(socialOut);
	
	$('#fs-image').click(function(){fsNav(1)});
	$('#gallery-image').click(function(){galleryNav(1)});
	
	$.address.change(function(e){
		if(e.value == '/video') videoMode();
		if(e.value == '/gallery') galleryMode();
	});
	
}

function socialOver() {
	$(this.parentElement).addClass(this.id);
}
function socialOut() {
	$(this.parentElement).removeClass();
}

function createFlag() {
		if(1) {
			$('#flagCanvas').replaceWith('<img src=flag.png id=flagCanvas>');
			return;
		}
		
	var img = new Image;
	
	img.onload = function(){
		var flag = $('#flagCanvas')[0];

		var amp = 20;
	
		flag.width  = img.width;
	
		flag.height = img.height + amp*2;
	
		flag.getContext('2d').drawImage(img,0,amp,img.width,img.height);
	

		var timer = waveFlag( flag, img, img.width/10, amp );
		
	};
	
	img.src = 'flag.png';
}

function waveFlag( canvas, img, wavelength, amplitude, period, shading, squeeze ){

	if (!squeeze)    squeeze    = 0;

	if (!shading)    shading    = 100;

	if (!period)     period     = 400;

	if (!amplitude)  amplitude  = 10;

	if (!wavelength) wavelength = canvas.width/10;



	var fps = 30;

	var ctx = canvas.getContext('2d');
	

	var   w = canvas.width, h = canvas.height;

	//var  od = ctx.getImageData(0,0,w,h).data;
	
	

	// var ct = 0, st=new Date;

	return setInterval(function(){

		//var id = ctx.getImageData(0,0,w,h);

		//var  d = id.data;

		var now = (new Date)/period;

		var lastO=0,shade=0;
	
		ctx.clearRect(0,0,w,h);

		var sq = 0;
		
		
			for (var x=0;x<w;++x){

				var pct = x/w;

				var o   = Math.sin(x/wavelength-now)*amplitude*pct;

				var y2  = 0 + (o+sq*pct)<<0;


				shade = (o-lastO)*shading;


				lastO = o;
				

				ctx.drawImage(img, x, 0, 1, img.height-0, x, y2, 1, img.height-0);
				

			}



	},1000/fps);

}


function createWater() {
	if($.browser.msie && $.browser.version <= 8) return;

	var img = new Image;
	
	img.onload = function(){
		var w = $('#waterCanvas')[0];
		//w.width = img.width;
		//w.height = img.height;
		if(mobile) w.width = 1280;
		else w.width = 2560;
		w.height = 349;
		if(mobile) { w.style.marginLeft = "0";
		w.style.left = "0";
		}
		var timer = doWater( w, img );
	
	};
	
	//img.src = 'water.jpg';
	img.src = 'css/main_bg.jpg';
}

function deltas(a) {
	var lastItem = -1;
	var r = [0]
	for(var i=1;i<a.length;i++) {
		if(a[i] == undefined) a[i] = a[i-1]+1;
		r[i] = a[i] - a[i-1]; 
	}
	return r;
}
function rle(a) {
	var r = [{val:-1,len:-1}];
	
	for(var i=0;i<a.length;i++) {
		if(a[i] == undefined || a[i] == r[r.length-1].val)
			r[r.length-1].len++;
		else
			r[r.length] = {val:a[i],len:1}; 
	}
	return r;
	
}

function doWater( canvas, img ){
var    period     = 200;
var wavelength = canvas.width/10;



	var fps = 24;

	var ctx = canvas.getContext('2d');

	var   w = canvas.width, h = canvas.height;

	
	ctx.drawImage(img, (mobile ? 640 : 0), 379, w, h, 0, 0, w, h);

	var y2s = [];
	
	return setInterval(function(){

		var now = (new Date)/period;


			var lastO=0,shade=0;

		var sq = 0;
		
		var x0 = mobile ? 640 : 0;
		
		if(0) {
			var y = h;
			var o = h/center * (y + center*2) * Math.sin ((h/center)*(h - y)/(y + 1) + now) / h;
			canvas.style.height = Math.floor(380+o) + "px";
			return;
		}
			for (var y=0;y<h;y+=1){
			
				var amplitude = 2/*$('#wateramp input').val()*/ / 2;

				var center = 2/*$('#waterwl input').val()*/ * 6;
				
				var o = h/center * (y + center*2) * Math.sin ((h/center)*(h - y)/(y + 1) + now) / h;

				var y2 = y - Math.floor(o*0.0625*amplitude);
				
				if(y2<0) y2 = 0;
				else if(y2>h-2) y2 = h-2;
				
				y2s[y] = y2;
				
				if(y/h < 0.5) {
					//if(y2s[y] != y2)
					if(!mobile) ctx.drawImage(img, x0, y2+380, w, 1, 0, y, w, 1);
				}
				else {
					//if(y2s[y] != y2)
					if(!mobile) ctx.drawImage(img, x0, y2+380, w, 2, 0, y, w, 2);
					y++;
				}
				
				
			}
			
			if(mobile) {
				var segments = rle(deltas(y2s));
				var y = 0;
				var y2 = y2s[y] + segments[1].val*segments[1].len;
				for(var i=2;i<segments.length;i++) {
					var len = segments[i].len;
					if(i%3==0) ctx.drawImage(img, x0, y2+379, w, len, 0, y, w, len);
					y += len;
					y2 += len * segments[i].val;
				}
			}
			

	},1000/fps);

}


$(init);
