$(document).ready(function()
{
	//scroll the message box to the top offset of browser's scrool bar
	$(window).scroll(function()
	{
  		$('#player').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350}); 

	});
    //when the close button at right corner of the message box is clicked 
	$('#close_message').click(function()
	{
  		//the messagebox gets scrool down with top property and gets hidden with zero opacity 
		$('#player').animate({ top:"+=15px",opacity:0 }, "slow");
	});
});

var qTipTag = "a,label,input"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipX = 0; //This is qTip's X offset//
var qTipY = 15; //This is qTip's Y offset//

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};

	var a, sTitle, elements;
	
	var elementList = qTipTag.split(",");
	for(var j = 0; j < elementList.length; j++)
	{	
		elements = document.getElementsByTagName(elementList[j]);
		if(elements)
		{
			for (var i = 0; i < elements.length; i ++)
			{
				a = elements[i];
				sTitle = a.getAttribute("title");				
				if(sTitle)
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function() {tooltip.hide()};
				}
			}
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}

function closeWindow(url,post)

{

      $(".overlay").fadeOut('slow', function() { $(".overlay").remove(); });  
	  $.post(post, { active: "no" },
  function(data){

  });
	  

}

function loadWindow( url, width, height, post, newpost )

{

	var yes = $(".overlay").attr('title');

	if(yes!='yes')
	{

	$("body").prepend("<div class='overlay' title='yes'><div class='overlay_top'></div><div class='overlay_mid'><div id='source'></div></div><div class='overlay_bot'></div></div>");	
	
	

	

	$(".overlay").fadeIn('slow');
	
	$("#source").load(url);
	
	}

	

    $(".overlay").click(function () { 

								  

	    

      

    });

}



function displayAd(url, post, newpost)
{
	var overlay_title = $(".overlay").attr('title');
	var global = $("#global").attr('title');
	
	if(newpost=='yes')
	  {
		  loadWindow(url,'500','400');
		
	  }
	  else
	  {
	


		setInterval(

function(data){
		
	  if(newpost=='yes')
	  {
		  loadWindow(url,'500','400');
		
	  }
	  else
	  {

		
	if(overlay_title=="yes")
	{
	    
	}
	else
	{
		   loadWindow(url,'500','400',post); 
		   $.post(post, { active: "yes" },
  function(data){
  });
		   
	}
		   
		}
		}
							 ,500000);  }
	}

		  

	
	


	

		
		function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById("myytplayer");
          setInterval(updateytplayerInfo, 250);
          updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          ytplayer.addEventListener("onError", "onPlayerError");
        }

 
 $(document).ready(function(){
    $("#slider").slider({ 
	
	min: 0,
	max: 100,
	animate: true,
		
	change: function() { var value = $('#slider').slider('option', 'value');
	
	seekIt(value);
	
	
	}	
						
	});
  });

