/*****************************************************
 * This version pauses at end of each video
 ****************************************************/

var togglepane = 0;
//var targetvid = -1;
var customPauseFlag = false;
var embedcurvid = "";
var embedallvid = "";
var encryptedindex = "";
var curpane = "";
var vidclick = 0;


var scrollApi;

function closepane() {
	$(curpane.attr("href")).hide();
	
	document.getElementById('sharesubmit').value='Share';
	
	$f("video").show();

	if(vidclick == 0 && $f("video").isPaused() == true && customPauseFlag == false){
		$f("video").resume();
	}

	vidclick = 0;
	togglepane = 0;
}

function swappane(current){
	$f("video").pause();
	$f("video").hide(true);
	$("a#splashpane").hide();
	
	$($("a#getcode").attr("href")).hide();
	$($("a#geturl").attr("href")).hide();
	$($("a#emailfriend").attr("href")).hide();
	
	if(current == "code"){
		togglepane = (togglepane == 1) ? 0 : 1;
	}
	else if(current == "email"){
		togglepane = (togglepane == 2) ? 0 : 2;
	}
	else if(current == "url"){
		togglepane = (togglepane == 3) ? 0 : 3;
	}
	
	return togglepane;
}
	
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 -1;}
  else{
    return results[1];}
}

$(function() {		
	//targetvid = parseInt(gup('v'), 10);
	
	// VIDEO PLAYER INITIALIZE
	$f("video", {src: 'http://www.binarypulse.com/multimedia/swf/flowplayer.commercial-3.1.1.swf', wmode: 'transparent'},{
	   	 key: '@7999ae8571d4535cf1d',
		 canvas: {
			backgroundColor: '#292929',
			backgroundGradient: 'none'
		 },
		 play: {
			opacity: 0.7,
		 	replayLabel: null
		 },
		 clip: {
			baseUrl: 'http://www.binarypulse.com/multimedia/videos',
			//baseUrl: 'http://dev.binaryinteractive.com/binarypulse2009/multimedia/videos',
			thumb: 'playlist-icon.jpg',
			scaling: 'fit',
			bufferLength: 2,
			autoBuffering: true,
			autoPlay: false,
			onSeek: function() {
				customPauseFlag = false;
			},
			onResume: function() {
				if(customPauseFlag == true){
					this.stop();
					this.play();
					customPauseFlag = false;
				}
			},
			onBegin: function(){
				if(customPauseFlag == true){
					this.play();
					customPauseFlag = false;
				}
				
				curpagenum = Math.floor(this.getClip().index/6);
				if( scrollApi.getPageIndex() != curpagenum){
					scrollApi.setPage(curpagenum);
				}
				
				encryptedindex = "s"+Math.floor(Math.random()*11)+"g" + Math.floor(Math.random()*101)+"z" + $f("video").getClip().index + "d"+Math.floor(Math.random()*2001)+"f";
			},
			onBeforeFinish: function(){
				customPauseFlag = true;
				this.pause();
			}
		},
		// our playlist 
		playlist: [
			{ 
				url: 'BP_BearVidV1.flv',
				title: '"One Step Ahead" Drive Demand with Multimedia',
				desc: '"One Step Ahead" Drive Demand with Multimedia',
				thumb: 'tn_bear.jpg',
			},
			{ 
				url: 'Hanger_whats_new.flv', 
				title: 'Hanger Orthopedic Group: "What&#180;s Next?" Video',
				desc: 'Hanger Orthopedic Group: "What&#180;s Next?" Video',
				thumb: 'tn_hanger.jpg'
			},
			{ 
				url: 'Newton_Mogomercial.flv', 
				title: 'Newton Peripherals: MoGo Mouse Launch Commercial',
				desc: 'Newton Peripherals: MoGo Mouse Launch Commercial',
				thumb: 'tn_newton2.jpg'
			},
			{ 
				url: 'GreenAware_BPmult.flv',
				title: 'Experian: GreenAware Product Overview Video',
				desc: 'Experian: GreenAware Product Overview Video',
				thumb: 'tn_greenaware.jpg'
			},
			{ 
				url: 'Avaak_Dog_Spot.flv',
				title: 'Avaak: Vue Web Spot',
				desc: 'Avaak: Vue Web Spot',
				thumb: 'tn_avaak.jpg'
			},
			{ 
				url: 'IOGear_30SecSpot.flv',
				title: 'IOGear: Wireless Audio/Video Kit Hulu Spot',
				desc: 'IOGear: Wireless Audio/Video Kit Hulu Spot',
				thumb: 'tn_iogear.jpg'
			},
			{ 
				url: 'Apriso_RoadRules.flv',
				title: 'Apriso: Initiative Launch Video',
				desc: 'Apriso: Initiative Launch Video',
				thumb: 'tn_apriso.jpg'
			},     
			{ 
				url: 'Averatec_NEO.flv', 
				title: 'Averatec: Product Preview Video',
				desc: 'Averatec: Product Preview Video',
				thumb: 'tn_averatec.jpg'
			},
				{ 
					url: 'Bluetooth_LivingWirelessly.flv', 
					title: 'Bluetooth: Consumer Education Video',
					desc: 'Bluetooth: Consumer Education Video',
					thumb: 'tn_bluetooth.jpg'
				},
				{ 
					url: 'EnCasePortable.flv', 
					title: 'Guidance: EnCase Portable',
					desc: 'Guidance: EnCase Portable',
					thumb: 'tn_guidance.jpg'
				},
				{ 
					url: 'Revolution.flv', 
					title: 'Medsphere: Recruitment and Tradeshow Video',
					desc: 'Medsphere: Recruitment and Tradeshow Video',
					thumb: 'tn_revolution.jpg'
				},
				{ 
					url: 'XTM_Rail.flv', 
					title: 'XTM Racing: Rail Video',
					desc: 'XTM Racing: Rail Video',
					thumb: 'tn_xtm.jpg'
				},
				{ 
					url: 'Hirsch_flash.flv', 
					title: 'Hirsch: Five Tips For Secure Success',
					desc: 'Hirsch: Five Tips For Secure Success',
					thumb: 'tn_hirsch.jpg'
				},
				{ 
					url: 'Xario_Ep1.flv', 
					title: 'Xario Campaign Microsite: Episode 1',
					desc: 'Xario Campaign Microsite: Episode 1',
					thumb: 'tn_xario1.jpg'
				},     
			{ 
				url: 'Xario_Ep2.flv', 
				title: 'Xario Campaign Microsite: Episode 2',
				desc: 'Xario Campaign Microsite: Episode 2',
				thumb: 'tn_xario2.jpg'
			},
			{ 
				url: 'Xario_Ep3.flv', 
				title: 'Xario Campaign Microsite: Episode 3',
				desc: 'Xario Campaign Microsite: Episode 3',
				thumb: 'tn_xario3.jpg'
			},     
			{ 
				url: 'Xario_Ep4.flv', 
				title: 'Xario Campaign Microsite: Episode 4',
				desc: 'Xario Campaign Microsite: Episode 4',
				thumb: 'tn_xario4.jpg'
			},
			{ 
				url: 'Xario_Ep5.flv', 
				title: 'Xario Campaign Microsite: Episode 5',
				desc: 'Xario Campaign Microsite: Episode 5',
				thumb: 'tn_xario5.jpg'
			},     
			{ 
				url: 'Xario_Ep6.flv', 
				title: 'Xario Campaign Microsite: Episode 6',
				desc: 'Xario Campaign Microsite: Episode 6',
				thumb: 'tn_xario6.jpg'
			},
			{ 
				url: 'WalkAide.flv', 
				title: 'Innovative Neurotronics: Patient Information DVD',
				desc: 'Innovative Neurotronics: Patient Information DVD',
				thumb: 'tn_walkaide.jpg'
			},
				{ 
					url: 'WalkAide_Herb.flv', 
					title: 'WalkAide Testimonial from Herb',
					desc: 'WalkAide Testimonial from Herb',
					thumb: 'tn_walkaide_herb.jpg'
				},
				{ 
					url: 'WalkAide_Maggie.flv', 
					title: 'WalkAide Testimonial from Maggie',
					desc: 'WalkAide Testimonial from Maggie',
					thumb: 'tn_walkaide_maggie.jpg'
				},
				{ 
					url: 'WalkAide_Testimonial_Peter.flv',
					title: 'WalkAide: Patient Testimonial Cable Spot - "Peter"',
					desc: 'WalkAide: Patient Testimonial Cable Spot - "Peter"',
					thumb: 'tn_walkaide_peter.jpg'
				},  
				{ 
					url: 'DPN_education.flv', 
					title: 'DeepNines: Product Demonstration',
					desc: 'DeepNines: Product Demonstration',
					thumb: 'tn_deepnines.jpg'
				},    
				{ 
					url: '3DS_english.flv', 
					title: '3D Systems: Product Announcement Demo',
					desc: '3D Systems: Product Announcement Demo',
					thumb: 'tn_3ds.jpg'
				},
				{ 
					url: 'Medsphere_Product_demo.flv', 
					title: 'Medsphere: Product Demonstration',
					desc: 'Medsphere: Product Demonstration',
					thumb: 'tn_medsphere.jpg'
				},
				
			{ 
				url: 'Averatec_6200_Athlon_web.flv', 
				title: 'Averatec: Point of Purchase Demo',
				desc: 'Averatec: Point of Purchase Demo',
				thumb: 'tn_averatec2.jpg'
			},
			{ 
				url: 'Bitfone_e2e_demo_rev06.flv', 
				title: 'Bitfone: Product Suite Demo',
				desc: 'Bitfone: Product Suite Demo',
				thumb: 'tn_bitfone.jpg'
			},
			{ 
				url: 'Amerisource_Bergen_iScan.flv', 
				title: 'AmerisourceBergen: Tradeshow product demonstration',
				desc: 'AmerisourceBergen: Tradeshow product demonstration',
				thumb: 'tn_amerisource.jpg'
			},
			{ 
				url: 'eEye.flv', 
				title: 'eEye Digital Security: Blink Vulnerability Prevention',
				desc: 'eEye Digital Security: Blink Vulnerability Prevention',
				thumb: 'tn_eeye.jpg'
			},
			{ 
				url: 'DIX_Metals_V7.flv', 
				title: 'Dix Metals: Corporate Video',
				desc: 'Dix Metals: Corporate Video',
				thumb: 'tn_dix.jpg'
			},
			{ 
				url: 'Apani_presentation.flv', 
				title: 'Apani: Corporate/Product Overview Presentation',
				desc: 'Apani: Corporate/Product Overview Presentation',
				thumb: 'tn_apani.jpg'
			},
			{ 
				url: 'Experian.flv', 
				title: 'Experian: Product Features Demo',
				desc: 'Experian: Product Features Demo',
				thumb: 'tn_experian.jpg'
			},
			{ 
				url: 'Newton_MogoHeadset.flv', 
				title: 'Newton Peripherals: MoGo Headset Intro Video',
				desc: 'Newton Peripherals: MoGo Headset Intro Video',
				thumb: 'tn_newton.jpg'
			}

		],
		// show playlist buttons in controlbar 
		plugins: { 
			controls: { 
				playlist: true,
				// location of the controlbar plugin 
				url: 'http://www.binarypulse.com/multimedia/swf/flowplayer.controls-3.1.1.swf'
			}
		} 
	}).playlist("div.items",{loop:false}).onLoad(function(){
		$("div.loadingitems").hide();
		$("div.items").show();
		if(tv > -1){
			this.play(tv);
		}
		else{
			$("a#splashpane").show();
			$f("video").hide(true);
		}
	});
	
	
	// SPLASH PANE CLICK
	$("a#splashpane").click(function(e){
		$("a#splashpane").hide();
		$f("video").show();
		$f("video").play();
		return e.preventDefault();
	});
	
	// PLAYLIST PAUSE PER VIDEO
	$("a.thevid").click( function(){
		vidclick = (vidclick == 0) ? 1 : 0;
		$("a#splashpane").hide();
		$f("video").show();
		closepane();
	});
	
	// PLAYLIST HORIZONTAL SCROLL 
	scrollApi = $("div.scrollable").scrollable({
		api: true,
		size: 6,
		loop: true,
		easing: 'swing',
		speed: 500,
		clickable: false
	});
	
	// PLAYLIST STAGGERING STRUCTURE
	var counter = 1;
	$("div.items a").each(function(){
		if(counter > 3 ){
			if(counter == 4){
				$(this).addClass("lower1");
			}
			else if(counter == 5){
				$(this).addClass("lower2");
			}
			else{
				$(this).addClass("lower3");
				counter = 0;
			}
		}
		
		counter++;
	});
	
	
	// EMAIL, URL, AND EMBED CODE
	
	
	$("a#geturl").click(function(e){
								 
		curpane = $(this);
		if(swappane("url") == 0){
			closepane();
		}
		else{
			//curindex = $f("video").getClip().index;
		
			$("input#urlbar").val( "http://www.binarypulse.com/multimedia/index.php?v="+encryptedindex );
		
			$(curpane.attr("href")).show();
		}

		// prevent link's default behaviour
		return e.preventDefault();	
	});

	$("a#getcode").click(function(e)  { 
		
		curpane = $(this);
		if(swappane("code") == 0){
			closepane();
		}
		else{
			curclip = $f("video").getClip().completeUrl;
			
			$f("video2", {src: 'http://www.binarypulse.com/multimedia/swf/flowplayer.commercial-3.1.1.swf', wmode: 'transparent'}, {
			    key: '@7999ae8571d4535cf1d',
				logo: { 
					url: 'http://www.binarypulse.com/multimedia/images/BP-logo.png', 
					fullscreenOnly: false,
					bottom: 40, 
					left: 10, 
					opacity: 0.5,
					displayTime: 1000,
					linkUrl: 'http://www.binarypulse.com/multimedia'
				},
				canvas: {
					backgroundColor: "#292929",
					backgroundGradient: 'none'
				},
				clip: {
					autoPlay:false
				},
				playlist: [{
					url: curclip,
					scaling: 'fit'
				}]
			});
			
			embedcurvid = $f("video2").embed({width: 480, height: 405}).getEmbedCode(true);
			$("div#codepane textarea").val( embedcurvid );
			$f("video2").unload();
		
			$(curpane.attr("href")).show();
		}

		// prevent link's default behaviour
		return e.preventDefault();
	});

	// embed buttons
	$("input#embedcurrent").click(function() {
		$("div#codepane textarea").val( embedcurvid );
	});
	$("input#embedall").click(function() {
		embedallvid = $f("video").embed({width: 480, height: 405}).getEmbedCode(true);

		$("div#codepane textarea").val( embedallvid.replace('config={"key":"@7999ae8571d4535cf1d",', 'config={"key":"@7999ae8571d4535cf1d","logo":{"url":"http://www.binarypulse.com/multimedia/images/BP-logo.png","fullscreenOnly":false,"bottom":40,"left":10,"opacity":0.5,"displayTime":1000,"linkUrl":"http://www.binarypulse.com/multimedia"},') );
	});
	
	$("a#emailfriend").click(function(e)  { 
									  
		curpane = $(this);
		if(swappane("email") == 0){
			closepane();
		}
		else{
			$("div#emailthank").hide();
			$("div#emailform").show();
		
			$(curpane.attr("href")).show();
		}

		// prevent link's default behaviour
		return e.preventDefault();
	});
	
	$("input#sharesubmit").click(function(e) {
										  						  
		if($(this).val() == "Share"){						  
			
			currentclip = $f("video").getClip();
			
			var name = $("#name").val();
			var email = $("#email").val();
			var femail = $("#femail").val();
			var comment = $("#comment").val();
			var title = currentclip.title;
			var desc = currentclip.desc;
			var url = "http://www.binarypulse.com/multimedia/index.php?v=" + encryptedindex;
			
			var dataString = "";
				
			if(name=='' || email=='' || femail==''){
				alert("Your Name, Your Email, and Friend's Email are required.");
			}
			
			else{
				if(comment == ""){comment = "No comments.";}
					
				dataString = 'name='+ name + '&email=' + email + '&femail=' + femail + '&comment=' + comment + '&title=' + title + '&desc=' + desc + '&url=' + url;
				
				$("#flash").html('Sending message...');
				$("div#emailform").hide();
				$("#flash").show();
				
				$.ajax({
				  type: "POST",
				  url: "ajaxemail.php",
				  data: dataString,
				  cache: false,
				  success: function(html){
					  
					  $("#flash").hide();
					  
					  document.getElementById('email').value='';
					  document.getElementById('femail').value='';
					  document.getElementById('name').value='';
					  document.getElementById('comment').value='';
					  
					  document.getElementById('emailthank').innerHTML='Message sent.<br/><br/>Thanks for sharing!';
					  $("div#emailthank").show();
					  document.getElementById('sharesubmit').value='Share Again';
				  }
				  
				});
			}	
		
		}
		else{
			$("div#emailthank").hide();
			$("div#emailform").show();
			 document.getElementById('sharesubmit').value='Share';
		}
		
		return e.preventDefault();
	});
	
	// close buttons
	$("input.close").click( function(){closepane();} );
});