// JavaScript Document

// JavaScript Document
var $vp = jQuery.noConflict();

//do not change these values
var animationSpeed = 400;
var $vp_player_loaded = false;
var $vp_timeout;
var $vp_player;

//page is ready
$vp(document).ready(vp_READY);

function vp_READY(){
	vp_manip_dom();
	vp_bind_events();
	vp_trigger_events();
}

function vp_trigger_events(){
	var playlistItem = parseInt(gup('playlistItem'));
	if(playlistItem){
		$vp('[file][image][title][group]:eq(' + parseInt(playlistItem - 1) + ')').trigger('click');
	}
}

function vp_manip_dom(){
	$vp('#vp_mask').appendTo('body');
	$vp('#vp').appendTo('body');
	$vp('#vp_mask').css({opacity: .9});
	
	$vp('[file][image][title]').each(function(){
		var myGroup = $vp(this).attr('group');
		if(!myGroup){	
			$vp(this).attr('group', 'default');
		}
	});
}

function vp_bind_events(){
	$vp('#vp_arrow_right').click(vp_arrow_right_CLICK);
	$vp('[file][image][title]').click(vp_CLICK);
	$vp('#vp_mask').click(vp_mask_CLICK);

	$vp(document).keyup(function(e) {
		if (e.keyCode == 27 && $vp( '#vp_mask' ).is( ':visible' ) ) { 
			$vp('#vp_mask').trigger( 'click' );
		}
	});
	
	$vp('#vp_close').click(function(){ $vp('#vp_mask').trigger( 'click' ) } );
}

function vp_mask_CLICK(e){
	e.preventDefault();
	e.stopImmediatePropagation();
	
	var tempTag = jQuery.url.attr("path").replace(".aspx", "") + "_html_video_stop_video";
	WTExternalBridge('dcsMultiTrack', 'DCS.dcsuri,' + tempTag );
	
	jwplayer("vp_video").stop();
	
	$vp('#vp_title').text('closing...');
	$vp_player_loaded = false;
	$vp('#vp:not(:animated)').fadeOut({complete: vp_FADE_OUT, duration: animationSpeed});	
	function vp_FADE_OUT(){
		$vp('#vp_mask:not(:animated)').fadeOut({complete: vp_mask_FADE_OUT, duration: animationSpeed});	
	}
	function vp_mask_FADE_OUT(){
		$vp('body, html').removeClass('vp_lock');
	}
}

function vp_arrow_right_CLICK(e, toggle){
	e.preventDefault();
	e.stopImmediatePropagation();
	
	var mySelector = '#vp_thumbs > img:first-child:not(:animated)';
	$vp(mySelector).clone(true).appendTo('#vp_thumbs');
	
	var customSpeed = animationSpeed;
	if(toggle){
		customSpeed = 0;
	}
	
	$vp(mySelector).animate({width: 0}, {complete: vp_thumb_ANIMATE, duration: customSpeed});
}

function vp_thumb_ANIMATE(){
	$vp(this).remove();
	$vp('#vp_thumbs > img:last-child').width(222);
}

function vp_CLICK(e){
	e.preventDefault();
	e.stopImmediatePropagation();
	
	$vp('#vp_title').text('loading...');
	
	var currentThumb = this;
	var currentIndex = $vp(this).index();
	
	if($vp('#vp').is(':hidden')){
		$vp('#vp_thumbs').empty();
		
		var myGroup = $vp(this).attr('group');
		myGroup = '[group=' + myGroup + ']';
		
		$vp('[file][image][title]' + myGroup).each(function(index){
			$vp(this).clone(true).appendTo('#vp_thumbs');
		});
		
		var totalThumbs = $vp('#vp_thumbs > img').length;
	
		if(totalThumbs < 2){
			$vp('#vp_thumbs').hide();
		}
		if(totalThumbs > 3){
			$vp('#vp_arrow_right').show();
		}
		
		vp_highlight_thumb(currentIndex);
		
		for(var i = 0; i < currentIndex - 1; i++){
			$vp('#vp_arrow_right').trigger('click', true);
		}
	}else{
		vp_highlight_thumb(currentIndex);
	}
	
	$vp('body, html').addClass('vp_lock');	
	$vp('#vp_mask:not(:animated)').fadeIn({complete: vp_mask_FADE_IN, duration: animationSpeed});	
	
	function vp_mask_FADE_IN(){
		$vp('#vp:not(:animated)').fadeIn({complete: vp_FADE_IN, duration: animationSpeed});	
	}
	
	function vp_FADE_IN(){
	
		var myFile = $vp(currentThumb).attr('file');
		var myImage = $vp(currentThumb).attr('image');
		var myTitle = $vp(currentThumb).attr('title');
		
		$vp('#vp_title').text(myTitle);
		
		if($vp.browser.msie)
			stretchingMethod = 'exactfit';
		else
			stretchingMethod = 'uniform';
		
//Apple Handhelds do not use the traditional YouTube player, so always reload the player on change		
		if( $vp_player_loaded && !is_handheld_apple() ){
			clearTimeout( $vp_timeout );
			try{
				$vp_player.sendEvent( 'LOAD', {'file':myFile, 'image': "/ScriptSite/VIC/2012/Gunny/black-636-x-361.gif", 'autostart':true} );
				if( jwplayer("vp_video").getState() == 'BUFFERING' )
					$vp_timeout = setTimeout( function(){
						vp_still_buffering( myFile, myImage, stretchingMethod )
					}, 5000);
					
			}catch(jwplayer_error){
				vp_init_jwplayer( myFile, myImage, stretchingMethod);			
			}
		}
		else{
			vp_init_jwplayer( myFile, myImage, stretchingMethod);
		}
	}
}

