/*******************************************************************************
* +--------------------------[ File Revision Info ]--------------------------+ *
* | $Revision::                                                           $: | *
* | $Date::                                                               $: | *
* | $Author::                                                             $: | *
* +--------------------------------------------------------------------------+ *
* | $Id::                                                                 $: | *
* +--------------------------------------------------------------------------+ *
*******************************************************************************/

/************************* JQUERY ONLOAD ACTIONS *****************************/
jQuery(document).ready(
	function()
	{
		var resourceTypeIDs = Array();
		resourceTypeIDs['audio'] = 1;
		resourceTypeIDs['image'] = 2;
		resourceTypeIDs['pdf'] = 3;
		resourceTypeIDs['excel'] = 4;
		resourceTypeIDs['video'] = 5;
		resourceTypeIDs['website'] = 6;
		resourceTypeIDs['word'] = 7;
		resourceTypeIDs['book'] = 8;
	
		$jQ('#addToFavorites').click(
			function()
			{
				_resourceID = $jQ('#resourceID').attr('value');
				_userID = $jQ('#userID').attr('value');
				_type = $jQ('#type').attr('value');
				
				if (_userID != '' && _resourceID != '')
				{
					addResourceToFavorites(_userID, _resourceID);
				}
			}
		);
		
		/**** HOVER EVENTS FOR MY RATING ****/
		$jQ('.myRatingStar1').mouseover(function(){ $jQ('.myRatingStar1').addClass('yellowstar'); });
		$jQ('.myRatingStar2').mouseover(function(){ $jQ('.myRatingStar1').addClass('yellowstar'); $jQ('.myRatingStar2').addClass('yellowstar'); });
		$jQ('.myRatingStar3').mouseover(function(){ $jQ('.myRatingStar1').addClass('yellowstar'); $jQ('.myRatingStar2').addClass('yellowstar'); $jQ('.myRatingStar3').addClass('yellowstar'); });
		$jQ('.myRatingStar4').mouseover(function(){ $jQ('.myRatingStar1').addClass('yellowstar'); $jQ('.myRatingStar2').addClass('yellowstar'); $jQ('.myRatingStar3').addClass('yellowstar'); $jQ('.myRatingStar4').addClass('yellowstar'); });
		$jQ('.myRatingStar5').mouseover(function(){ $jQ('.myRatingStar1').addClass('yellowstar'); $jQ('.myRatingStar2').addClass('yellowstar'); $jQ('.myRatingStar3').addClass('yellowstar'); $jQ('.myRatingStar4').addClass('yellowstar'); $jQ('.myRatingStar5').addClass('yellowstar'); });

		$jQ('.myRatingStar1').mouseout(function(){ $jQ('.myRatingStar1').removeClass('yellowstar'); });
		$jQ('.myRatingStar2').mouseout(function(){ $jQ('.myRatingStar1').removeClass('yellowstar'); $jQ('.myRatingStar2').removeClass('yellowstar'); });
		$jQ('.myRatingStar3').mouseout(function(){ $jQ('.myRatingStar1').removeClass('yellowstar'); $jQ('.myRatingStar2').removeClass('yellowstar'); $jQ('.myRatingStar3').removeClass('yellowstar'); });
		$jQ('.myRatingStar4').mouseout(function(){ $jQ('.myRatingStar1').removeClass('yellowstar'); $jQ('.myRatingStar2').removeClass('yellowstar'); $jQ('.myRatingStar3').removeClass('yellowstar'); $jQ('.myRatingStar4').removeClass('yellowstar'); });
		$jQ('.myRatingStar5').mouseout(function(){ $jQ('.myRatingStar1').removeClass('yellowstar'); $jQ('.myRatingStar2').removeClass('yellowstar'); $jQ('.myRatingStar3').removeClass('yellowstar'); $jQ('.myRatingStar4').removeClass('yellowstar'); $jQ('.myRatingStar5').removeClass('yellowstar'); });
	}
);

/********************** DYNAMIC DATA FUNCTIONS **********************/

// GET DETAIL FOR RESOURCE AND UPDATE DETAIL POPUP WINDOW
function getResourceDetail(_resourceID, _userID) 
{
	$jQ.post('../includes/ajax/favorite-resources-detail.php',{
		resourceID: _resourceID,
		userID: _userID
	},function(data){
		$jQ('div#resourceDetailContainer').html(data);
		$jQ('div#resourceDetail').fadeIn('fast');
	});
}

function addResourceToFavorites(_userID, _resourceID)
{
	$jQ.ajax({ 
		type: 'POST', 
		url: '../includes/ajax/favorite-resources-add.php',
		data: 'userID='+_userID+'&resourceID='+_resourceID,
		success: function(data) {
			$jQ('#addToFavorites').attr('innerHTML','');
			$jQ('#myFavoriteResource').removeClass('hide');
		},
		error: function(data) {
			$jQ.prompt(data);
		},
	});
}

// UPDATES USER RATING FOR A RESOURCE
function updateResourceRating(userID, resourceID, newRating)
{
	$jQ(document.body).css('cursor', 'wait');
	
	$jQ.ajax({ 
		type: 'POST', 
		url: '../includes/ajax/resource-rating.php',
		data: 'userID='+userID+'&resourceID='+resourceID+'&rating='+newRating,
		success: function(data) 
		{
			$jQ('#avgResourceRating'+resourceID).attr('innerHTML',data);
			
			for (i=1;i<6;i++) 
			{
				if (i<=newRating)
				{
					document.getElementById('resource'+resourceID+'rating'+i).src = '../images/user/yellowstar.png';
				} 
				else
				{
					document.getElementById('resource'+resourceID+'rating'+i).src = '../images/user/greystar.png';
				}
			}
			
			$jQ(document.body).css('cursor', '');
		},
		error: function(data)
		{
			$jQ.prompt(data);
		},
	});
}