var rater;

function initArticlePage()
{
	var rss = $( 'productFeed' );
	
	if( rss ) {
		
		new Ajax.Updater( rss, getRootPath(), 
		{
			parameters: { 
				act: 		'rssbox',
				id: 		productID
			}
		} );
	}
	
	setupRating();
}

function setupRating()
{
 	rater = new Control.Rating( 'rating', {  
		value: 			userRating,  
		rated: 			alreadyRated,  
		min: 			1,  
		max: 			5,  
 		multiple: 		false,
		hovering: 		ratingUpdate,
		updateUrl: 		getRootPath() + '?act=software.rate&id=' + productID,
		updateOptions: 	{
			onSuccess: 	function( response )
			{
				$( 'ratingCountNum' ).innerHTML = response.responseJSON.count;
				
				rater.setValue( response.responseJSON.rating, true, true );
				ratingUpdate( parseFloat( response.responseJSON.rating ).toFixed() );
			}
		}
	});
}

function ratingUpdate( val )
{	
	var texts = [ '', DC_HATE_IT, DC_DONT_LIKE_IT, DC_ITS_OK, DC_ITS_GOOD, DC_ITS_EXCELLENT ];

	$( 'ratingDescr' ).innerHTML = texts[ val ];
}

function addBookmark()
{
	if( window.sidebar )		// Mozilla Firefox Bookmark
		window.sidebar.addPanel( window.title, location.href, '' );
	else if( window.external )	// IE Favorite
		window.external.AddFavorite( location.href, window.title );
}

function addComment()
{
	Effect.Appear( 'addCommentBox', { duration: 0.5 } );
}

function submitComment()
{
	new Ajax.Request( getRootPath(), 
	{
		parameters: { 
			act: 				'software.addComment', 
			title: 				$F( 'commentTitle' ),
			comment: 			$F( 'commentBody' ),
			software: 			productID 
		},
		onSuccess: 	function( response )
		{
			alert( DC_COMMENT_QUEUED );
			cancelComment();
			
			$( 'commentTitle' ).value = '';
			$( 'commentBody' ).value = '';
		},
		onFailure: 	function( response )
		{
			alert( DC_ERR_SAVING_COMMENT );
		}
	} );
}

function cancelComment()
{
	Effect.Fade( 'addCommentBox', { duration: 0.5 } );
}

function showCommentsPage( page )
{
	new Ajax.Updater( 'commentsBox', getRootPath(), 
	{
		parameters: { 
			act: 		'software.getComments',
			id: 		productID,
			page: 		page
		}
	} );
	
	return( false );
}

function printArticle()
{
	window.open( getRootPath() + '?act=software.printArticle&id=' + productID,
	 			 'PrintArticle',
				 defaultPopupParams( 850, 500 ));
}

function toggleWatchlist()
{
	if( $( 'watchlistTool' ).hasClassName( 'removeFromWatchlist' ))
		removeFromWatchlist();
	else
		addToWatchlist();
}

function removeFromWatchlist()
{
	new Ajax.Request( getRootPath(), 
	{
		parameters: { 
			act: 				'user.watchlist.remove', 
			software: 			productID
		},
		onSuccess: 	function( response )
		{
			if( response.responseJSON.result ) {
				var ctrl = $( 'watchlistTool' );
				
				ctrl.removeClassName( 'removeFromWatchlist' );
				ctrl.addClassName( 'addToWatchlist' );
				
				$( 'watchlistToolLink' ).innerHTML = DC_ADD;
				$( 'watchlistSpecs' ).innerHTML = '<img src="images/watchlist/add.png"> ' + DC_ADD_TO_WATCHLIST;
			}
		}
	} );
}

function addToWatchlist( id )
{
	new Ajax.Request( getRootPath(), 
	{
		parameters: { 
			act: 				'user.watchlist.add', 
			software: 			productID
		},
		onSuccess: 	function( response )
		{
			if( response.responseJSON.result ) {
				var ctrl = $( 'watchlistTool' );
				
				ctrl.addClassName( 'removeFromWatchlist' );
				ctrl.removeClassName( 'addToWatchlist' );
				
				$( 'watchlistToolLink' ).innerHTML = DC_REMOVE;
				$( 'watchlistSpecs' ).innerHTML = '<img src="images/watchlist/remove.png"> ' + DC_REMOVE_FROM_WATCHLIST;
			}
		}
	} );
}

function checkDownload( id, url, preview )
{
	$( 'checkingDownloads' ).show();

	new Ajax.Request( getRootPath(), 
	{
		parameters: { 
			act: 		'software.checkDownload', 
			link: 		id,
			preview: 	preview ? 1 : 0
		},
		onSuccess: 	function( response )
		{
			$( 'checkingDownloads' ).hide();

			if( response.responseJSON.result )
				location.href = url;
			else
				downloadFailed( id );
		},
		onFailure: function( response )
		{
			$( 'checkingDownloads' ).hide();

			downloadFailed( id );
		}
	} );
}

function downloadFailed( id )
{
	Modalbox.show( $( 'brokenDownload' ), { title: DC_DOWNLOAD_PROBLEM, width: 450 } );	
}

