/*
 * Video view page functions
 * $Id: video.js 3553 2009-03-23 05:21:39Z rgill $
 */

function searchBarOnClick()
{
	if( document.videosearchform.searchvalue.value == 'Search Videos!' )
	{
		$(".header-search").val('').css('color' , 'black');
	}
}

function getFormData( form )
{
	var sending = {};
	$(form).find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea") .filter(":enabled") .each(function() { sending[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; });
	
	return sending;
}

function reEncode( form , video_id)
{
	if( form )
	{
		name = form.ytuser_name.value;
		pass = form.ytuser_pass.value;
	}else{
		name = '';
		pass = '';
	}
	
	jQuery.post( '/video/encode/' + video_id + '/' ,
		{ ytuser_name: name , ytuser_pass: pass } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// Comment posted.  reload comments to page 1.
					jQuery('#encodeMessage').html( 'Video is now being encoded and uploaded to youtube.');
				}
				else
				{
					jQuery('#encodeError').html( json.result.error).fadeIn('fast')
				}
			}
		,
		"json"
	);
		
	return false;
}

function contactUs( form )
{
	name = form.name.value;
	email = form.email.value;
	comment = form.comment.value;
	
	jQuery.post( '/site/contact' ,
		{ name: name , email: email , comment: comment } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// Comment posted.  reload comments to page 1.
					form.innerHTML = '<h4>Message sent. We will get back to you shortly.</h4>' + form.innerHTML ;
				}
				else
				{
					jQuery('#contactMessage').html( json.result.error).fadeIn('fast')
					window.setTimeout(function() {jQuery('#contactMessage').fadeOut('slow');}, 2000);
				}
			}
		,
		"json"
	);
		
	return false;
}


function getVideoSiblingPage(videoid, page)
{
	jQuery.ajax({
	    url:      '/video/siblings/' + videoid + '/page/' + page,
	    type:     'GET',
	    dataType: 'html',
	    timeout:  1000,
	    error: function()
	    {
	       // Do nothing
	    },
	    success: function(html)
	    {
			//jQuery('html,body').animate({scrollTop: 630}, 400);
	    	document.getElementById('videoSiblingsContainer').innerHTML = html;
	    }
	});
}


// -----------------------------
// COMMENTS STUFF
// -----------------------------
function getVideoCommentPage(videoid, page)
{
	jQuery.ajax({
	    url:      '/video/comments/' + videoid + '/page/' + page,
	    type:     'GET',
	    dataType: 'html',
	    timeout:  1000,
	    error: function()
	    {
	       // Do nothing
	    },
	    success: function(html)
	    {
			jQuery('html,body').animate({scrollTop: 630}, 400);
	    	document.getElementById('videoCommentsContainer').innerHTML = html;
	    }
	});
}
function getUserFavoritesPage(user, page)
{
	jQuery.ajax({
	    url:      '/user/' + user + '/favorites/page/' + page,
	    type:     'GET',
	    dataType: 'html',
	    timeout:  1000,
	    error: function()
	    {
	       // Do nothing
	    },
	    success: function(html)
	    {
			//jQuery('html,body').animate({scrollTop: 630}, 400);
	    	document.getElementById('userFavoritesContainer').innerHTML = html;
	    }
	});
}
function getUserVideosPage(user, page)
{
	jQuery.ajax({
	    url:      '/user/' + user + '/videos/page/' + page,
	    type:     'GET',
	    dataType: 'html',
	    timeout:  1000,
	    error: function()
	    {
	       // Do nothing
	    },
	    success: function(html)
	    {
			//jQuery('html,body').animate({scrollTop: 630}, 400);
	    	document.getElementById('userVideosContainer').innerHTML = html;
	    }
	});
}



