//version 2.018
//07/01/2010  - 10:31 ET

var touchStartX = 0;
var touchMoveX = 0;
var oldMoveX = 0;
var touchEndX = 0;
var touchMoveDistanceX = 0;
var currentScroll = 0;
var inertia = 0;
var defa;
var isiPad;
var isiPhone;
var isiPod;
var storeURL;
var filmURL;
var vari;
var filmARRAY = [];
var thumbnail_urlARRAY = []; 
var titleARRAY = [];
var desARRAY = [];
var timeARRAY = [];
var player_urlARRAY = [];
var store_urlARRAY = [];
var trailer_urlARRAY = [];
var url_ARRAY = [];   
var html_text = '';
var html_trailer = '';
var index = '';  
var count=0;
var wrapper_width;

$.ajax({
	url: '/contents.xml',
	dataType: 'xml', 
	success: function(data){
   		vari = $('div.chooser').attr('default');
		setTimeout(getData, 1000);
		function getData(){
			$(data).find('content').each(function(){
				var $content = $(this);  
				filmARRAY.unshift($content.text());
				thumbnail_urlARRAY.unshift($content.find('thumbnail_url').text());
				titleARRAY.unshift($content.find('title').text()); 
				desARRAY.unshift($content.find('description').text());
				timeARRAY.unshift($content.find('duration').text());
				player_urlARRAY.unshift($content.find('player_url').text()); 
				store_urlARRAY.unshift($content.find('store_link').text());
				trailer_urlARRAY.unshift($content.find('trailer_link').text());
				url_ARRAY.unshift($content.find('url').text());
				var html_img = '<li class="thumb" id="thumb'+count+'"><img src="' + thumbnail_urlARRAY[0] + '" width="189px" alt="' + titleARRAY[0] + '" /><span class="title">' + titleARRAY[0].replace('NOOMA ','').replace('Rob Bell', '') + '</span><div class="over"></div></li>';
				$('ul.sc_menu').prepend(html_img);
				count++;
			}); 
		//generate default
	    	wrapper_width= count*190; 
			defa = 0; 
			defa = url_ARRAY.indexOf(vari);
			var pos = (defa * -190);	
			var drag = ((defa * 190)/5.65); 
		}
		return setTimeout(checkUA,500);
		   
	}	   
});

