// JavaScript Document

// for voting system
var votes = new Array();
var sl_video = '';


//function to add vote for video via ajax.
function addVote(id)
{
	if(sl_video == id)
	{
		str = $("#sl_vote").html();
		if(str.charAt(0) == '<')
			$("#sl_vote").html("Vote Added");
		else
			$("#sl_vote").html("Already Voted");
	}
	votes[votes.length] = id;
    $.get("add_vote.php", { id_video: id },
		  function(data){
	  		$("#vote_"+id).html(data);
	  		$("#pvote_"+id).html(data);
	});
}


//function to search when user clicks on any topics
function searchTopic(a)
{
	$("#search").val(a.title);
	document.searchForm.submit();
}


function playVideo(id,viewCount)
{
	window.scroll(0,0);
	sl_video = id;
	$("#main_video_window").html('<object width="360" id="video_object" height="240" data="http://www.youtube.com/v/'+id+'?autoplay=1" type="application/x-shockwave-flash"><param name="src" id="video_param" value="http://www.youtube.com/v/'+id+'?autoplay=1" /></object>');
	$("#sl_heading").html('Now Playing:');
	$("#sl_title").html($("#title_"+id).html());
	$("#sl_view").html("Views: "+viewCount);
	var str = $("#vote_"+id).html();
	if(str.charAt(0) == '<')
		$("#sl_vote").html('<img src="images/vote_sl.gif" onclick="this.parentNode.innerHTML=\'Vote Added\';addVote(\''+id+'\');" border=0 />');
	else
		$("#sl_vote").html('Already Voted.');
	
	$.get("videodetails.php", { id_video: id },
			  function(data){
		  		$("#sl_desc").html(create_urls(data));
			  });
}

function pageselectCallback(page_index, jq) {
	
	var i = 0;
	for(i=0;i<votes.length;i++)
	{	
		$("#vote_"+votes[i]).html('Already Voted');
	}
	
	  var x = 12;
	  var start_id = page_index * x;
	  var end_id = start_id + x;
	  var new_content = '';
	  $('#videos').empty();
	  for(i=start_id; i<end_id; i++)
	  {
	    new_content = $('#video_dummy div.featured_topic_video:eq('+i+')').clone();
	    $('#videos').append(new_content);
	  }
	  
}

//for opening new window for center search.
function findCenter()
{
	if($("#search_address").val() != '')
	{
		var url = "http://www2.learningrx.com/locator/?radius=50&search_address="+$("#search_address").val();
		window.open (url,"mywindow");
	}
}

//function for converting urls in tweets to links
function create_urls(input)
{
    return input
    .replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '<a href="$&" target="_blank" >$&</a>')
    .replace(/([^\/])(www[\S]+(\b|$))/gim, '$1<a href="http://$2" target="_blank" >$2</a>');
}


$(document).ready(function() {

	$("#sl_desc").html(create_urls($("#sl_desc").html()));

});








    