function updateHTML(elmId, value) {

        }

        function setytplayerState(newState) {
          if(newState=='2')
		  {
			  $(".play").html('<a href="javascript:void(0)" onClick="play();"><img src="http://i36.tinypic.com/2qir3wp.png" /></a>');
		  }
		  else
		  {
			  if(newState=='1')
			  {
				  $(".play").html('<a href="javascript:void(0)" onClick="pause();"><img src="http://i38.tinypic.com/9tke1j.png" /></a>');
			  }
			  else
			  {
				  if(newState=='-1')
				  {
					 $(".play").html('<a href="javascript:void(0)" onClick="play();"><img src="http://i36.tinypic.com/2qir3wp.png" /></a>');
				  }
			  }
		  }
        }
		
		function songSearch(url)
		{
		var searchitem = $("#searchit").val();
		if(searchitem=='')
		{
			var newsearch = 'Blank';
		}
		else
		{
			var newsearch = searchitem;
		}
		$.get(url+'/'+newsearch, function(data){
  $("#videos2").html(data);
});
		}

        function onytplayerStateChange(newState) {
          setytplayerState(newState);
        }

        function onPlayerError(errorCode) {
          alert("An error occured: " + errorCode);
        }

        function updateytplayerInfo() {
          updateHTML("bytesloaded", getBytesLoaded());
          updateHTML("bytestotal", getBytesTotal());
          updateHTML("videoduration", getDuration());
          updateHTML("videotime", getCurrentTime());
          updateHTML("startbytes", getStartBytes());
          updateHTML("volume", getVolume());
        }

        // functions for the api calls
        function loadNewVideo(id, startSeconds, current, duration, title) {
          if (ytplayer) {
            ytplayer.loadVideoById(id, parseInt(startSeconds));
			$("#current").html(current);
			
				$('#slider').slider('option', 'max', duration);
				
				$(".songtitle").fadeOut('slow');
				$(".songtitle").html(title);
				$(".songtitle").fadeIn('slow');
				
				
	
	var t = setInterval(function(){ $('#slider').slider('option', 'value', ytplayer.getCurrentTime()); }, 200);
          }
        }
		
		function next()
		{
			var current = $("#current").html();
			var newcurrent = ++current;
			var title = $("."+current).attr('title');
			var url = $("."+current).attr('url');
			var duration = $("."+current).attr('duration');
			
			loadNewVideo(url,0,newcurrent,duration,title);
		}
		
		function backit()
		{
			var current = $("#current").html();
			var newcurrent = --current;
			var title = $("."+newcurrent).attr('title');
			var url = $("."+newcurrent).attr('url');
			var duration = $("."+current).attr('duration');
			
			loadNewVideo(url,0,newcurrent,duration,title);
		}
		
		function seekIt(seconds) {
	    
		ytplayer.seekTo(seconds, 3);
	 
         }

        function cueNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.cueVideoById(id, startSeconds);
          }
        }

        function play() {
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }

        function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
          }
        }

        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
          }
        }

        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getPlayerState();
          }
        }

        function seekTo(seconds) {
          if (ytplayer) {
            ytplayer.seekTo(seconds, true);
          }
        }

        function getBytesLoaded() {
          if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
          }
        }

        function getBytesTotal() {
          if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
          }
        }

        function getCurrentTime() {
          if (ytplayer) {
            return ytplayer.getCurrentTime();
          }
        }

        function getDuration() {
          if (ytplayer) {
            return ytplayer.getDuration();
          }
        }

        function getStartBytes() {
          if (ytplayer) {
            return ytplayer.getVideoStartBytes();
          }
        }

        function mute() {
          if (ytplayer) {
            ytplayer.mute();
          }
        }

        function unMute() {
          if (ytplayer) {
            ytplayer.unMute();
          }
        }
        
        function getEmbedCode() {
          alert(ytplayer.getVideoEmbedCode());
        }

        function getVideoUrl() {
          alert(ytplayer.getVideoUrl());
        }
        
        function setVolume(newVolume) {
          if (ytplayer) {
            ytplayer.setVolume(newVolume);
          }
        }

        function getVolume() {
          if (ytplayer) {
            return ytplayer.getVolume();
          }
        }

        function clearVideo() {
          if (ytplayer) {
            ytplayer.clearVideo();
          }
        }
		
function showMyVideos2(data) {
  var feed = data.feed;
  var entries = feed.entry || [];
  var html = [''];
  for (var i = 0; i < entries.length; i++) {
    var entry = entries[i];
    var new1 = entry.title.$t.substr(0, 50);
	var new2 = new1.replace('"','');
	var new3 = new2.replace('"','');
	var new4 = new3.replace("'","");
	var new5 = new4.replace("'","");
	var new6 = new5.replace('!','');
	var new7 = new6.replace('%','');
	var new8 = new7.replace('-','');
	var title = new8;
    var thumbnailUrl = entries[i].media$group.media$thumbnail[0].url;
	var str1 = entries[i].media$group.media$content[0].url;
	var str2 = str1.replace('http://www.youtube.com/v/',"");
	var str3 = str2.replace('?f=videos&app=youtube_gdata',"");
	var duration = entries[i].media$group.yt$duration.seconds;
	var playerUrl = str3;
	if(i == 0 )
	{
		var type = "row_top";
	}
	else{
    if(i % 2 == 0){ 
       var type = "row_norm"; 
     }else{ 
       var type = "row_blue"; 
     }    
	}
    html.push('<div title="',title,'" url="',playerUrl,'" duration="',duration,'" class="',i,'" id="',type,'" onclick="loadNewVideo(\'', playerUrl, '\', 0 , \'',i,'\',\'',duration,'\',\'',title,'\')">',
              '<span id="song"><img src="', 
              thumbnailUrl, '" height="27" width="44" />', title, '...</span><span id="time">',duration,'</span><span id="rating">Rating</span><span id="options"><a href="http://www.mooplo.com/index.php/playlist/share/',playerUrl,'/',duration,'">Share</a> | <a href="http://www.mooplo.com/index.php/playlist/add/',title,'/',playerUrl,'/',duration,'">Add To Playlist</a></span>', '</span></div>');
  }
  html.push('');
  document.getElementById('videos2').innerHTML = html.join('');
}

