// Create an overlay for selecting video/picture to add to poll/opinion
$(document).ready(function()
{
	$(".addPicture").click(function()
			{
				var diag = $("#addpic");
				$(diag).css("display", "block");
				$(diag).dialog({
					height:460, 
					width:750, 
					modal:true, 
					resizable: false,
					overlay:{background: '#555', opacity: 0.7}
				});
			});	
	$(".addVideo").click(function()
		{
			var diag = $("#addmedia");
			$(diag).css("display", "block");
			$(diag).dialog({
				height:460, 
				width:750, 
				modal:true, 
				resizable: false,
				overlay:{background: '#555', opacity: 0.7}
			});
	});	
	$('#videoquery').click(function()
		{
			var query = $('#videoq').attr('value');
			query = jQuery.trim(query);
			if (query == '')
			{
				$('#videoArea').html('<h3 class="search-error">You need to include a search term.</h3>');
			}
			else
			{
				var query_array = query.split(" ");
				if (query_array.length == 1)
				{
	
					var match = /watch\?v=([a-zA-Z0-9_]+)/i.exec(query);
					if (match)
					{
						$('#videoArea').html('<img class="videoloader" src="/web/images/videoload.gif" />');
						$('#videoPreview').text('');
						$('#videoArea').load("/index.php/poll/video_view_single/" + match[1] + "/");				
				
					}
					else
					{
						$('#videoArea').html('<img class="videoloader" src="/web/images/videoload.gif" />');
						$('#videoPreview').text('');
						$('#videoArea').load("/index.php/poll/video_view/" + query + "/");				
					}
				}
				else
				{
					var i = 0;
					$('#videoArea').html('<img class="videoloader" src="/web/images/videoload.gif" />');
					var real_query = '';
					for (i = 0; i < query_array.length; i++)
					{
						if (query_array[i] != '')
						{
							real_query = real_query + query_array[i];
						}
						if (i < query_array.length - 1)
						{
							real_query += '010';
						}
					}
					$('#videoPreview').text('');
					$('#videoArea').load("/index.php/poll/video_view/" + real_query + "/");
				}
			}
		});	
	$('#picSearchQuery').click(function()
		{
			var pquery = $('#picq').attr('value');
			pquery = jQuery.trim(pquery);
			if (pquery == '')
			{
				$('#pictureArea').html('<h3 class="search-error">You need to include a search term.</h3>');
			}
			else
			{
				var query_array = pquery.split(" ");
				var i = 0;
				$('#pictureArea').html('<img class="videoloader" src="/web/images/videoload.gif" />');
				var real_query = '';
				for (i = 0; i < query_array.length; i++)
				{
					if (query_array[i] != '')
					{
						real_query = real_query + query_array[i];
					}
					if (i < query_array.length - 1)
					{
						real_query += '010';
					}
				}
				$('#pictureArea').load('/index.php/poll/pic_search/' + real_query + '/');
				$('#picturePreview').text('');
			}
		});
	$('#single').click(function()
		{
			$(this).attr('class', 'selected_poll_type');
			$('#polltype').attr('value', '0');
			$('#multi').attr('class', 'unselected_poll_type');
			$('#type_req').attr('src', '/web/images/icons/tick.png').add().attr('alt', 'OK!').add().attr('title', 'OK!');			
		});
	$('#multi').click(function()
			{
				$(this).attr('class', 'selected_poll_type');
				$('#polltype').attr('value', '1');
				$('#single').attr('class', 'unselected_poll_type');
				$('#type_req').attr('src', '/web/images/icons/tick.png').add().attr('alt', 'OK!').add().attr('title', 'OK!');
			});	
	$('#catselect').change(function() 
		{
			var selected = $('#catselect').attr('value');
			if (selected != -1)
			{
				$('#cat_req').attr('src', '/web/images/icons/tick.png').add().attr('alt', 'OK!').add().attr('title', 'OK!');
			}
			else
			{
				$('#cat_req').attr('src', '/web/images/icons/cross.png').add().attr('alt', 'You need to choose a category.').add().attr('title', 'You need to choose a category.');
			}
		});
});


function showPreview(videoid)
{
	showTitle(videoid);
	videoLoc = $("#videoPreview");
	embedCode = '<object width="320" height="225"><param name="movie" value="http:www.youtube.com/w/' + videoid + '&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><embed src="http://www.youtube.com/v/' + videoid + '&h1=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="false" width="320" height="225"></embed></object>';
	embedCode += '<br /><br /><h3 class="saveit">Save It  </h3><a href="javascript:;"><img class="imgLink" src="/web/images/icons/accept.png" alt="Save It" id="' + videoid + '" onclick="saveVideo(this.id)" title="Save It" /></a>';
	$(videoLoc).html(embedCode);
}


function showPicPreview(id)
{
	showPicTitle(id);
	picLoc = document.getElementById("picturePreview");
	embedCode = '<img class="picPrev" id="previewImage" src="' + id + '" />';
	embedCode += '<br /><br /><h3 class="saveit">Save It  </h3><a href="javascript:;"><img class="imgLink" src="/web/images/icons/accept.png" alt="Save It" id="previewImage" title="Save It" onClick="savePicture()" /></a>';
	picLoc.innerHTML = embedCode;
}

function showTitle(id)
{
	var thumb = document.getElementById(id);
	var title = thumb.getAttribute('videotitle');
	var previewt = document.getElementById('previewt');
	previewt.innerHTML = '<h3 class="diag-desc-video">' + title + '</h3>';	
}

function showPicTitle(id)
{
	var thumb = document.getElementById(id);
	var title = thumb.getAttribute('name');
	var previewp = document.getElementById('previewp');
	previewp.innerHTML = '<h3 id="previewImageT" name="' + title + '" class="diag-desc-video">' + title + '</h3>';	
}

function saveVideo(id)
{

	//var thumb = document.getElementById(id);
	//var title = thumb.getAttribute('videotitle');
	$('#videoembed').attr('value', id);
	$('#video_status').html('Video Added! <a href="javascript:;" onClick="javascript:removeVid();">(remove)</a>');
	$('#addmedia').dialog('close');		
	$('.ui-dialog-overlay').css('display', 'none');
	$('.ui-dialog').css('display', 'none');	
}

function savePicture()
{
	var thumb = $('#previewImage').attr('src');
	//var prevt = $('#previewImageT').attr('name');
	$('#picembed').attr('value', thumb);
	$('#pic_status').html('Picture Added! <a href="javascript:;" onClick="javascript:removePic();">(remove)</a>');
	$('#addpic').dialog('close');
	$('.ui-dialog-overlay').css('display', 'none');
	$('.ui-dialog').css('display', 'none');
}

function removePic()
{
	$('#picembed').attr('value', '');	
	$('#pic_status').empty();
}

function removeVid()
{
	$('#videoembed').attr('value', '');	
	$('#video_status').empty();
}



