var defaultpagename="";
var defaultShare="";
//var defaultShare=$('#addthis').attr('href');
var html5_current_video_index=0;
var shareURL="http://www.getolympus.com/lifeproof/index.asp?cid=lifeproof";
var flowplayer_object="";
var videos =[{"file":"whitewater_proof","omniture_title":"whitewater-proof"},
			{"file":"snowboard_proof","omniture_title":"snowboard-proof"},
			{"file":"slam_proof","omniture_title":"slam-proof"},
			{"file":"spin_cycle_proof","omniture_title":"spin-cycle-proof"},
			{"file":"ocean_proof","omniture_title":"ocean-proof"},
			{"file":"mountain_proof","omniture_title":"mountain-proof"},
			{"file":"snowplow_proof","omniture_title":"snowplow-proof"},
			{"file":"strike_proof","omniture_title":"strike-proof"}];

var start_clip=videos[0].file;
function tough_player()
{
	$('#video-player').html('');
	
	flowplayer_object="";
	var mod_rtmp_server="rtmp://media2.olympusamerica.com/vod";
	flowplayer_object=flowplayer
	(
	"video-player",
	{
		src:"/swf/flowplayer/flowplayer.commercial-3.1.5.swf",
		wmode:'opaque'
	},
	{
		'key':oly_fp_key,
		play:{opacity:0},
		api:true,
		canvas:
		{
			width:640,
			height:360,
			backgroundColor:'#000000',
			backgroundImage: 'url(/lifeproof/images/video-player-bg.jpg)',
			backgroundGradient:'none'
		},
		screen:
		{
			width:640,
			height:360,
			top:0,
			left:0
		},
		clip:
		{
			provider:'olympus_rtmp'
		},
		playlist:
		[
			//url:'mp4:lifeproof/10sec/'+videos[0].file+'.mp4',
			{
				url:'mp4:lifeproof/30sec/'+videos[0].file+'.mp4',
				onStart:function(){selectClip(0);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[1].file+'.mp4',
				onStart:function(){selectClip(1);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[2].file+'.mp4',
				onStart:function(){selectClip(2);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[3].file+'.mp4',
				onStart:function(){selectClip(3);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[4].file+'.mp4',
				onStart:function(){selectClip(4);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[5].file+'.mp4',
				onStart:function(){selectClip(5);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[6].file+'.mp4',
				onStart:function(){selectClip(6);}
			},
			{
				url:'mp4:lifeproof/30sec/'+videos[7].file+'.mp4',
				onStart:function(){selectClip(7);},
				onBeforeFinish:function() {return false;}
			}
		],
		plugins: 
		{
			olympus_rtmp:
			{
				url:'/swf/flowplayer/flowplayer.rtmp-3.1.4.swf',
				netConnectionUrl:mod_rtmp_server
			},
		   controls: 
		   {
				url:'/swf/flowplayer/flowplayer.controls-3.1.5.swf',
				all:false,
				play:true,
				scrubber:true,
				time:true,
				volume:true,
				autoHide:'always',
				hideDelay:800,
				backgroundColor:'#000000',
				backgroundGradient:'none',
				borderRadius:'0px',		  
				volumeSliderGradient:'none',
				volumeSliderColor:'#007fd8',
				sliderGradient:'none',
				sliderColor:'#bbbbbb',
				bufferGradient:'none',
				bufferColor:'#d5d5d5',
				progressGradient: 'none',
				progressColor: '#ffffff',
				buttonColor:'#000000',
				buttonOverColor:'#007fd8',
				timeBgColor:'#000000',
				timeColor:'#007fd8',
				durationColor:'#ffffff',
				tooltipColor:'#5F747C',
				tooltipTextColor: '#ffffff',
				height: 20,
				bottom:15,
				width:500
			}
		}
	});
}

function tough_player_html5()
{
	$('#video-player').html('');
	$('#video-player').append('<video id="video-player-html5" controls autoplay style="width:640px;height:360px;" onended="next_video_html5()"></video>');
}

function selectClip(video_id)
{
	var tmpvideo=videos[video_id].file;
	
	$('.video-thumb_active').removeClass('video-thumb_active').addClass('video-thumb');
	$('#video-'+tmpvideo).removeClass('video-thumb').addClass('video-thumb_active');
	$('.video-thumb .overlay').show();
	
	$('.video-title_active').removeClass('video-title_active').addClass('video-title');
	$('#video-title-'+tmpvideo).removeClass('video-title').addClass('video-title_active');
	$('.video-title').hide();
	$('.video-title_active').show();
	
	buildShareURL(video_id);
	track_video(tmpvideo);
}
function track_video(omni_title)
{
	var s=s_gi(s_account);
	s.linkTrackVars="pageName";
	s.pageName=defaultpagename+":"+omni_title;
	void(s.t());
	
}
function buildShareURL(number)
{
	shareTag='<a href="http://www.addthis.com/bookmark.php" id="video-share" class="addthis_button" ';
	shareTag2='><img src="/lifeproof/images/share_video.png" alt="Share Video" /></a>';
	shareURL='addthis:url="http://www.getolympus.com/lifeproof/index.asp?video='+videos[number].file+'&cid=lifeproof_'+videos[number].file+'"';
	addThisElement=shareTag+shareURL+shareTag2;
	$('#video-share').remove();
	$('#video-content').append(addThisElement);
}
function play_clip(video)
{
	var video_index=0;
	for (var i in videos)
	{
		if(videos[i].file==video){video_index=Number(i);}
	}
	flowplayer_object.play(video_index);
}

function play_clip_html5(video)
{
	var video_index=0;
	var video_prefix="/lifeproof/videos/";
	for (var i in videos)
	{
		if(videos[i].file==video){video_index=Number(i);}
	}
	html5_current_video_index=video_index;
	$('#video-player-html5').attr('src',video_prefix+videos[video_index].file+'.m4v');
	selectClip(html5_current_video_index);
}

function next_video_html5()
{
	$('#video-player').html('');
	var video_num=(videos.length-1);
	html5_current_video_index++;
	if (html5_current_video_index>video_num)
	{html5_current_video_index=0;}
	if (html5_current_video_index<0)
	{html5_current_video_index=0;}
	tough_player_html5();
	play_clip_html5(videos[html5_current_video_index].file);
}
function change_video_html5(video)
{
	$('#video-player').html('');
	tough_player_html5();
	play_clip_html5(video);
}

function getVideoNameFromID(id)
{
	var tmpvideo=id.replace("video-","");
	return tmpvideo;
}
function urlencode(str) {
return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

$(document).ready(function()
{	
	defaultpagename=s.pageName;
	defaultShare=$('#addthis').attr('href');
	/*if(DetectIphoneOrIpod() || DetectIpad())
	{
		$('.video-thumb').live('click',function(){
			change_video_html5(getVideoNameFromID($(this).attr('id')));
		});
		$('.video-thumb_active').live('click',function(){
			change_video_html5(getVideoNameFromID($(this).attr('id')));
		});
		tough_player_html5();
		play_clip_html5(start_clip);
	}
	else
	{*/
		$('.video-thumb').live('mouseover',function(){
			$(this).children('.overlay').hide();
			$('.video-title_active').hide();
			$('.video-title').hide();
			videoTitleToShow="#video-title-"+getVideoNameFromID($(this).attr('id'));
			$(videoTitleToShow).show();
		});
		$('.video-thumb').live('mouseout',function(){
			$(this).children('.overlay').show();
			$('.video-title').hide();
			$('.video-title_active').show();			
		});
		$('.video-thumb').live('click',function(){
			play_clip(getVideoNameFromID($(this).attr('id')));
		});
		$('.video-thumb_active').live('click',function(){
			play_clip(getVideoNameFromID($(this).attr('id')));
		});
		tough_player();
		if (start_clip!=videos[0].file)
		{play_clip(start_clip);}
	//}
});