function checkUA(){
	isiPad = navigator.userAgent.match(/iPad/i)  != null;
	isiPhone = navigator.userAgent.match(/iPhone/i)  !=null;
	isiPod = navigator.userAgent.match(/iPod/i)  !=null;
	
	if(!isiPad && !isiPhone && !isiPod){
		//Desktop
		//addJavascript('js/jquery.dimensions.min.js', 'head');
		//addJavascript('js/jquery.mousewheel.min.js', 'head');
		//addJavascript('js/help.js', 'head');
		//addJavascript('js/swfobject.js', 'head');
	    setTimeout(desktop, 1000);
		//desktopSlider();
	} else if(isiPad){
		//iPad 
		$('body.player').attr('id','ipad'); 
		setTimeout(desktop, 1000);		
		//addJavascript('js/help.js', 'head');
		//addJavascript('js/swfobject.js', 'head');
		//singleFingerScroll();
	} else if( isiPhone || isiPod){ 
		setTimeout(mobileChooser, 1000);
	}
}	
function mobileChooser(){
	pos = (defa * -104);	
	drag = ((defa * 104)/5.65);
	html_text ='<p class="title">' + titleARRAY[defa] + '</p><p class="des">' + desARRAY[defa] + '</p><p class="time">Run Time: ' + timeARRAY[defa] + '</p><a id="trailer" href="' + trailer_urlARRAY[defa] + '.mp4">View Trailer</a>';
	var sideheight = $('#filmdata').height() +15;
	$('#wrapper').css({height: sideheight+"px"});
       
	storeURL = store_urlARRAY[defa];
	filmURL = player_urlARRAY[defa];
	$('div.info').empty(); 
	$('div.info').append(html_text);
	$('#tickets a').attr('href', filmURL );
	$('#notickets a').attr('href', storeURL );
	
	$('li#thumb'+defa).find('span.title').css({'color': '#bae1d0'}).siblings('div.over').css({
		'border-left-width': '1px',
		'border-left-style': 'solid',
		'border-left-color': '#bae1d0',
		'border-right-width': '1px',
		'border-right-style': 'solid',
	   	'border-right-color': '#bae1d0', 
		'border-top-width': '1px',
		'border-top-style': 'solid',
		'border-top-color': '#bae1d0',
		'border-bottom-width': '1px',
		'border-bottom-style': 'solid',
		'border-bottom-color': '#bae1d0',
		'width': '187px',
		'height': '104px',
		'position': 'relative', 
		'top': '-111px',
		'left': '0px'
	}); 
	//click selections
	$('li.thumb').click(function(){
		index = $('li.thumb').index(this);
    	html_text ='<p class="title">' + titleARRAY[index] + '</p><p class="des">' + desARRAY[index] + '</p><p class="time">Run Time: ' + timeARRAY[index] + '</p><a id="trailer" href="' + trailer_urlARRAY[index] + '.mp4">View Trailer</a>';
		storeURL = store_urlARRAY[index];
		filmURL = player_urlARRAY[index];
		//clear selections
		$('li.thumb').find('div.over').css({
			'border-left-style': 'none',
			'border-right-style': 'none',
			'border-top-style': 'none',
			'border-bottom-style': 'none'
		}); 
		
		$('#tickets a').attr('href', filmURL );
		$('#notickets a').attr('href', storeURL );
		$('li.thumb').find('span.title').css({'color': '#fff'});
		//highlight selections
		$('li.thumb').eq(index).find('span.title').css({'color': '#bae1d0'}).siblings('div.over').css({
			'border-left-width': '1px',
			'border-left-style': 'solid',
			'border-left-color': '#bae1d0',
			'border-right-width': '1px',
			'border-right-style': 'solid',
		   	'border-right-color': '#bae1d0', 
			'border-top-width': '1px',
			'border-top-style': 'solid',
			'border-top-color': '#bae1d0',
			'border-bottom-width': '1px',
			'border-bottom-style': 'solid',
			'border-bottom-color': '#bae1d0',
			'width': '187px',
			'height': '105px',
			'position': 'relative',
			'top': '-111px'
		}); 
		$('div.info').empty(); 
		$('div.info').append(html_text);
		var sideheight = $('#filmdata').height() +15;
		$('#wrapper').css({height: sideheight+"px"});
	});
	$(window).scrollTo('1px');
}
function desktop(){
	$('ul.sc_menu').css({width: wrapper_width});
	
	pos = (defa * -190);	
	drag = ((defa * 190)/5.65);	
	if(defa < (url_ARRAY.length)){
		if(isiPad){
			if(defa>5){
				document.getElementById('thumb'+defa).scrollIntoView(true);
			}
		} else{
			if(defa>=5){
				$('#scroller').animate({left: -(defa-4)*190});
				$('div.jScrollPaneDrag').animate({left: drag});
			}
		}
	} else{                                
		//setTimeout(desktop, 500);
	}
	html_text ='<p class="title">' + titleARRAY[defa] + '</p><p class="des">' + desARRAY[defa] + '</p><p class="time">Run Time: ' + timeARRAY[defa] + '</p>';
   
	storeURL = store_urlARRAY[defa];
	filmURL = player_urlARRAY[defa];
    $('#mediaspace').empty().html("<video id='player' controls autobuffer height='294' width='523' poster='"+thumbnail_urlARRAY[defa]+"' src='"+trailer_urlARRAY[defa]+".mp4'><source src='"+trailer_urlARRAY[defa]+".mp4'/><source src='"+trailer_urlARRAY[defa]+".ogg'/><script type='text/javascript'>var so = new SWFObject('player.swf','mpl','523','294','9');so.addParam('allowfullscreen','true');so.addParam('allowscriptaccess','always');so.addParam('wmode','opaque');so.addVariable('file','"+trailer_urlARRAY[defa]+".mp4');so.addVariable('image','"+thumbnail_urlARRAY[defa]+"');so.addVariable('controlbar','none');so.write('mediaspace'); </script></video>");

	$('div.info').empty().append(html_text); 
	$('li.thumb').eq(defa).find('span.title').css({'color': '#bae1d0'}).siblings('div.over').css({
		'border-left-width': '1px',
		'border-left-style': 'solid',
		'border-left-color': '#bae1d0',
		'border-right-width': '1px',
		'border-right-style': 'solid',
	   	'border-right-color': '#bae1d0', 
		'border-top-width': '1px',
		'border-top-style': 'solid',
		'border-top-color': '#bae1d0',
		'border-bottom-width': '1px',
		'border-bottom-style': 'solid',
		'border-bottom-color': '#bae1d0',
		'width': '187px',
		'height': '104px',
		'position': 'absolute', 
		'top': '0'
	});

	//click selections
	$('li.thumb').bind('click', selectThumb); 
	$('#tickets a').attr('href', filmURL );
	$('#notickets a').attr('href', storeURL );
	return either();  
}

