﻿function FeaturedArticleSorter(){ // singleton
	var self = this;
	var $featuredTable = jQuery('.Table1 tbody:eq(0)');
	var $libraryTable = jQuery('.Table1 tbody:eq(1)');
	var $errorBalloon = jQuery('<div id="FeaturedArticleSorterError"><p></p></div>');
	var $statusConsole = jQuery('.Status td span');
	var $helper = jQuery('.helper');
	var idArray = new Array();
	var idArraySorted = new Array();
	var tableDataStatusTemplate = '<td class="status"><span>{0}</span> <img src="/images/Search_Divider.gif" alt="" /> <a href="' + dlabs.settings.urls.forms + '{1}.aspx?id={2}">Edit</a></td>';
	var tableDateOrderTemplate = '<td class="order">{0}</td>';
	var tableRowTemplate = '<tr id="id{0}"><td class="feature"><input type="checkbox" /></td><td class="date">{1}</td><td class="title">{2}</td><td class="views">{3}</td><td class="rating"><img src="/images/stars/stars_3_{4}.gif" alt="Rating" /></td><td class="earnings">${5}</td></tr>';
	var xhr = null;
	var errorBalloonTimoutId = null;
	var maxItems = 5;
	var options = {
		containment: '#FeaturedTableContainer',
		forcePlaceholderSize: true,
		scroll: false
	};
	var messages = {
		processingRequest: 'Please wait a moment while we process your request.',
		duplicateItems: 'This article has already been added to your featured articles.',
		maxItems: 'You may only select up to <strong>' + maxItems + '</strong> articles to be featured on your profile.',
		minItems: 'There are no more items left to remove.',
		savingArticle: 'Saving article&hellip; please wait.',
		removingArticle: 'Removing article&hellip; please wait.',
		savingArticleOrder: 'Saving article order&hellip; please wait.',
		successAddTableRow: '<em>{0}</em> was <strong>added</strong> to your featured articles.',
		successRemoveTableRow: '<em>{0}</em> was <strong>removed</strong> from your featured articles.',
		successUpdateTableRowOrder: 'Your featured articles order has been saved. <a href="/members/' + dlabs.user.name + '.html?view=3rd">See your public profile</a>.',
		failedAddTableRow: 'Sorry, we were unable to add <em>{0}</em> to your featured articles.',
		failedRemoveTableRow: 'Sorry, we were unable to remove <em>{0}</em> from your featured articles.',
		failedUpdateTableRowOrder: 'Sorry, we were unable to save the order of your featured articles.'
	};

	function isChecked(tableRow){		
		return jQuery('.feature input', tableRow).is(':checked') === false;
	}
	function formatTableRowObject(object){
		var tableRowObject = {
			id: object.ArticleId || object.ArticleID,
			url: object.Url || object.UrlSnippet,
			date: object.CreatedDate.split(' ')[0],
			title: object.Title,
			views: object.ViewCount.toString(),
			format: 1,
			rating: (parseFloat(object.Rating) * 10).toString(),
			earnings: (parseFloat(object.Earning).toFixed(2)).toString()
		};
		
		if(object.ArticleType){
			tableRowObject.type = object.ArticleType.toLower();
			tableRowObject.status = object.ArticleStatus;
			tableRowObject.format = 2;
		}
		
		return tableRowObject;
	}
	function formatTableRow(tableRow){				
		if(tableRow.Title)
			tableRow = formatTableRowObject(tableRow);
		
		var $tableRow = tableRow.format >= 1
			? jQuery(String.format(tableRowTemplate, tableRow.id, tableRow.date, tableRow.title, tableRow.views, tableRow.rating, tableRow.earnings))
			: jQuery(tableRow).clone(false);
		
		if($tableRow.length === 0)
			return false;
		if(jQuery.browser.msie6)
			$tableRow.hoverClass('hover');
		if(tableRow.format === 2)
			$tableRow.append(String.format(tableDataStatusTemplate, tableRow.status, tableRow.type === 'p' ? 'contributeehowp' : 'contribute', tableRow.id));
		else{
			$tableRow.removeAttr('class');
			$tableRow.children('.status').remove();
			jQuery(String.format(tableDateOrderTemplate, idArray.length))
				.insertBefore($tableRow.children('.date'));
			jQuery('.feature input', $tableRow)
				.unbind('click')
				.bind('click', function(){ return removeTableRow($tableRow[0]); });
			jQuery('.title a', $tableRow)
				.replaceWith(jQuery('.title a', $tableRow).html());
		}
		
		return $tableRow;
	}
	function addTableRow(tableRow){
		var id = tableRow.id.replace(RegExp.filter.nonNumeric, String.empty);				

		if(idArray.length >= maxItems)
			return showErrorBallon(tableRow, messages.maxItems);
		if(idArray.contains(id))
			return showErrorBallon(tableRow, messages.duplicateItems);
		
		self.status.update(messages.savingArticle, 1);
		self.saveArticleState(id, function(hasFailed){				
			var $tableRow =  $libraryTable.children('#' + tableRow.id);
			var title = jQuery('.title a', $tableRow).html();
			
			jQuery('.feature input', $tableRow)
				.attr('checked', !hasFailed);

			if(hasFailed || !addTableRowId(id))
				return self.status.update(String.format(messages.failedAddTableRow, title), 4);
			
			$tableRow.addClass('selected');
			
			$tableRow = formatTableRow(tableRow);
			
			$tableRow.appendTo($featuredTable).hide();
			
			$tableRow.fadeIn(200, function(){
				jQuery('.feature input', this).attr('checked', true);
				
				self.saveArticleOrder(function(hasFailed){						
					self.status.update(String.format(messages.successAddTableRow, title), 3);
				});
			});
		});

		return false;
	}
	function addTableRowId(id){				
		if(idArray.contains(id))
			return false;

		idArray.push(id);

		return true;
	}
	function removeTableRow(tableRow){
		self.status.update(messages.removingArticle, 1);
		
		var id = tableRow.id.replace(RegExp.filter.nonNumeric, String.empty);

		if(idArray.length === 0)
			return self.status.update(messages.minItems, 2);

		self.saveArticleState(id, function(hasFailed){		
			var $tableRow = $libraryTable.children('#' + tableRow.id);
			var title = jQuery('.title', $featuredTable.children('#' + tableRow.id)).html();

			jQuery('.feature input', $tableRow)
				.attr('checked', hasFailed);
			
			if(hasFailed || !removeTableRowId(id))
				return self.status.update(String.format(messages.failedRemoveTableRow, title), 4);
			
			$tableRow.removeClass('selected');
			$featuredTable.children('#' + tableRow.id).fadeOut(200, function(){
				jQuery(this).remove();
				
				self.saveArticleOrder(function(){
					return idArray.length === 0
						? self.status.reset(1)
						: self.status.update(String.format(messages.successRemoveTableRow, title), 3);
				});
			});
		});

		return false;
	}
	function removeTableRowId(id){
		if(!idArray.contains(id))
			return false;

		idArray.remove(idArray.indexOf(id));

		return true;
	}
	function showErrorBallon(tableRow, value){
		var $input = jQuery('.feature input', tableRow);
		var position = $input.position();
		
		clearTimeout(errorBalloonTimoutId);
		$errorBalloon.children('p').html(value);
		
		position.top -= $errorBalloon.height() + $input.height();
		position.left -= ($errorBalloon.width() / 2) - ($input.width() / 2);

		$errorBalloon.css(position).fadeIn(200);
			
		errorBalloonTimoutId = setTimeout(function(){
			$errorBalloon.fadeOut(200);
		}, 3000);
		
		return false;
	}
	function buildFeaturedTable(tableRows){				
		jQuery(tableRows).each(function(i, tableRow){
			if(!addTableRowId(tableRow.ArticleId))
				return false;
			
			var $tableRow = formatTableRow(tableRow);

			$featuredTable.append($tableRow);
			
			jQuery('.feature input', $tableRow)
				.attr('checked', true)
				.bind('click', function(){ return removeTableRow($tableRow[0]); });
		});
	}
	function buildLibraryTable(tableRows){			
		var $tableRows = tableRows.jquery ? tableRows : jQuery(tableRows);
		
		if(!tableRows.jquery){
			$libraryTable.children().remove();
			$tableRows.each(function(i, tableRow){
				var $tableRow = formatTableRow(tableRow);
				
				$libraryTable.append($tableRow);
			});
		}

		$tableRows.each(function(i, tableRow){
			jQuery('input', tableRow)
				.attr('checked', idArray.contains(tableRow.id.replace(RegExp.filter.nonNumeric, String.empty)))
				.bind('click', function(){ return isChecked(tableRow) ? removeTableRow(tableRow) : addTableRow(tableRow); });
		});
	}
	
	self.status = new MessageConsole($statusConsole, $statusConsole.html(), { timeout: 15000, speed: 200, opacity: 0 });
	
	self.saveArticleOrder = function(fn){
		if(!jQuery.isFunction(fn))
			self.status.update(messages.savingArticleOrder, 1);				
		if(xhr || idArray.length === 0){
			if(jQuery.isFunction(fn))
				fn(true);
			
			return self;
		}

		idArraySorted = $featuredTable.sortable('toArray');
		
		function onResponse(response){
			var hasFailed = parseInt(response, 10) !== 1;
			
			if(hasFailed){
				self.status.update(messages.failedUpdateTableRowOrder, 4);
				
				if(jQuery.isFunction(fn))
					fn(true);
				
				return false;
			}

			for(var i = 0, n = idArraySorted.length; i < n; i++)
				jQuery('#' + idArraySorted[i] + ' .order', $featuredTable)
					.html((i + 1).toString());
			
			self.status.update(messages.successUpdateTableRowOrder, 3);	
			
			if(jQuery.isFunction(fn))
				fn(false);

			return xhr = response = null;
		}

		xhr = jQuery.ajax({
			type: 'POST',
			url: '/ajax/common.aspx?method=UpdateFeaturedArticleOrder',
			data: '&articles=' + idArraySorted.toString().replace(/id/g, String.empty).replace(/,/g, '|'),
			success: onResponse,
			error: onResponse
		});
		
		return self;
	}
	self.saveArticleState = function(id, fn){
		if(xhr){
			self.status.update(messages.processingRequest, 1);
		
			return self;
		}

		function onResponse(response){
			var hasFailed = (parseInt(response, 10) || 3) === 3;

			if(jQuery.isFunction(fn))
				fn(hasFailed);
			
			return xhr = response = null;
		}

		xhr = jQuery.ajax({
			type: 'POST',
			url: '/ajax/Common.aspx?method=UpdateFeaturedArticle',
			data: '&articleid=' + id,
			success: onResponse,
			error: onResponse
		});
		
		return self;
	}
	
	return (function(){				
		jQuery.getJSON('/ajax/Common.aspx?method=LoadFeaturedArticle', function(response){
			jQuery('.loading', $featuredTable).remove();
			buildFeaturedTable(response);
			buildLibraryTable($libraryTable.children());
			$errorBalloon.appendTo('body').hide();

			options.revert = !jQuery.browser.msie
			options.stop = self.saveArticleOrder;
			
			$featuredTable.sortable(options);
			
			return response = null;
		});
	})();
}
function ArticleLibraryPager(){ // singleton
	var self = this;
	var xhr = null;
	

	self.options = {
		sortOrder: 1,
		sortBy: 'DATE'
	};
	self.getPage = function(pageNumber, fn){
		if(xhr !== null){
			try{ xhr.status; }
			catch(error){
				xhr.abort();
				jQuery.event.trigger('ajaxStop');
				jQuery.active--;
				
				xhrRetryCount = 0;
				xhr = null;							
			}
		}
		
		function onResponse(response, x){
			var hasFailed = false;
			
			console.log(response, x)

			if(jQuery.isFunction(fn))
				fn(hasFailed);
			
			return xhr = response = null;
		}
		
		xhr = jQuery.ajax({
			type: 'POST',
			url: '?response=json',
			data: '&page=' + pageNumber,
			success: onResponse,
			error: onResponse
		});
	};
}

