function CloseButton(dark){
	var src = 'png/close.png';
	this.a = document.createElement('a');
	this.a.setAttribute("href","#");
	this.a.setAttribute('class','closeButton');
	this.img = new Image();
	this.width = 0;
	this.height = 0;

	this.img.object = this;
	$(this.a).click(function(e){
		$.address.value('/'+mt11.projectAr[mt11.activeProject].id);
		e.preventDefault();
	});

	$(this.img).load(function(){
		this.object.width = this.width;
		this.object.height = this.height;
		if(mt11.detail) mt11.detail.resize();
	}
	).attr('src',src);
	$(this.a).append(this.img);

	this.fadeIn = function(){
		$(this.a).fadeIn();
	}

	this.fadeOut = function(){
		$(this.a).fadeOut();
	}

	this.resize = function(){
		$(this.a).css('left', mt11.windowWidth + 10 - this.width);
		$(this.a).css('top',-10);
	}

}
function Detail(div){
	this.div = div;
	this.active = false;
	this.project;
	this.con = $(this.div).children('#detailContent');
	this.back = $(this.div).children('#back');
	this.bg = $(this.div).children('#detailBg');
	this.about = new DetailAbout($('#detailAbout'));
	this.yt;
	this.photos = "";

	var _ = this;


	this.hideAll = function(){
		if(_.photos) _.photos.hide();
		if(_.about) _.about.hide();
		if(_.yt) _.yt.hide();
	}

	$(this.div).hide();
	$(this.bg).hide();
	$(this.bg).click(function(e){
		$.address.value('/'+mt11.projectAr[mt11.activeProject].id);
		e.preventDefault();
	});

	this.showDefault = function(){
		var section = "about";
		if(this.project.yt) section = "video";
		else if(this.project.imageCount) section = "photos";

		$.address.value('/'+mt11.projectAr[activeProject].id+'/'+section);
	}

	this.show = function(p){
		if(this.active) return;
		this.active = true;
	 	$(this.div).show();
	}

	this.showAbout = function(){
		this.hideAll();
		this.show();

		_.about.load(this.project.about.text());
		_.about.show();
		this.showBg();
	}

	this.showVideo = function(){
		this.hideAll();
		this.show();
		if(_.yt && _.yt.project != _.project){
			$(_.yt).remove();
			_.yt = null;
		}
		if(!_.yt){
			_.yt = new Youtube(_.project.yt,_.project.ytw,_.project.yth);
			$(_.con).append(_.yt.div);
		}

		setTimeout(function(){_.yt.show();},500);
		this.showBg();
	}

	this.showBg = function(){
		$(_.bg).fadeTo(500,.7);
	}

	this.showPhotos = function(){
		this.hideAll();
		this.show();

		if(_.photos && _.photos.project != _.project){
			$(_.photos).remove();
			_.photos = "";
		}
		if(!_.photos){
			_.photos = new ProjectPhotos(_.project);
			$(_.con).append(_.photos.div);
		} else {
			_.photos.show();
		}
		this.showBg();
	}


	this.hide = function(){
		this.hideAll();
		if(!this.active) return;
		$(this.bg).fadeOut('slow');
		$(this.nav).fadeOut();
		this.cleanup();
		this.active = false;
	}

	this.cleanup = function(){
		if(this.yt){
			$(this.yt.div).remove();
			this.yt = null;
		}

		if(this.photos){
			$(this.photos.div).remove();
			this.photos = null;
		}
	}

	this.resize = function(){
		$(this.bg).css('width',mt11.windowWidth);
		$(this.bg).css('left',-mt11.windowWidth/2);
		$(this.bg).css('height',mt11.windowHeight);
		if(this.yt) this.yt.resize();
		if(this.photos) this.photos.resize();
		if(this.about) this.about.resize();
		$(this.nav).css('top',mt11.windowHeight-110);
	}


}
function DetailAbout(div){
	var _self = this;
	this.div = div;
	this.text = $(div).find(".textbox");
	this.p = $(div).find('p:first');
	this.height = 0;
	this.isHidden = true;
	this.close = new CloseButton();
	this.width = $(this.text).width();
	$(this.div).hide();
	$(this.div).append(this.close.a);


	this.load = function(val){
		$(_self.p).html(val);
		_self.height = $(_self.p).height();
		_self.resize();
	}

	this.show = function(){
		if(!this.isHidden) return;
		this.isHidden = false;
		setTimeout(function(){
			$(_self.div).fadeIn('slow');
			_self.resize();

		},500);
	}

	this.hide = function(){
		if(this.isHidden) return;
		this.isHidden = true;
		$(this.div).hide();
	}

	this.resize = function(){
		this.close.resize();
		this.height = this.p.height();

		$(this.div).css('top', ( mt11.windowHeight - this.height) / 2);
	}
}
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