function vp_still_buffering( myFile, myImage, stretchingMethod ){
	if( jwplayer("vp_video").getState() == 'BUFFERING' ){
		vp_init_jwplayer( myFile, myImage, stretchingMethod);			
	}
}

function vp_init_jwplayer( myFile, myImage, stretchingMethod){
	jwplayer("vp_video").setup({
		flashplayer: "/ScriptSite/jwplayer/player.swf", 
		height: 406, 
		width: 720,
		skin: '/ScriptSite/jwPlayer/skin/glow.zip',
		stretching: stretchingMethod,
		file: myFile,
		image: "/ScriptSite/VIC/2012/Gunny/black-636-x-361.gif",
		controlbar: "bottom",
		autostart: true,
		stretching: 'none',
		events: {
		/*
			'onVolume': function(){
			    var tempTag = jQuery.url.attr("path").replace(".aspx", "") + "_html_video_volume_change";
				WTExternalBridge('dcsMultiTrack', 'DCS.dcsuri,' + tempTag );
			},
			'onResize': function(event){
				var tempTag = "";
				if(event.width == 720)
				{
					tempTag = jQuery.url.attr("path").replace(".aspx", "") + "_html_video_reg_screen";
					WTExternalBridge('dcsMultiTrack', 'DCS.dcsuri,' + tempTag );
				}
				else if(event.width > 720)
				{
					tempTag = jQuery.url.attr("path").replace(".aspx", "") + "_html_video_full_screen";
					WTExternalBridge('dcsMultiTrack', 'DCS.dcsuri,' + tempTag );
				}
			},
			'onSeek': function(){
			    var tempTag = jQuery.url.attr("path").replace(".aspx", "") + "_html_move_scrub_bar";
				WTExternalBridge('dcsMultiTrack', 'DCS.dcsuri,' + tempTag );
			},*/
			'onReady':function(){
				$vp_player = document.getElementById('vp_video');
				$vp_player_loaded = true;
			},
			'onError':function(){
				vp_init_jwplayer( myFile, myImage, stretchingMethod);
			},
			'onPlay':function(){
				if(document.URL.indexOf("judge") > -1) {
					WTExternalBridge('dcsMultiTrack','DCS.dcsuri,/en-us/victory-motorcycles/cruisers/judge/pages/introduction/event_videoplay_bike_launch,WT.si_n,VIC-videoplay-enus-2012_Judge_Intro-event-Bike_Launch,WT.si_x,1');
				}
				else if(document.URL.indexOf("valentines-gunny-message.aspx") > -1) {
					if(document.URL.indexOf("en-us") >-1)
					{
					WTExternalBridge('dcsMultiTrack','"DCS.dcsuri,/en-us/victory-motorcycles/landing/pages/valentines-gunny-message/event_videoplay_valentine_msg,WT.si_n,VIC-videoplay-enus-Gunny_Valentine-event-Valentine_Msg,WT.si_x,1"');
					}
					else if(document.URL.indexOf("en-ca") >-1)
					{
					WTExternalBridge('dcsMultiTrack','"DCS.dcsuri,/en-ca/victory-motorcycles/landing/pages/valentines-gunny-message/event_videoplay_valentine_msg,WT.si_n,VIC-videoplay-enca-Gunny_Valentine-event-Valentine_Msg,WT.si_x,1"');
					}
				}
			}
		}
	});
}

function vp_highlight_thumb(currentIndex){
	$vp('#vp_thumbs > img').css({opacity: .5});
	$vp('#vp_thumbs > img:eq(' + currentIndex + ')').css({opacity: 1});
}

function gup(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  	var regexS = "[\\?&]"+name+"=([^&#]*)";
  	var regex = new RegExp( regexS );
  	var results = regex.exec(window.location.href);
  	if(results == null){
  		return "";
  	}else{
  		return results[1];
  	}
}

function is_handheld_apple(){
    return ( (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPad") != -1) || (navigator.platform.indexOf("iPod") != -1) );
}