function either(){
	if (isiPad){
		singleFingerScroll();
	}else{
		desktopSlider();
	}
}

function selectThumb(){
	index = $('li.thumb').index(this);
  		html_text ='<p class="title">' + titleARRAY[index] + '</p><p class="des">' + desARRAY[index] + '</p><p class="time">Run Time: ' + timeARRAY[index] + '</p>';
	$('#mediaspace').empty().html("<video id='player' controls autobuffer height='294' width='523' poster='" + thumbnail_urlARRAY[index] + "' src='" + trailer_urlARRAY[index] + ".mp4'><script type='text/javascript'>var so = new SWFObject('player.swf','mpl','523','294','9');so.addParam('allowfullscreen','true');so.addParam('allowscriptaccess','always');so.addParam('wmode','opaque');so.addVariable('file','"+ trailer_urlARRAY[index] +".mp4');so.addVariable('image','" + thumbnail_urlARRAY[index] + "');so.addVariable('controlbar','none');so.write('mediaspace'); </script></video>");
	storeURL = store_urlARRAY[index];
	filmURL = player_urlARRAY[index];
	$('div.info').empty(); 
	$('div.info').append(html_text);
	//clear selections
	$('li.thumb').find('div.over').css({
		'border-left': 'none',
		'border-right': 'none',
		'border-top': 'none',
		'border-bottom': 'none'
	});
	$('li.thumb').find('span.title').css({'color': 'white'});
	//highlight selections
	$('li.thumb').eq(index).find('span.title').css({'color': '#bae1d0'});
	$('li.thumb').eq(index).find('div.over').css({
		'border-left-width': '1px',
		'border-left-style': 'solid',
		'border-left-color': '#bae1d0',
		'border-right-width': '1px',
		'border-right-style': 'solid',
	   	'border-right-color': '#bae1d0', 
		'border-top-width': '1px',
		'border-top-style': 'solid',
		'border-top-color': '#bae1d0',
		'border-bottom-width': '1px',
		'border-bottom-style': 'solid',
		'border-bottom-color': '#bae1d0',
		'width': '187px',
		'height': '104px',
		'position': 'absolute', 
		'top': '0'
	});
	$('#tickets a').attr('href', filmURL );
	$('#notickets a').attr('href', storeURL );
	
}
 
function desktopSlider(){
	$('#scroller').jScrollHorizontalPane({scrollbarHeight:11});
	$('#scroller').css({left: '0px'});
}
 	
function singleFingerScroll(){
	setTimeout(events, 500);
	function events(){
		$(document).ready(function() {
			document.getElementById("wrapper").addEventListener("touchstart", onTouchStart, false);
			document.getElementById("wrapper").addEventListener("touchmove", onTouchMove, false);
			document.getElementById("wrapper").addEventListener("touchend", onTouchEnd, false);
		});
	}
	function onTouchStart(event){
		//event.preventDefault();
		touchStartX = event.targetTouches[0].pageX;
		currentScroll = $("#scroller").scrollLeft();
		$("#scroller").stop(true, true);
		oldMoveX = 0;
		touchMoveX = 0;
		touchEndX = 0;
		touchMoveDistanceX = 0;
	}
	function onTouchMove(event){
		event.preventDefault();
		oldMoveX = touchMoveX;
		touchMoveX = event.targetTouches[0].pageX;
		touchMoveDistanceX = touchMoveX - touchStartX;
		$("#scroller").scrollLeft(currentScroll + touchMoveDistanceX * -1);
	}
	function onTouchEnd(event){
		event.preventDefault();
		inertia = (oldMoveX - touchMoveX);
		// Inertia Stuff
		if( Math.abs(inertia) > 10 ){
			$("#scroller").animate({
				'scrollLeft': $("#scroller").scrollLeft() + (inertia * 10)
			}, inertia * 20);
		}else{

		}
	}
	function thumbClick(event){
   		if(touchMoveDistanceX < 5){
			var targetThumb = event.target.id;
			$("#" + targetThumb).css("-webkit-transform", "transformZ(40)");
		}
	}
}

function hasTicket(){
	$('div#notickets').hide();
	$('div#tickets').show();
	$('#tickets a').attr('href', filmURL );
}   

function noTicket(){
	$('div#tickets').hide();
	$('div#notickets').show();
	$('#notickets a').attr('href', storeURL );
}
   
/*Load External JS*/
function addJavascript(jsname,pos) {
	var th = document.getElementsByTagName(pos)[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',jsname);
	th.appendChild(s);
} 