var mt11 = new MT11();

function MT11(){
	var _ = this;
	var mouseX = 0;
	_.windowWidth = 0;
	_.projectAr = [];
	_.projectByName = new Array();
	_.windowHeight = 600;
	_.projectSpacing = 100;
	var scrollThreshold = 0;
	var scrollTick = 0;
	var mouseDown = false;
	var iscroll;
	_.template = new Object();
	_.iOS = false;
	var iPhone = false;
	_.projectWidth = 370;
	_.activeProject = 0;
	var isWheelDisabled = false;
	var browserTitle = "Mike Tucker: ";
	var browserSubtitle = "Interaction Creator";
	var twitter;

	_.detail;
	var nav;
	_.slider;
	_.scroller;
	_.ua = $.browser;




	_.start = function(){
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			_.iOS = true;
		}
		console.log('boo');
		if($.browser.msie || _.iOS){
			console.log('ok');
			window.location = "simple.php";
			return;
		}


		$.get('template.html', function(data){
			_.template.project = $(data).children('.projectItem');
			_.template.scroller = $(data).children('#scrollContainer');
			_.prepare();
		});

	}


	_.prepare = function(){
		_.slider = new Slider(document.getElementById('projectSlider'),document.getElementById('projectContainer'));
		_.scroller = new Scroller(_.template.scroller.clone());
		_.detail = new Detail(document.getElementById('detail'));
		_.loadProjects();
		$(window).resize(_.windowResize);
		$('#projectContainer').scroll(_.windowScroll);
		_.windowResize();

		if(_.iOS){ // IPHONE / IPAD //////////////////////////////////////////////////
			iscroll = new iScroll('projectContainer',{snap:true,bounceLock:true,hScrollbar:true,vEnabled:false});
			_.scroller.div.hide();
		} else { // DESKTOP //////////////////////////////////////////////////////

			$(window).mousemove(function(e){mouseX = e.pageX;});

			$(document).mousewheel(function(event, delta) {
				if(isWheelDisabled) return;
				isWheelDisabled = true;
				setTimeout(function(){isWheelDisabled = false;},60);
				if(delta < 0) d = 1;
				else d = -1;
			    var z = _.activeProject + d;
				if(z < 0) z = 0;
				else if(z >= _.projectAr.length) z = _.projectAr.length - 1;

				$.address.value('/'+_.projectAr[z].id);
			});


			$(document).keydown(function(e) {
				var n = _.activeProject;
				switch(e.keyCode){
					case 38:
					case 37:
						n--;
						break;
					case 40:
					case 39:
						n++;
						break;
				}
				if(n < 0) n = _.projectAr.length -1;
				else if(n > _.projectAr.length -1) n = 0;
				$.address.value('/'+_.projectAr[n].id);
			});

			setInterval(_.slideUpdate,1000/50);

		}

	}


	_.init = function(){


		$.address.change(function(event) {
			_.navigate(event);
		}).history(true);


		if ($.address.value() == '' || $.address.value() == '/') {
		    $.address.value(_.projectAr[_.activeProject].id);
		}

		_.slider.instant = true;
		_.navigate();
		_.slider.instant = false;

		_.windowResize();
		_.initPaper = true;

	}

	_.navigate = function(event){
		var p;
		if(!event){
			p = $.address.value();
		} else {
			p = event.value;
		}

		var split = p.split('/');
		_.slider.gotoProjectByName(split[1]);

		var t = browserTitle;

			t += _.projectAr[_.activeProject].title;

			switch(split[2]){
				case "photos":
					t += " | Photos";
					_.detail.showPhotos();
					break;
				case "about":
					t += " | About";
					_.detail.showAbout();
					break;
				case "video":
					t += " | Video";
					_.detail.showVideo();
					break;
				default:
					_.slider.show();
					_.detail.hide();
					break;
			}

		if(_.projectAr[_.activeProject].id == 'mike')
			t = browserTitle + browserSubtitle;
		$.address.title( t );
	}

	_.slideUpdate = function(){
		_.slider.update();
		_.scroller.update();
		var p = Math.abs(Math.cos(_.slider.percent()*Math.PI+1));
		$('#scrollDrag').css('background-color',_.hsv2rgb(p,.2,.6	) );
		$('#projectYear').css('color',_.hsv2rgb(p,.3,.6) );


	}


	_.windowScroll = function(e){
		if(!mouseDown) return;
		isSliding = true;
	}

	_.isPortrait = function(){
		switch($(page).width()){
			case "768":
				return true;
				break;
			case "1024":
				return false;
				break;
		}
		return undefined;
	}

	_.windowResize = function(){
		_.windowWidth = $(window).width();

		if(_.iOS){
			_.windowWidth = $('#page').width();
			_.windowHeight = $('#page').height();
		} else if(window.navigator.userAgent.indexOf("MSIE")>0){
	        _.windowHeight=window.innerHeight;

	    } else {
			_.windowHeight = $(window).height();
			if(_.ua.mozilla){
				_.windowHeight = window.innerHeight;
			}
		}


		if(_.iOS){
			if(isPortrait){
				$("#projectContainer").css('width', sliderWidth + _.projectWidth/2 + 384);
			} else {
				$("#projectContainer").css('width', sliderWidth + _.projectWidth/2 + 512);
			}
		}

		scrollThreshold = _.windowWidth * .2;
		for(var i = 0; i < _.projectAr.length; i++){
			_.projectAr[i].resize();
		}


		_.slider.resize();
		_.scroller.resize();
		_.detail.resize();
	}

	_.loadProjects = function(){
		$.ajax({
			type: 'GET',
			url: 'data.xml',
			dataType: 'xml',
			success: _.parseProjects
		});
	}

	_.parseProjects = function(xml){

		$(xml).find("project").each(function(){
			new Project(this);
		});

		twitter = new Twitter(_.projectAr[0],$(xml).find('hello').text());

		_.slider.showLabel(0);
		_.init();
	}

	_.sliderWidth = 0;

	_.getBGColor = function(b){

		return  _.hsv2rgb(Math.abs(Math.cos(_.slider.percent()*Math.PI+1)),.15,(b) ? b : .9);
	}


	_.hsv2rgb = function (h,s,v) {
		var r, g, b;
		var var_r,var_g,var_b;
		var RGB = new Array();
		if(s==0){
		  var_r = var_g = var_b =Math.round(v*255);
		}else{
		  var var_h = h * 6;
		  if (var_h==6) var_h = 0;
		  var var_i = Math.floor( var_h );
		  var var_1 = v*(1-s);
		  var var_2 = v*(1-s*(var_h-var_i));
		  var var_3 = v*(1-s*(1-(var_h-var_i)));
		  if(var_i==0){
		    var_r = v;
		    var_g = var_3;
		    var_b = var_1;
		  }else if(var_i==1){
		    var_r = var_2;
		    var_g = v;
		    var_b = var_1;
		  }else if(var_i==2){
		    var_r = var_1;
		    var_g = v;
		    var_b = var_3
		  }else if(var_i==3){
		    var_r = var_1;
		    var_g = var_2;
		    var_b = v;
		  }else if (var_i==4){
		    var_r = var_3;
		    var_g = var_1;
		    var_b = v;
		  }else{
		    var_r = v;
		    var_g = var_1;
		    var_b = var_2
		  }
	  }
	  return '#'+RGBtoHex(Math.round(var_r * 255),Math.round(var_g * 255),Math.round(var_b * 255));

	};
}

