		
		var ua = navigator.userAgent;
		var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
			function addslashes(str) {
					// http://kevin.vanzonneveld.net
					// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
					// + improved by: Ates Goral (http://magnetiq.com)
					// + improved by: marrtins
					// + improved by: Nate
					// + improved by: Onno Marsman
					// * example 1: addslashes("kevin's birthday");
					// * returns 1: 'kevin\'s birthday'
						return (str + '').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
		} 	
		
		 function validate(f) {
      var required = { client: "Client", amount: "Amount" };
      return checkRequiredFields(f, required) &&
             checkAmountField(f, 'amount', 'Amount', 1000, "$1,000");
    }
    
    
    function validateReservePip(formData, jqForm, options){
    	
    	
    	var valid =  $("#pip_reserve_form").valid();
    	
    	
    	
    	if(valid){
    		
    	
    								
			$("#btnPipSubmit").val('please wait...');
			$("#btnPipSubmit").attr('disabled', 'disabled');
					
			
    	}
    	   
    	return $("#pip_reserve_form").valid();
    	
    	
    }
    

    function processReservePip(data){
    	
    	// alert(data.action + " " + data.id);
    	
    	
    	if(data.action == "reserve"){
    		if(data.id > 0){
    			
    			GetPipPopupContent(data.spotid);
    		
    			// var html ="";
    				 // html += "<h1>Pitch activated!!!</h1>";
					 // html += "<p>Click <a href='javascript:GetPipPopupContent(" + data.spotid + ")'>here</a> to Snap-Spec It!<\/p>";
					 // html += "<br \/>";
					 // html += "<p>";
					 // $("#pip_popup_content").html(html);
    				
    		}
    	}
    	
    		if(data.action == "release"){
    		
    		alert(data.id);
    	}
    	
    		if(data.action == "snap_spec"){
    	
    			if(data.id > 0){
    				
    				var html ="";
    				 html += "<h1>NICE SNAPPING!!!</h1>";
					 html += "<p>We\'ll deliver it to you within 4 business hours.<\/p>";
					 html += "<br \/>";
					 html += "Thanks.";
					 html += "<p>";
					 html += "<img src=\'../images/logo.gif\' alt=\'Cash By Creative Logo\' \/>";
					 html += "</p>";
					 html += "<a href=\"javascript:$.fancybox.close()\">Close window<\/a>";
    			
    			$("#pip_popup_content").html(html);
    			}
    	
    	}
    	
    }
    
    function validateSnapSpec(formData, jqForm, options){
    	
    	
    	var valid =  $("#snap_spec_form").valid();
    	
    	
    	
    	if(valid){
    		
    	
    								
			$("#submitsnapspec").val('please wait...');
			$("#submitsnapspec").attr('disabled', 'disabled');
					
			
    	}
    	
    	return $("#snap_spec_form").valid();
    	
    }
    
   
		/**
   * Check the length of a text control and display count in a span
   * pass jquery input object and span id
   * Length is pulled from maxlength text box property
   */
   function checklength(control, label, buttonname){
	    	
	    var textl = $(control).val();
	    		
	    var max = $(control).attr("maxlength");
	    var len = textl.length;
	    $("#" + label).html((max - textl.length) + " characters left.");
	    
	 	if(len > max){
	 		
	 		$("#"+buttonname).attr('disabled', 'disabled');
	 		$("#"+buttonname).val('150 max characters');
	 	}
	 	else{
	 		$("#"+buttonname).removeAttr('disabled');
	 		$("#"+buttonname).val('submit');
	 	}
	 
	 }
		
		
		/**
		 * Track HTML5 audio play events
		 */
		function trackPlay(player){
			
					
					var spotid = $(player).attr('id').replace('audio5','');
					trackMediaPlay(spotid, "audioexample-ipad");
					
					
		}
		
		function GetFancyBoxHeight(spotid){
			
			  $.ajax({
									type : 'POST',
									async: false,
									url : '/lib/snap_spec_check_for_pip.php',
									dataType : 'text',
									data: {
										
										spotid : spotid
										
									},
									success : function(data){
										
										return data;
									},
									error : function(XMLHttpRequest, textStatus, errorThrown) {
									
										return 150;
				
									}
								});	
		}
		
		/**
		 * Insert new media track
		 */
		function trackMediaPlay(spotid, mediaType){
			
			//track user play
							  $.ajax({
									type : 'POST',
									url : '/lib/track_preview.php',
									dataType : 'text',
									data: {
										
										spotid : spotid,
										mediatype : mediaType
										
									},
									success : function(data){
				
									},
									error : function(XMLHttpRequest, textStatus, errorThrown) {
									
										$.jGrowl(errorThrown);
				
									}
								});
			
		}
		
		function GetPipPopupContent(spotid){
			
			$("#pip_popup_content").html('<h2>Please wait...loading</h2>');
			
			$.fancybox({
					 maxWidth	: 700,
					 maxHeight	: 550,
					 
					fitToView	: false,
					autoSize	: true,
					closeClick	: false,
					openEffect	: 'elastic',
					closeEffect	: 'elastic',
					scrolling	: 'no',
					href		: '#pip_popup'
							
  
						});
			
			$.ajax({
									type : 'POST',
									async: true,
									url : 'lib/pip_popup_content.php',
									dataType : 'text',
									data: {
										
										spotid : spotid
										
									},
									success : function(data){
										
										
										$("#pip_popup_content").html(data);
									
										$("a.downloadpopupicon").cluetip(
											{
												splitTitle: '|',
												showTitle: false,
											  	cluezIndex: 1005,
											  	cluetipClass: "nicetitle"
											  });
											  
									 
											  
										$("#spotid").val(spotid);
										
										
											
												var pip_reserve_options = { 
											         
											        beforeSubmit:	validateReservePip,
											        dataType:		'json',
											         success:		processReservePip
											        
											    };
											    
											     
    											$('#pip_reserve_form').ajaxForm(pip_reserve_options); 
														
												
												  $("#pip_reserve_form").validate({
															
															rules:{
																
																client: "required",
																amount: {
																	required: true,
																	min: 1000
																
																}
															},
															messages: {
															
																client: "required",
																amount: "<br />number greater than 1000"	
															}
															
														}); 
										
										
										
										
											
											var snap_spec_options = { 
											         
											        beforeSubmit:	validateSnapSpec,
											        dataType:		'json',
											        success:		processReservePip   
											    };
											    
											     
    											$('#snap_spec_form').ajaxForm(snap_spec_options); 
														
												
												  $("#snap_spec_form").validate({
															
															rules:{
																
																client_name: "required",
																message: {
																	required: true,
																	maxlength: 150
																
																}
															},
															messages: {
																
																client_name: "<br />required"
																
															}
															
														}); 
														
														
														
														$.fancybox.close();
											
										$.fancybox({
												 maxWidth	: 700,
												 maxHeight	: 550,
												 
												fitToView	: false,
												autoSize	: true,
												closeClick	: false,
												openEffect	: 'elastic',
												closeEffect	: 'elastic',
												scrolling	: 'no',
												href		: '#pip_popup'
														
							  
													});
								
										
											
									},
									error : function(XMLHttpRequest, textStatus, errorThrown) {
									
										
				
									}
								});	
					
		}
		
		$(document).ready(function(){
			
			//bind to sample download
			$(".downloadsample").click(function(ev){
				
				
				if(ev.preventDefault){ev.preventDefault();}
				//track download
				
				
				trackMediaPlay($(this).attr('rel'), "sample");
				
				//location.href =  $(this).attr('href');
				
				return false;
			})
					
			$(".hovertext").cluetip(
				{
					splitTitle: '|',
					showTitle: false,
				  	cluezIndex: 1005,
				  	cluetipClass: "nicetitle",
				  	dropShadow: false
				  });		 
			
			
			//hook up play tracking to html5 audio tags
			//$("audio").each(function(){
				
		//		var pla = document.getElementById($(this).attr('id'));
				
				
			//		pla.addEventListener('play', trackPlay);
				
		//	});
			
			
			$("#btnSubmitVideoRequest").click(function(){
				
				var spotid = $("#prerollSpotId").val();
				var message = $("#txtVideocontact").val();
				
				if(message == ''){alert('please enter message text'); return false;}
				
				  
				 
				  $.ajax({
									type : 'POST',
									url : '/lib/request_custom_preroll.php',
									dataType : 'text',
									data: {
										
										spotid : spotid,
										message: message
										
										
									},
									success : function(data){
										
										
									if(data=="1"){
											$.jGrowl('Submitted request to Cash By Creative.');
					
											
											$("#prerollform").slideUp();
											$("#prerollconfirm").slideDown();
											$("#txtVideocontact").val('');
									
									} else{
										
										alert('error submitting request');
									}
										
									},
									error : function(XMLHttpRequest, textStatus, errorThrown) {
									
										$.jGrowl(textStatus);
				
									}
								});		
				
				
				
			});
		
				$(".snapspec").click(function(ev){
					
					ev.preventDefault();
					var spotid = $(this).attr('rel');
					var snapspec = $(this);
					
					
						GetPipPopupContent(spotid);
					
					
					 
					
						return false;
					
					
				});
					
					
						
			$(".prerollipad").click(function(ev){
						
						ev.preventDefault();
						
						$("#prerollform").show();
						$("#prerollconfirm").hide();
						
						var videourl = $(this).attr('href');
						
						var title = $(this).attr('rel');
						
						var spotid = $(this).attr('spotid');
						
						$("#prerollSpotId").val(spotid);
						
						
						var html5video = "";
						// html5video +='<video width="320" height="240" controls="controls">';
   						// html5video +='<source src="'+ videourl +'" type="video/mp4" />';
   						// html5video +='Your browser does not support the video tag.';
	 					// html5video +='<\/video>'; 
									

					 	html5video +=' <div class="video-js-box">';
					    html5video +='<!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->';
					    html5video +='<video id="example_video_1" class="video-js" width="320" height="240" controls="controls" preload="auto" poster="http://video-js.zencoder.com/oceans-clip.png">';
					    html5video +='<source src="' + videourl + '" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />';
						html5video +='';      
					    html5video +='<!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->';
					    html5video +='<object id="flash_fallback_1" class="vjs-flash-fallback" width="320" height="240" type="application/x-shockwave-flash"';
					    html5video +='data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">';
					    html5video +='<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />';
					    html5video +='<param name="allowfullscreen" value="true" />';
					    html5video +='<param name="flashvars" value=\'config={"playlist":["http://video-js.zencoder.com/oceans-clip.png", {"url": "'+videourl+'","autoPlay":true,"autoBuffering":true}]}\' />';
					    html5video +='</object>';
					    html5video +='</video>';
					    html5video +='<!-- Download links provided for devices that can\'t play video in the browser. -->';
					    html5video +='<p class="vjs-no-video"><strong>Download Video:</strong>';
					    html5video +='<a href="' + videourl + '">Download Video</a>,';
					    html5video +='<!-- Support VideoJS by keeping this link. -->';
					    html5video +='<a href="http://videojs.com">HTML5 Video Player</a> by VideoJS';
					    html5video +='</p>';
						html5video +='</div>';
					  	html5video +='<!-- End VideoJS -->';
						
						$("#prerollplayer").html(html5video);
						 
						
						 
						
									$.fancybox({
										maxWidth	: 440,
										maxHeight	: 550,
										fitToView	: true,
										autoSize	: true,
										closeClick	: false,
										openEffect	: 'elastic',
										closeEffect	: 'elastic',
										//title		: title,
										href		: '#prerolldialog'
										
									});
									
						//track video play			
						  //record play
							  trackMediaPlay(spotid, "pre-roll");		
						return false;
						
					});


					$(".preroll").click(function(ev){
						
						ev.preventDefault();
						
						$("#prerollform").show();
						$("#prerollconfirm").hide();
						
						var videourl = $(this).attr('href');
						
						var downloadurl = $(this).attr('download');
						$("#videodownload").attr('href', downloadurl);
						
						var mp4url = videourl.replace(".flv", ".mp4");
						
						var title = $(this).attr('rel');
						
						var spotid = $(this).attr('spotid');
						
						$("#prerollSpotId").val(spotid);
						
						var videohtml = '';
						
				
								videohtml += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='340' height='260' id='single1' name='single1'>";
											videohtml += "<param name='movie' value='/swf/flv-player.swf'> ";
											videohtml += "<param name='allowfullscreen' value='true'> ";
											videohtml += "<param name='allowscriptaccess' value='always'> ";
											videohtml += "<param name='wmode' value='transparent'> ";
											videohtml += "<param name='flashvars' value='file=" + videourl + "&autostart=true'> ";
											videohtml += "<embed ";
											videohtml += "type='application/x-shockwave-flash' ";
											videohtml += "id='single2' ";
											videohtml += "name='single2' ";
											videohtml += "src='/swf/flv-player.swf' ";
											videohtml += "width='340' ";
											videohtml += "height='260' ";
											
											videohtml += "allowscriptaccess='always' ";
											videohtml += "allowfullscreen='true' ";
											videohtml += "wmode='transparent' ";
											videohtml += "flashvars='file=" + videourl + "&autostart=true' ";
											videohtml += "\/>";
									videohtml += "<\/object>";

						var html5video = "";
						html5video +='<video width="320" height="240" controls="controls">';
   						html5video +='<source src="'+ mp4url +'" type="video/mp4" />';
   						html5video +='Your browser does not support the video tag.';
	 					html5video +='<\/video>'; 
									
						if(isiPad){
								$("#prerollplayer").html(html5video);
						} else{
						 
						 $("#prerolldialog > h1").html(title);
						 
						 $("#prerollplayer").html(videohtml);
						 
						 }
									$.fancybox({
										maxWidth	: 440,
										maxHeight	: 600,
										fitToView	: true,
										autoSize	: true,
										closeClick	: false,
										openEffect	: 'elastic',
										closeEffect	: 'elastic',
									//	title		: title,
										href		: '#prerolldialog'
										
									});
									
						//track video play			
						  //record play
							  trackMediaPlay(spotid, "pre-roll");		
						return false;
						
					});
					
					$(".audiopreview").click(function(ev){
						
						ev.preventDefault();
						
						var audiourl = $(this).attr('href');
						var audiotitle = $(this).attr('title');
						var spotid = $(this).attr('spotid');
						
						 var audiohtml = '';
							  audiohtml +=	'<object type="application/x-shockwave-flash" data="/assets/dewplayer/dewplayer-mini.swf?mp3=' + audiourl + '&autostart=1&javascript=on" width="40" height="20" class="dewplayermini" id="dewplayer-mini-'+audiourl+'">';
							  audiohtml +=	'	<param name="wmode" value="transparent" />';
							  audiohtml +=	'	<param name="movie" value="/assets/dewplayer/dewplayer-mini.swf?mp3=' + audiourl + '&autostart=1&javascript=on" \/>';
							  audiohtml +=	'<\/object>';
									  
								//stop any currently players
							  $(".dewplayermini").each(function(){
							  	
							  			var dewp = document.getElementById($(this).attr('id'));
							  			 if(dewp!=null) dewp.dewstop();
							  
							  });
							  
							  							  		 
							//show audio player
							$(this).text('').after(audiohtml);
										 
							  //track user play
							 trackMediaPlay(spotid, "audioexample");
								
						
						return false;
						
					});
				
			});
				