dlabs.objects.titleExists = function (title) {
    var returnValue = true;
    jQuery.ajax(
        {
            type: 'post',
            url: '/ajax/CheckTitle.aspx',
            data: { title: title },
            success: function(data) { returnValue = data == "DUP" ? true : false; },
            error: function() { returnValue = null; },
            async: false
        }        
    )
    
    return returnValue;
}

dlabs.objects.writeArticle = function(selector){
    var title = jQuery(selector).val();
    title = title.replace(/\s{2,}/ig, ' ').replace(/^\s+/ig, '').replace(/\s+$/ig, '');
    var titleExists =  dlabs.objects.titleExists(title);
    if(titleExists == null)
    {
        alert('The system is temporarily unavailable. Please try again later.');
        return false;
    }
    else if(titleExists) {
        alert('There are already articles with the same title.');
        return false;
    }
    
	title = title.replace(/^(how to)/i, String.empty).trim().replace(/ /g, '+');
	
	if(title)
	{
		window.location = dlabs.settings.urls.forms + 'contribute.aspx?title=' + title;
	}
		
	return false;
};
dlabs.objects.sortTable = function(sortValue){	
	var sortByInput = $('#sortby');
	var sortOrderInput = $('#sortorder');			

    if(sortByInput.val() != sortValue){
        sortByInput.val(sortValue);
        sortOrderInput.val(sortOrderInput.val() == "1" ? "1" : "0");
    }
    else
    {
		sortOrderInput.val(sortOrderInput.val() == "1" ? "0" : "1");
    }

    document.forms.articles.submit();
};