$(function() {
	mt11.start();
});
function Project(xml){
	var _ = this;
	this.isHidden = true;
	this.projectID = mt11.projectAr.length;
	this.id = $(xml).find('id').text();
	this.title = $(xml).find('name').text();
	this.year = $(xml).find('year').text();
	this.urls = [];
	this.navAr = [];
	this.navByName = [];
	$(xml).find('url').each(function(){
		_.urls.push(this);
	});

	this.div = $(mt11.template.project).clone();
	this.imageCount = $(xml).find('images').text();
	this.about = $(xml).find('about');
	this.yt = $(xml).find('youtube').text();
	this.ytw = $(xml).find('youtube').attr('width');
	this.yth = $(xml).find('youtube').attr('height');
	this.width = 370;
	this.height = 500;
	this.x = 0;
	this.navContainer = document.createElement('div');
	this.navContainer.setAttribute('class','navContainer');
	$(this.div).append(this.navContainer);
	if(!mt11.iOS) $(this.navContainer).hide();

	/* METHODS ///////////////////////////////////////////////////////////// */


	this.addLink = function(_type,_label,_link){
		if(!_label) _label = "Visit Site";

		var n = document.createElement('a');
		n.setAttribute('class','nav' );
		n.setAttribute('href','#');
		if(_link){
			$(n).click(function(e){
				window.open(_link);
				e.preventDefault();
			});
		} else {
			$(n).click(function(e){
				$.address.value('/'+_.id+ '/' + _type );
				e.preventDefault();
			});
		}

		$(n).html(_label);
		$(this.navContainer).append(n);
		this.navByName[_label] = n;
		this.navAr.push(n);
	}

	this.populate = function(){
		if(this.yt){
			this.addLink('video','Video');
			_.play = new Image();
			_.play.src = "png/play.png";
			_.play.setAttribute('class','play');
			$(_.play).click(function(e){ $.address.value('/'+_.id+ '/video' );});
			$(_.div).append(_.play);
			$(_.play).hide();
		}
		if(this.imageCount) this.addLink('photos','Photos');
		for( i = 0; i < _.urls.length ; i++ ){

			_.addLink('link',$(_.urls[i]).attr('label'),$(_.urls[i]).text());
		}
		if(this.about) this.addLink('about', ($(this.about).attr('label')) ? $(this.about).attr('label') : 'About');
	}

	this.showLabel = function(){
		if(_.iOS) return;
		if(this.isHidden == false) return;
		this.isHidden = false;
		$('#projectTitle').html(this.title);
		$('#projectYear').html(this.year);
		$(this.navContainer).fadeIn();
		$(this.aTag).css('cursor','default');
		$(_.play).fadeIn('slow');
	}

	this.hideLabel = function(){
		if(_.iOS) return;
		if(this.isHidden) return;
		this.isHidden = true;
		$(this.navContainer).fadeOut();
		$(this.aTag).css('cursor','hand');
		$(_.play).fadeOut('slow');
	}

	this.resize = function(){
		$(this.navContainer).css('top', 500 ) ;
		$(this.navContainer).css('left', (_.width - $(this.navContainer).width() )  / 2 );
		$(_.div).css('top', -250 );
		$(_.img).css('top', (500-this.img.height) * .5);
	}


	/* CONSTRUCTOR ///////////////////////////////////////////////////////////// */



	this.aTag = document.createElement('a');
	this.aTag.setAttribute('href','#');
	this.img = new Image();
	this.img.project = this;

	if(!_.iOS){
		$(this.img).click(function(e){
			if(this.project.projectID != mt11.activeProject){
				if(!_.iOS) $.address.value('/'+this.project.id);
				mt11.slider.gotoProject(this.project.projectID);
			}
			e.preventDefault();
		});
	} else {
		$(this.img).click(function(e){
			e.preventDefault();
		});
	}

	var format = (this.id=='mike') ? ".gif" : ".png";

	$(this.img).load(function(){
		_.height = this.height;
		_.width = this.width;
		_.resize();
		$(this).fadeIn();
	}).attr('src','png/' + this.id + format);
	$(this.img).hide();

	$(this.aTag).append(this.img);
	this.div.append(this.aTag);

	this.x = (370 + mt11.projectSpacing) * mt11.projectAr.length;
	$(this.div).css('left', this.x) ;
	$('#projectSlider')[0].style.width = mt11.sliderWidth += mt11.projectWidth+mt11.projectSpacing;

	if(_.iOS){
		$('#projectContainer')[0].style.width = sliderWidth;
	}

	this.populate();

	$('#projectSlider').append(this.div);
	mt11.projectAr.push(this);
	mt11.projectByName[this.id] = this;


}
function ProjectPhotos(p){
	this.project = p;
	this.imageAr = [];
	this.width = 0;
	this.height = 0;
	this.origWidth = 0;
	this.origHeight = 0;
	this.curPhoto = 0;
	this.div = document.createElement('div');
	this.div.setAttribute('class','projectImages');

	this.close = new CloseButton();
	$(this.div).append(this.close.a);


	var _self = this;

	for(var i=0; i < p.imageCount; i++){
		var img = new Image();
		img.imgId = i;
		img.projectPhotos = this;
		img.setAttribute('class','projectImage');

		var num = (i+1);
		if(num < 10) num = "0"+ num;
		this.imageAr.push(img);
		$(img).load(function(){
			if(this.imgId == 0){
				this.projectPhotos.origWidth = this.width;
				this.projectPhotos.origHeight = this.height;
				this.projectPhotos.firstLoaded();
				this.projectPhotos.resize();
			}

		}).attr('src','screens/'+p.id+'-'+num+'.jpg');
		$(img).hide();
		$(img).click(function(){
			_self.nextPhoto();
		});

		$(this.div).append(img);
	}



	this.nextPhoto = function(){
		$(this.imageAr[this.curPhoto]).fadeOut();
		this.curPhoto ++;
		if(this.curPhoto >= this.imageAr.length) this.curPhoto = 0;
		$(this.imageAr[this.curPhoto]).fadeIn();
	}


	this.firstLoaded = function(){
		_self.show();
		_self.resize();
	}

	this.show = function(){
		$(this.imageAr[this.curPhoto]).fadeIn();
	}

	this.hide = function(){
		$(this.imageAr[this.curPhoto]).fadeOut();
	}

	this.resize = function(){
		var r = this.origHeight / this.origWidth;
		var w = mt11.windowWidth;
		var h = mt11.windowHeight;
		var wr = 1;
		var hr = 1;

		if( w > this.origWidth && h > this.origHeight ){
			w = this.origWidth;
			h = this.origHeight;
		} else {
			wr =  w / this.origWidth;
			hr = h / this.origHeight;

			if(wr > hr){
				h = this.origHeight * hr;
				w = this.origWidth * hr;
			} else {
				h = this.origHeight * wr;
				w = this.origWidth * wr;
			}

		}

		this.close.resize();

		$(".projectImage").each(function(){
			$(this).css('left', -w/2);
			$(this).css('top', 0);
			$(this).css('width',w);
			$(this).css('height',h);
		});

		$(this.div).css('left', 0);
		$(this.div).css('top', (mt11.windowHeight - h ) / 2 ); //( windowHeight - w ) / 2
	}

	this.resize();

}
function Scroller(div){
	var _self = this;
	this.div = div;
	this.bar = $(div).children('#scrollBar');
	this.dragger = $(this.bar).children('#scrollDrag');
	this.titleLabel = $(this.bar).children('#projectTitle');
	this.yearLabel = $(this.bar).children('#projectYear');
	this.dragger.css('left',$(this.bar).css('left'));
	this.dragMax = 0;
	this.isScrolling = false;
	this.targetX = 0;
	this.currentX = 0;


	this.hide = function(){
		$(this.bar).fadeOut();
		$(this.dragger).fadeOut();
	}

	this.show = function(){
		$(this.bar).fadeIn();
		$(this.dragger).fadeIn();
	}

	$(this.dragger).click(function(e){e.preventDefault();});

	$(this.dragger).draggable({
		axis:'x',
		containment:'parent',
		start:function(e,ui){ _self.isScrolling=true; },
		stop:function(e,ui){
			_self.isScrolling=false;
			mt11.slider.snapToClosest(_self.percent());
		},
		drag:function(e,ui){
			mt11.slider.scrollX = ( $('#projectSlider').width() - mt11.projectWidth*1.25  ) * -_self.percent();
			mt11.slider.showLabel(mt11.slider.getClosest(_self.percent()));
		}
	});

	this.percent = function(){
		return ( _self.dragger.position().left )  / _self.dragMax;
	}

	this.update = function(){
		if(this.isScrolling) return;
		var off = mt11.slider.scrollX / ($('#projectSlider').width() - mt11.projectWidth*1.25);
		this.dragger[0].style.left = this.dragMax * -off;
	}

	this.x = function(val){
		$('#navContainer').css('left',val+'px');
	}

	this.y = function(val){
		$('#navContainer').css('top',val+'px');
	}

	this.resize = function(){
		_self.x( mt11.windowWidth * .5);
		_self.y( mt11.slider.y + 350 );
	}

	$('#navContainer').append(this.div);
	this.dragMax = $(this.bar).width() - $(this.dragger).width();
}
function Slider(div,container){
	var _ = this;
	this.container = container;
	this.scrollX = 0;
	this.scrollXCurrent = 0;
	this.div = div;
	this.isScrolling = false;
	this.timeout = 0;
	this.instant = false;
	this.x = 0;
	this.y = 0;
	this.activeY = 0;
	this.inactiveY = -500;
	this.active = true;
	this.minHeight = 800;

	this.show = function(){
		if(this.active) return;
		this.active = true;
		$(this.div).animate({top: this.activeY},1000, 'easeOutCubic');
	}

	this.hide = function(){
		if(!this.active) return;
		this.active = false;
		if(this.instant) $(this.div).css('top', this.inactiveY );
		else $(this.div).animate({top: this.inactiveY}, 500, 'easeInCubic');
	}

	this.resize = function(){
		if(mt11.windowHeight < _.minHeight){
			this.activeY = mt11.windowHeight/2 - (_.minHeight - mt11.windowHeight) * .5;
		} else {
			this.activeY = mt11.windowHeight/2;
		}

		this.x = mt11.windowWidth/2 - mt11.projectWidth/2;

		$(this.container).css('left',this.x);

		if(this.active){
			this.y = this.activeY;
			$(this.div).css('top',this.y);
		} else {
			this.y = this.inactiveY;
			$(this.div).css('top',this.y);
		}

	}

	this.width = function(){
		return $(this.div).width();
	}

	this.touchStart = function(){
		clearTimeout(this.timeout);

	}

	this.touchEnd = function(){
		this.timeout = setTimeout(this.scrollToClosest,500);
	}

	this.update = function(){
		if(this.isScrolling) return;
		this.scrollXCurrent += (this.scrollX - this.scrollXCurrent) * .2;
		_.div.style.left = this.scrollXCurrent;
		this.getClosest();
	}

	this.getClosest = function(targetPercent){
		var d = 99999;
		var c = 99999;
		var z = 0;
		var t = 0;
		if(targetPercent != null){
			t = -targetPercent * this.width();
		} else {
			if(_.iOS){
				t = -iscroll.x;
			} else {
				t = this.scrollXCurrent;
			}
		}

		for(var i in mt11.projectAr){
			c = Math.abs( t + (mt11.projectAr[i].x + mt11.projectWidth / 2) );
			if (c < d){
				z = i;
				d = c;
			}
		}
		return z;
	}

	this.showLabel = function(id){
		this.show();
		mt11.detail.hide();
		for(var i = 0; i < mt11.projectAr.length; i++){
			if(i != id) mt11.projectAr[i].hideLabel();
		}
		mt11.detail.project = mt11.projectAr[id];
		mt11.projectAr[id].showLabel();
	}

	this.scrollToClosest = function(){
		$.address.value('/'+mt11.projectAr[	_.getClosest(iscroll.x/iscroll.maxScrollX)].id);
	}

	this.scrollToProject =  function(name){
		iscroll.scrollTo(-mt11.projectByName[name].x,0,1000);
	}

	this.snapToClosest = function(targetPercent){
		this.showLabel(this.getClosest(targetPercent));
		$.address.value('/'+mt11.projectAr[this.getClosest(targetPercent)].id);
		if(this.getClosest(targetPercent) == mt11.activeProject) this.snap();
	}

	this.snap = function(){
		if(!_.iOS) this.scrollX = -mt11.projectAr[mt11.activeProject].x;
	}

	this.percent = function(){
		if(_.iOS) return -iscroll.x / this.width();
		return Math.max(this.scrollXCurrent / this.width() * -1,0.001);
	}


	this.gotoProject = function(id){
		this.scrollX = -mt11.projectAr[id].x;
		this.showLabel(id);
		mt11.activeProject = id;
	}

	this.gotoProjectByName = function(name){
		var id = mt11.projectByName[name].projectID;
		mt11.activeProject = id;
		this.showLabel(id);

		if(_.iOS) this.scrollToProject(name);
		else this.scrollX = -mt11.projectByName[name].x;
	}

}
function Twitter(project,firstMessage){
	this.project = project;
	this.tweetAr = [];
	this.user = 'miketucker';
	this.count = 10;
	this.current = -1;
	this.div;
	this.firstMessage = firstMessage;
	var _self = this;

	this.div = document.createElement('div');
	this.div.setAttribute('id','tweetbox');
	this.p = document.createElement('p');
	$(this.div).append(this.p);

	$(this.project.div).append(this.div);
	this.topMargin = $(this.div).position().top;
	$(this.div).css('left', ( $(project.div).width() - $(this.div).width() ) / 2 );

	$(this.p).html(this.firstMessage);


	this.align = function(){
		$(_self.div).css('top',_self.topMargin + 17 - $(_self.p).height() / 2 );
	}

	this.align();

	this.parse = function(data){
		$.each(data, function(index, item){
			_self.tweetAr.push(item.text);
		});
		_self.start();
	}

	this.start = function(){
		setInterval(this.updateTweet,8000);
	}

	this.updateTweet = function(){
		_self.current ++;
		if(_self.current >= _self.tweetAr.length) _self.current = 0;
		var t = _self.tweetAr[_self.current];

		if(t.charAt(0) == "@"){ // SKIP TARGETED MESSAGES
			_self.updateTweet();
		} else {
			$(_self.p).html(_self.linkify(t));
			_self.align();
		}
	}

	this.linkify = function(str) {
		str = str.replace(/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
			function(url){
               var full_url = url;
               if (!full_url.match('^https?:\/\/')) {
                   full_url = 'http://' + full_url;
               }
               return '<a href="' + full_url + '" target="_blank">' + url + '</a>';
		});

		str = str.replace(/@+[A-Za-z0-9-_]+/gi, function(m) {
			return m.link('http://twitter.com/'+m.substr(1));
		});

		return str;
	};
	$.ajax({
		type: 'GET',
		url: 'http://twitter.com/status/user_timeline/'+this.user+'.json?count='+this.count+'&callback=?',
		dataType: 'json',
		success: this.parse
	});
}