function postComment(videoid)
{
	form = jQuery('#commentform');
	data = getFormData( form );
	
	jQuery.post( jQuery('#commentform').attr('action') ,
		data ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// Comment posted.  reload comments to page 1.
					getVideoCommentPage( videoid , 1);
					document.getElementById('commentform').reset();
				}
				else
				{
					document.getElementById('postCommentErrorMessage').innerHTML = json.result.error;
					jQuery('#postCommentErrorMessage').fadeIn('fast')
					window.setTimeout(function() {jQuery('#postCommentErrorMessage').fadeOut('slow');}, 2000);
				}
			}
		,
		"json"
	);
	
	return false;
	
}


function addFavorite(videoid)
{
	jQuery.post( '/video/addfavorite/' + videoid + '/' ,
		{ videoid: videoid } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// You successfully added a friend
					jQuery('#addFavoriteContainer').hide();
					jQuery('#isFavoriteContainer').show('slow');
				}
				else
				{
					document.getElementById('favoriteErrorMessage').innerHTML = json.result.error;
					jQuery('#favoriteErrorMessage').fadeIn('fast')
					window.setTimeout(function() {jQuery('#favoriteErrorMessage').fadeOut('slow');}, 4500);
				}
			}
		,
		"json"
	);
}

function removeFavorite(videoid)
{
	jQuery.post( '/video/removefavorite/' + videoid + '/' ,
		{ videoid: videoid } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// You successfully added a favorite
					jQuery('#isFavoriteContainer').hide();
					jQuery('#addFavoriteContainer').show('slow');
				}
				else
				{
					document.getElementById('favoriteErrorMessage').innerHTML = json.result.error;
					jQuery('#favoriteErrorMessage').fadeIn('fast')
					window.setTimeout(function() {jQuery('#favoriteErrorMessage').fadeOut('slow');}, 4500);
				}
			}
		,
		"json"
	);
}


function rateVideo(videoid,rating)
{
	//THIS FUNCTION IS NOT USED.  SEE THE JQUERY PLUGIN jquery.rating.js
	jQuery.get( '/video/ratevideo/' + videoid + '/' ,
		{ videoid: videoid , rating: rating } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// Success
				}
				else
				{
					
				}
			}
		,
		"json"
	);
}

function flagVideo(videoid)
{
	jQuery.get( '/video/flag/' + videoid + '/' ,
		{ videoid: videoid } ,
			function(json) 
			{
				jQuery( "#flagVideo" ).html( 'Flagged');
				if (json.result.success == true)
				{
					// Success
				}
				else
				{
					
				}
			}
		,
		"json"
	);
	
	return false;
}

function rateComment(commentid,rating)
{
	jQuery.post( '/video/ratecomment/now/' ,
		{ commentid: commentid , rating: rating } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// You successfully added a friend
				}
				else
				{
					//alert( json.result.error );
				}
			}
		,
		"json"
	);
}

function flagComment(commentid)
{
	jQuery.post( '/video/flagcomment/now/' ,
		{ commentid: commentid } ,
			function(json) 
			{
				if (json.result.success == true)
				{
					// You successfully added a friend
				}
				else
				{
					//alert( json.result.error );
				}
			}
		,
		"json"
	);
}


function writeAjaxRating( id , rating )
{
	document.write( '<div id="rating_'+id+'" class="rating"></div>');
	$(document).ready(function() {
		$("#rating_"+id).rating('/video/rate/' + id , {maxvalue:5, increment:.5, curvalue: rating } );
	} );
}


function areyousure(htmlobj,href, message)
{
	htmlobj.resetAreYouSure = function()
	{
		this.href      = this.oldhref;
		this.innerHTML = this.oldmessage;
		this.onclick   = this.oldonclick;
		htmlobj.setAttribute('id','');
	}
		
	htmlobj.oldhref    ='#';
	htmlobj.oldmessage = htmlobj.innerHTML;
	htmlobj.oldonclick = htmlobj.onclick;
	htmlobj.setAttribute('id', 'areyousure111');
	
	setTimeout("document.getElementById('areyousure111').resetAreYouSure()", 3000);
	
	htmlobj.innerHTML = message;
	htmlobj.href = href;
	htmlobj.onclick = function() {};
}