///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// LINK MOV
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

//IPAD
var target_ipad;


//EASING
jQuery.easing.quart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};




//ONLOAD
$(document).ready(function(){
	
	if(navigator.userAgent.search(/iPhone/) != -1 || navigator.userAgent.search(/iPod/) != -1 || navigator.userAgent.search(/iPad/) != -1){
		target_ipad = true;
	}else{
		target_ipad = false;
	}
	
	
	$("ul.making_list li a").click(function() {
		$("ul.making_list li a.sel").removeClass("sel");
		$(this).addClass("sel");
	//	$("#movie div.sel").hide();
	//	$("#movie div.sel").removeClass("sel");
		$($(this).attr("rel")).addClass("sel");
		$($(this).attr("rel")).show();
		return false;
	});
	
	
	setMovies('document_a',455,307,456,308);
});



//EMBED MOVIES
function setMovies(mname,flash_w,flash_h,video_w,video_h){
	if(target_ipad){
		setVIDEO(mname,video_w,video_h);
	}else{
		setFLASH(mname,flash_w,flash_h);
	}
}


function setFLASH(mname,mw,mh){
	var param = "?mname="+mname+"\&mw="+mw+"\&mh="+mh;
	swfobject.embedSWF('treehouse.swf'+param, 'movie', mw, mh, '9.0.0');
}



function setVIDEO(mname,mw,mh){

	var cont_node = document.getElementById('movie');
	for(var i=0; i<cont_node.childNodes.length; i++){
		var remove_node = cont_node.childNodes[0];
		var remove_src = remove_node.childNodes[0];
		remove_node.removeChild(remove_src);
		cont_node.removeChild(remove_node);
	}
	
	var video_node = document.createElement('video');
	video_node.width = mw;
	video_node.height = mh;
	video_node.poster = '';
	video_node.controls = true;
	video_node.autobuffer = true;
	video_node.autoplay = true;
	var source_node =  document.createElement('source');
	source_node.src = 'movie/'+mname+'.mp4';
	source_node.type = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
	video_node.appendChild(source_node);
	cont_node.appendChild(video_node);
	
	fakeClick(function() {
		video_node.play();
	});
}


//SCROLL TO TOP
function scrollAndSet(mname,flash_w,flash_h,video_w,video_h){
	setMovies(mname,flash_w,flash_h,video_w,video_h);
	$('html,body').animate({ scrollTop: 180 }, 1000, 'quart');
}



//AUTOPLAY
function fakeClick(fn) {
	var $a = jQuery('<a href="#" id="fakeClick"></a>');
		$a.bind("click", function(e) {
			e.preventDefault();
			fn();
		});
	jQuery("body").append($a);
	var evt,
		el = jQuery("#fakeClick").get(0);
	if (document.createEvent) {
		evt = document.createEvent("MouseEvents");
		if (evt.initMouseEvent) {
			evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
			el.dispatchEvent(evt);
		}
	}
	jQuery(el).remove();
}

