/* 
	Company: BrandUp Interactive - www.brandup.ro
	Author: Constantin Dumitrescu - @daslocer
*/
var videos = [
			['Three Feet High', 'Christmas Promotion', 'Buy 1 Get 1 Free', 'v0_free', ''],
			['Blooded', 'Film Trailer', 'Post-production &amp; delivery for Revolver Entertainment.', 'v1_blooded', '01:21'],
			['Give iTunes', 'Promo', 'Creative, animation & delivery for Apple.', 'v2_itunes', '01:00'],
			['The Wingman', '7 x 48" Series', 'Full series production for Red Bull.', 'v3_wingman', '01:00'],
			['Cricket in America', 'Documentary', 'Full production & delivery for ICC.', 'v4_cricketUSA', '11:36'],
			['Freetown', 'Feature Film', '2nd Unit action photography & development for May 13.', 'v5_freetown', '02:12'],
			['Heartless', 'Film Trailer', 'Post-production & delivery for Lionsgate.', 'v6_heartless', '02:03'],
			['Primevil', 'Film Trailer', 'Post-production & delivery for Revolver Entertainment.', 'v7_primevil', '01:05'],
			['Thinkwise', 'Public Service Films', 'Full production for Unicef & UNAids.', 'v8_thinkwise', '00:40'],
			['Psalm 21', 'Film Trailer', 'Post-production & delivery for Revolver Entertainment.', 'v9_psalm21', '01:17'],
			['Give iTunes (Japan)', 'Promo', 'Creative, animation & delivery for Apple.', 'v10_itunesJP', '01:00'],
			['What War May Bring', 'Film Trailer', 'Post-production & delivery for Revolver Entertainment.', 'v11_whatwar', '01:09'],
			['What we do', 'Promo', 'Luttrell, 3FH and Nuview partner reel.', 'v12_whatwedo', '03:17'],
			['Hayemaker', 'Promo', 'Promotional shoot coverage & post for Icon.', 'v13_haye', '01:04'],
			['Change the World', 'Promo', 'Promotional video for Laureus & The Qatar Foundation.', 'v14_qatar', '04:01'],
			['What they say', 'Promo', 'What they say about Three Feet High.', 'v15_whattheysay', '01:01'],
			['Robbie Keane', 'Promo', 'Promotional shoot coverage &amp; post for Icon.', 'v16_keane', '01:14']
		];
		
var Drag = function() {

	var init = { x : 0 },
			end = { x : 0},
			ul = $('#player-que ul'),
			elem = {},
			count = $('#player-que li').length,
			queLength = count * 208 + count -1,
			limit = -1 * (queLength - $('#player-que').width() + 100); 
	
	function selection(val) {
		if ('undefined' !== typeof this.onselectstart) {
				document.onselectstart = function () { return false; };
		} else {
				document.onmousedown   = function () { return false; };
				document.onclick       = function () { return true;  };
		}
	}
	
	function dragging(e) {
		var pos = end.x - init.x + e.pageX;
		/*
		if ( 100 < pos ) {
			pos = 100;
		} else if ( limit > pos ) {
			pos = limit;
		}
		*/
		setPosition(pos);
	}
	
	function setPosition( pos) {
		ul.css({marginLeft: pos});
	}
	
	function clicked() {
	
		var index = $(elem).index();
		
		$('#player-title').html(videos[index][0]);
		$('#player-genere').html(videos[index][1]);
		$('#player-description').html(videos[index][2]);
		$('#player-time').html(videos[index][4]);
		
		
		if(index == 0) { 
		$('.video-js-box').html('<a href="mailto:verity@threefeethigh.co.uk"><img src="video/' + videos[index][3]+ '_large.jpg" /></a>');
		} else {
		$('.video-js-box').html(
						'<video class="video-js" height="548" width="980" poster="http://threefeethigh.co.uk/video/' + videos[index][3]+'_large.jpg">'+
		   '+<source src="http://threefeethigh.co.uk/video/' + videos[index][3]+'_video.mp4" type=\'video/mp4; codecs="avc1.42E01E,'+'mp4a.40.2"\' />'+
      '<object id="flash_fallback_1" class="vjs-flash-fallback" width="980" height="548" type="application/x-shockwave-flash"'+
        'data="flowplayer-3.2.7.swf">'+
        '<param name="movie" value="flowplayer-3.2.7.swf" />'+
        '<param name="allowfullscreen" value="true" />'+
        '<param name="flashvars" value=\'config={"playlist":["video/' + videos[index][3]+'_large.jpg", {"url":'+
				'"video/' + videos[index][3]+'_video.mp4","autoPlay":false,"autoBuffering":true}]}\' />'+
      '</object>'+
			'			</video>');
		VideoJS.setupAllWhenReady();
		}
	}
	
	return {
		startDragging : function(that, e) {
			init = { x: e.pageX };
			elem = that;
			$('body')
				.bind('mousemove', function(e) { selection(false); dragging(e); })
				.bind('mouseup', function(e) { Drag.endDragging(e); });
				
			selection(false);
		},
		endDragging : function(e) {
			end = { x : ul.css('margin-left').replace('px', '')*1 };

			if ( init.x == e.pageX || ( init.x < e.pageX && init.x > e.pageX - 10 ) || ( init.x > e.pageX && init.x - 10 < e.pageX ) ) {
				clicked();
			}
			
			if ( 0 < end.x ) {
				ul.stop().animate({marginLeft:0}, 200, 'easeOutSine');
				end.x = 0;
			} else if ( -1 * (queLength - $('#player-que').width()) > end.x ) {
				ul.stop().animate({marginLeft: -1 * (queLength - $('#player-que').width())}, 200, 'easeOutSine');
				end.x = -1 * (queLength - $('#player-que').width());
			}
			
			$('body').unbind('mousemove').unbind('mouseup');
			selection(true);
		}
	}
}();

$(function() {
	var iPad = navigator.userAgent.match(/iPad/i) != null;
	$('#logo img').animate({top:0}, 2200, 'easeOutBounce', function() {
		$('#main').fadeIn(1200, 'easeInOutSine', function() {
		    if(!iPad) VideoJS.setupAllWhenReady();
		});
		$('footer').css({display:'block'});
	});
	if(iPad) { 
		var start = 0;

		$('#player-que li').mtouch({
			draggedLeft: function(e) {
				// new start
				if(start != e.start.x) {
					start = e.start.x;
					ulStart = $('#player-que ul').css('margin-left').replace('px', '')*1;
				}
				$('#player-que ul').css({marginLeft:  ulStart - e.delta.x});  
			},
			draggedRight: function(e) {
				if(start != e.start.x) {
					start = e.start.x;
					ulStart = $('#player-que ul').css('margin-left').replace('px', '')*1;
				}
				$('#player-que ul').css({marginLeft:  ulStart + e.delta.x*-1});  
			}
		});
	} else {
	$('#player-que li')
		.bind('mousedown', function(e) { Drag.startDragging(this, e); });
	}
	$('#backToTop').click(function() {

	
	});
	
	function rotate() {
	
	}
	
	$('#description-logo').hover(function() {


	}, function() {
	
	
	});

});























