      /*
       * Chromeless player has no controls.
       */
      var ytplayer;
      var $video_start = -1;
      var $cid;
      
      // Update a particular HTML element with a new value
      function updateHTML(elmId, value) {
        document.getElementById(elmId).innerHTML = value;
      }
      
      // This function is called when an error is thrown by the player
      function onPlayerError(errorCode) {
        alert("An error occured of type:" + errorCode);
      }
      
      // This function is called when the player changes state
      function onPlayerStateChange(newState) {

        updateHTML("playerState", newState);
      }
      
      // Display information about the current state of the player
      function updatePlayerInfo() {
        // Also check that at least one function exists since when IE unloads the
        // page, it will destroy the SWF before clearing the interval.
        if(ytplayer && ytplayer.getDuration) {
        
        	if((ytplayer.getDuration() == ytplayer.getCurrentTime()) && ytplayer.getCurrentTime() != 0) {
        		$.LoadVideo();
        	}
        	
        	

          //updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
          //updateHTML("startBytes", ytplayer.getVideoStartBytes());
          //updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
          //updateHTML("volume", ytplayer.getVolume());
        }

        
      }
      
      // Allow the user to set the volume from 0-100
      function setVideoVolume() {
        var volume = parseInt(document.getElementById("volumeSetting").value);
        if(isNaN(volume) || volume < 0 || volume > 100) {
          alert("Please enter a valid volume between 0 and 100.");
        }
        else if(ytplayer){
          ytplayer.setVolume(volume);
        }
      }
      
      function playVideo() {
        if (ytplayer) {
          ytplayer.playVideo();
        }
      }
      
      function pauseVideo() {
        if (ytplayer) {
          ytplayer.pauseVideo();
        }
      }
      
      function muteVideo() {
        if(ytplayer) {
          ytplayer.mute();
        }
      }
      
      function unMuteVideo() {
        if(ytplayer) {
          ytplayer.unMute();
        }
      }
      
      
      // This function is automatically called by the player once it loads
      function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("ytPlayer");
        // This causes the updatePlayerInfo function to be called every 250ms to
        // get fresh data from the player
        setInterval(updatePlayerInfo, 1000);
		$.LoadVideo();
        // ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
        //ytplayer.addEventListener("onError", "onPlayerError");
        //Load an initial video into the player
        //ytplayer.cueVideoById("X0mLvV4A3O0");
        //ytplayer.playVideo();
        //$.HideTitle();
      }
		(function($) {
		  var cache = [];
		  // Arguments are image paths relative to the current page.
		  $.preLoadImages = function() {
		    var args_len = arguments.length;
		    for (var i = args_len; i--;) {
		      var cacheImage = document.createElement('img');
		      cacheImage.src = arguments[i];
		      cache.push(cacheImage);
		    }
		  }
		})(jQuery);
		
		jQuery.preLoadImages("images/play.png");      
		
	  $.HideTitle = function()
	  {
	  	setTimeout(function() {
	  		$('#song_title').fadeOut('slow')
	  	}, 30000)	
	  }

	jQuery.cookie = function (key, value, options) {
	
	    // key and at least value given, set cookie...
	    if (arguments.length > 1 && String(value) !== "[object Object]") {
	        options = jQuery.extend({}, options);
	
	        if (value === null || value === undefined) {
	            options.expires = -1;
	        }
	
	        if (typeof options.expires === 'number') {
	            var days = options.expires, t = options.expires = new Date();
	            t.setDate(t.getDate() + days);
	        }
	
	        value = String(value);
	
	        return (document.cookie = [
	            encodeURIComponent(key), '=',
	            options.raw ? value : encodeURIComponent(value),
	            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
	            options.path ? '; path=' + options.path : '',
	            options.domain ? '; domain=' + options.domain : '',
	            options.secure ? '; secure' : ''
	        ].join(''));
	    }
	
	    // key and possibly options given, get cookie...
	    options = value || {};
	    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
	};
	  
	$.LoadVideo = function () {
		
		$.getJSON($video_list, function(data) {
		
		if($video_start == data.length) {
			$video_start = 0;
		} else {
			$video_start = $video_start+1;
		}

		$point = data[$video_start];
		$('#cid').val($point.cid);
		
		$('#song_title').html($point.artist_name+'<br /><strong>'+$point.video_title+'</strong>');
		document.title = $point.artist_name+' - '+$point.video_title+' | '+$main_site_title;
		Cufon.replace('#song_title');
		
		$('#song_title').fadeIn('fast');


		$.ajax({
		  url: "check_video.php?yt_id="+$point.api_id
		});

        ytplayer.cueVideoById($point.api_id);

        ytplayer.playVideo();        
        $.HideTitle();
        
        if(!$.cookie('voting['+$point.cid+']')) {

        	$('#side_control .heart_norm, , #side_control .heart').removeClass('heart').addClass('heart_norm');
        
        } else {
        	$('#side_control .heart_norm, #side_control .heart').removeClass('heart_norm').addClass('heart');
        }
		
		}
	)}
	  
	  
	  
      $(function($){
      		var $height = $(window).height();
      		var $width = $(window).width();
      	Cufon.replace('span, #menu, #song_title');	
      	Cufon.replace('.hour', { color : '#4f9b02'});
      	Cufon.replace('.sitemap');
      	
        // Lets Flash from another domain call JavaScript
        var params = { allowScriptAccess: "always", "wmode" : "transparent" };
        // The element id of the Flash embed
        var atts = { id: "ytPlayer", "wmode" : "transparent" };

        $('#videoDiv').css({
        	height : $height-5,
        	width : $width,
        	'overflow' : 'hidden'
        });

	    $('.input').live('focus', function(){
	        
	        var $rel = $(this).attr('rel');
	        if($(this).val() == $rel) $(this).val('');
	        
	    });
	    
	    $('.input').live('blur', function(){
	        
	        var $rel = $(this).attr('rel');
	        var $val = $(this).val();
	        if($val == $rel || !$val.length) $(this).val($rel);
	         
	    });

        
        $('#top_controls, #bottom_controls').find('a').hover(function (){
        	
        	$(this).css('color', 'lightgray');
        	Cufon.replace($(this));
        	
        }, function (){
        	
        	$(this).css('color', '#fff');
        	Cufon.replace($(this));        	
        	
        });
        
        $('#side_control, #song_title').css({
        	bottom: ($height*0.4)
        })
        
		var $items;

        swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                           "version=3&enablejsapi=1&playerapiid=videoDiv", 
                           "videoDiv", $width, $height-5, "9", null, null, params, atts);
                                
              
        $('#side_control .pause').live('click', function (event){
        	event.preventDefault();
        	ytplayer.pauseVideo();
        	$(this).toggleClass('pause play');
        });
        
        $('#side_control .play').live('click', function (event){
        	event.preventDefault();
        	ytplayer.playVideo();
        	$(this).toggleClass('pause play');
        });
        
        $('#side_control .heart').live('click', function (event){
        	event.preventDefault()
        })
                
        
        $('#side_control .heart_norm').live('click', function (event){
			
			event.preventDefault();
				
			$cid = $('#cid').val();
			
			if(!$.cookie('voting['+$cid+']')) {
			
				
				$.ajax({
				   type: "POST",
				   url: "vote.php",
				   data: "cid="+$cid,
				   success : function () { $.cookie('voting['+$cid+']', "1", { expires: 30 }); $('#side_control .heart_norm').removeClass('heart_norm').addClass('heart'); }
				 });
			
			}
			
			setTimeout(function() {
				$('#vote_notify').fadeOut('slow', function(){
					$(this).remove()
				})
			}, 5000);
			
        });        
        
        $('#side_control .forward').live('click', function(event){
        	event.preventDefault();
        	$('#side_control .play, #side_control .pause').removeClass('play').addClass('pause');
            $.LoadVideo();

        });
        
        $.AddLevitation =  function () {
        	
        	$('body .levitation').remove();
        	
			$width = $(window).width();
			$height = $(window).height();
			
			$top = ($height * 0.3)/2;
			$left = ($width * 0.4)/2;
			
        	$('body').append(
        		$('<div></div>')
        		.addClass('levitation')
        		.css({
        			top : $top,
        			left : $left,
        			height : ($height * 0.7)+'px'
        		})
        		.fadeTo(0, 0.9)
        		.append(
        			$('<a class="exit" href="#"></a>')
        		)
        		.append(
        			$('<div></div>')
        			.addClass('html')
        		)
        	);

        	
        }
        

        $('.sponsorship').live('click', function (event){
        	
        	event.preventDefault();
        	$.AddLevitation();
        	
			$.ajax({
			  url: 'sponsorship.html',
			  success : function (html){

			  	$('.levitation .html')
			  	.fadeTo(0, 1)
			  	.css({
			  		height : $('.levitation').height()-80
			  	})
			  	.append(html);
			  	
        	$('.levitation .html input, .levitation .html textarea')
        	.css({
        		width : $('.levitation').width()-100
        	});
        	
        	$('.levitation .html .small')
        	.css({
        		width : ($('.levitation').width()-80-60-8)/3,
        		
        	})
        	
        	$('.levitation .html .small:lt(2)')
        	.css({
        		'margin-right' : '10px'
        	})
        	
        	$('.levitation .html .small:last')
        	.css('float', 'right');
        	
			Cufon.replace('.levitation .html');  	
			$('#spons_msg').limit('144', '#charsLeft');
			 
			  }
			});
        	
        });

		var $resp = 0;
		
        $('#video_submit_btn').live('click', function (event){
        	
        		event.preventDefault();
        		
        		var $video_url = $('#vs_url').val();
        		var $artist_name = $('#vs_artistname').val();
        		var $track_title = $('#vs_tracktitle').val();


        		
				$.ajax({
				   type: "POST",
				   url: "video_submit.php",
				   data: "yt_id="+encodeURIComponent($video_url)+'&artist_name='+encodeURIComponent($artist_name)+'&track_title='+encodeURIComponent($track_title),
				   success : function (response) { 

				   	if (response == 1) {
				   		$('#vs_response').css('color', '#fff').html('Thank you for submiting. This video will be reviewed by our staff for approval.');
				   		$('.levitation .html input').css({ 'border' : '0' });
				   		$resp = 0;
				   	} else {
				   		$len = $('.levitation .html input').width();
				   		$('#vs_response').css('color', 'red').html('This video URL already exists in our database.<br />Please add another YouTube video URL.');

				   		if($resp != 1)
				   		{
				   			$('.levitation .html input').css({ 'border' : '2px solid red', 'width' : $len-4 });
				   			
				   		}
				   		
				   		$resp = 1;
				   	}
				   	
				   	Cufon.replace('#vs_response');
				   	
				   }
				 });  	
        	
        });

		// sponsorship submit
        $('#spons_complete').live('click', function (event){

        		event.preventDefault();
        		
        		var $name = $('#name').val();
        		var $email = $('#email').val();
        		var $qt = $('#qt').val();
        		var $msg = $('#spons_msg').val();
        		var $promo = $('#promo_url').val();

				$len = $('.levitation .html input').width();
				
		    	if($name.length == 0) {
		    		
		    		$('#name').css({ 'border' : '2px solid red', 'width' : $len-4 });
					return false;
		    		
		    	}
		    	if( !isValidEmailAddress( $email ) ) {
		    		
		    		$('#email').css({ 'border' : '2px solid red', 'width' : $len-4 });
					return false;
		    	}
		    	if($qt == 0) {
		    		
		    		$('#qt').css({ 'border' : '2px solid red', 'width' : $len-4 });
					return false;
		    	}
		    	if($msg.length == 0) {
		    		
		    		$('#spons_msg').css({ 'border' : '2px solid red', 'width' : $len-4 });
					return false;
		    	}
		    	if($promo.length == 0) {
		    		
		    		$('#promo_url').css({ 'border' : '2px solid red', 'width' : $len-4 });
					return false;
		    	}
        		
				$.ajax({
				   type: "POST",
				   url: "add_sponsor.php",
				   data: "name="+encodeURIComponent($name)+'&email='+encodeURIComponent($email)+'&qt='+encodeURIComponent($qt)+'&msg='+$msg+'&promo='+$promo,
				   success : function (response) { 

					response = $.parseJSON(response);

				   	if (response.result == 1) {
				   		
				   		location.href = 'purchase.php?id='+response.last_id;
				   		
				   	} else {
				   		$len = $('.levitation .html input').width();
				   		$('#vs_response').css('color', 'red').html('Please fill out all the fields.');


				   	}
				   	
				   	Cufon.replace('#vs_response');
				   	
				   }
				 });  	
        	
        });

        
		function isValidEmailAddress(emailAddress) {
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			return pattern.test(emailAddress);
		}; 
		
		var $c_resp = 0;
		
        $('#contact_submit_btn').live('click', function (){
        	
        		var $name = $('#contact_name').val();
        		var $email = $('#contact_email').val();
        		var $text = $('#contact_text').val();
        		
		$len = $('.levitation .html input').width();
		
    	if($name.length == 0) {
    		
    		$('#contact_name').css({ 'border' : '2px solid red', 'width' : $len-4 });
    		return false;
    		
    	}
    	if( !isValidEmailAddress( $email ) ) {
    		
    		$('#contact_email').css({ 'border' : '2px solid red', 'width' : $len-4 });
    		return false;
    	}
    	if($text.length == 0) {
    		
    		$('#contact_text').css({ 'border' : '2px solid red', 'width' : $len-4 });
    		return false;
    	}
    	
    	setTimeout(function (){
    		
    		$('#contact_email, #contact_name, #contact_text').css({ 'border' : 0, 'width' : $len});
    		
    		
    	}, 4000);
    	
    
				$.ajax({
				   type: "POST",
				   url: "contact.php",
				   data: 'name='+encodeURIComponent($name)+'&email='+encodeURIComponent($email)+'&text='+encodeURIComponent($text),
				   success : function (response) { 

				   	if (response == 1) {
				   		
				   		$('#contact_response').html('Thank you. We will respond as soon as possible.');
				   		
				   	}
				   	
				   	Cufon.replace('#contact_response');
				   	
				   }
				 });
        });
        
        
        $('#spons_msg').live('keyup', function (){
        	$('#spons_msg_preview').val($(this).val());
        });

	     $.fn.extend({  
	         limit: function(limit,element) {
				
				var interval, f;
				var self = $(this);
						
				$(this).focus(function(){
					interval = window.setInterval(substring,100);
				});
				
				$(this).blur(function(){
					clearInterval(interval);
					substring();
				});
				
				substringFunction = "function substring(){ var val = $(self).val();var length = val.length;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
				if(typeof element != 'undefined')
					substringFunction += "if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);} Cufon.replace('#charsLeft');"
					
				substringFunction += "}";
				
				eval(substringFunction);
				
				
				
				substring();
				
	        } 
	    });
        
        $('#video_submit').live('click', function (event){
        	
        	event.preventDefault();
        	$.AddLevitation();
        	
			$.ajax({
			  url: 'submit_video.html',
			  success : function (html){

			  	$('.levitation .html')
			  	.fadeTo(0, 1)
			  	.css({
			  		height : $('.levitation').height()-80
			  	})
			  	.append(html);
			  	
        	$('.levitation .html input')
        	.css({
        		width : $('.levitation').width()-100
        	});
        	
			Cufon.replace('.levitation .html');  	
			 
			  }
			});
        	
        });

        $('#about').live('click', function (event){
        	
        	event.preventDefault();
        	$.AddLevitation();
        	
			$.ajax({
			  url: 'about.html',
			  success : function (html){

			  	$('.levitation .html')
			  	.fadeTo(0, 1)
			  	.css({
			  		height : $('.levitation').height()-80
			  	})
			  	.append(html);
			  	
        	$('.levitation .html input')
        	.css({
        		width : $('.levitation').width()-100
        	});
        	
			Cufon.replace('.levitation .html');  	
			 
			  }
			});
        	
        });


        $('#contact').live('click', function (event){
        	
        	event.preventDefault();
        	$.AddLevitation();
        	
			$.ajax({
			  url: 'contact.html',
			  success : function (html){

			  	$('.levitation .html')
			  	.fadeTo(0, 1)
			  	.css({
			  		height : $('.levitation').height()-80
			  	})
			  	.append(html);
			  	
        	$('.levitation .html input, .levitation .html textarea')
        	.css({
        		width : $('.levitation').width()-100
        	});
        	
			Cufon.replace('.levitation .html');  	
			 
			  }
			});
        	
        });

        $('#topvideo').live('click', function (event){
        	
        	event.preventDefault();
        	$.AddLevitation();
        	
			$.ajax({
			  url: 'topvideo.php',
			  success : function (html){

			  	$('.levitation .html')
			  	.fadeTo(0, 1)
			  	.css({
			  		height : $('.levitation').height()-80
			  	})
			  	.append(html);
			  	
        	$('.levitation .html input')
        	.css({
        		width : $('.levitation').width()-100
        	});
        	
			Cufon.replace('.levitation .html');  	
			 
			  }
			});
        	
        });

        $('#sitemap').live('click', function (event){
        	
        	event.preventDefault();
        	$.AddLevitation();
        	
			$.ajax({
			  url: 'sitemap.php',
			  success : function (html){

			  	$('.levitation .html')
			  	.fadeTo(0, 1)
			  	.css({
			  		height : $('.levitation').height()-80
			  	})
			  	.append(html);
			  	
        	$('.levitation .html input')
        	.css({
        		width : $('.levitation').width()-100
        	});
        	
			Cufon.replace('.levitation .html');  	
			 
			  }
			});
        	
        });
        
        $('.levitation .exit').live('click', function (event){
        	event.preventDefault();
        	$(this).parent().fadeOut('fast', function (){
        		$(this).remove();
        	})
        });
                           
      });