function Youtube(id,w,h){
	if(w==null) w = 845;
	if(h==null) h = 500;
	this.width = w;
	this.height = h;

	this.div = document.createElement('div');
	$(this.div).css('position','absolute');
	$(this.div).css('width',w);
	$(this.div).css('height',h);
	$(this.div).css('top', (mt11.windowHeight - h) /2);
	$(this.div).css('left', (- w ) /2);
	this.div.setAttribute('class','youtubeContainer');

	str = '<iframe width="'+ w + '" height="' + h + '" src="http://www.youtube.com/embed/'+ id +'?theme=dark&modestbranding=1&autohide=1&autoplay=1" frameborder="0" allowfullscreen></iframe>';

	$(this.div).html(str);

	this.object = $(this.div).find('object');
	this.embed = $(this.div).find('embed');

	this.close = new CloseButton();

	$(this.div).append(this.close.a);

	$(this.div).hide();

	this.hide = function(){
		$(this.div).hide();
	}

	this.show = function(){
		$(this.div).show();
		this.resize();
	}

	this.resize = function(){

		var r = this.width / this.height;
		var w = mt11.windowWidth - 100;
		var h = mt11.windowHeight - 100;
		var wr = 1;
		var hr = 1;

		if( w > this.width && h > this.height ){
			w = this.width;
			h = this.height;
		} else {
			wr =  w / this.width;
			hr = h / this.height;

			if(wr > hr){
				h = this.height * hr;
				w = this.width * hr;
			} else {
				h = this.height * wr;
				w = this.width * wr;
			}

		}

		$(this.object).attr('width',w);
		$(this.object).attr('height',h);
		$(this.embed).attr('width',w);
		$(this.embed).attr('height',h);
		$(this.div).css('left',w /-2);
		$(this.div).css('top', (mt11.windowHeight-h) /2 );
		this.close.resize();
	}
}

