/**
 * Class for creating deep links section.
 * @class This is the Deep Links class.  
 *
 * @param {Array} conf
 *
 * @param {String} conf.container The id container element for deep links
 * @param {String} conf.id The id of deep links element
 * @param {Array} conf.session The array of last state
 * @param {Boolean} conf.restore Restoring data after updating box
 *
 */
esyndicat.deepLinks = function(conf)
{
	var objContainer = (-1 != conf.container.indexOf('#')) ? $(conf.container) : $('#' + conf.container);
	var obj = (-1 != conf.id.indexOf('#')) ? $(conf.id) : $('#' + conf.id);

	var restore = conf.restore ? conf.restore : false;
	var deepLinks = (typeof conf.session != 'undefined') ? conf.session : new Array();

	var titleLabel = esyndicat.lang.title ? esyndicat.lang.title : 'Title';
	var titleUrl = esyndicat.lang.url ? esyndicat.lang.url : 'URL';


	/**
	 * Create deep links section
	 *
	 * @param {Integer} The number of deep links
	 */
	this.create = function(num)
	{
		var html = '';
		
		html += '<div id="form_deep_links">';

		for(var i = 0; i <= num - 1; i++)
		{
			var defaultTitle = '';
			var defaultUrl = '';

			if(restore && (typeof deepLinks[i] != 'undefined'))
			{
				defaultTitle = deepLinks[i].title;
				defaultUrl = deepLinks[i].url;
			}

			html += '<div>';
			html += '<label>';
			html += '<strong>' + titleLabel + '</strong>:&nbsp;';
			html += '<input type="text" class="text" name="deep_links[title][]" value="'+ defaultTitle +'" size="15" />';
			html += '</label>&nbsp;';
			html += '<label>';
			html += '<strong>' + titleUrl + '</strong>:&nbsp;';
			html += '<input type="text" class="text" name="deep_links[url][]" value="'+ defaultUrl +'" size="15" />';
			html += '</label>';
			html += '</div>';

			if(num > 1)
			{
				html += '<hr />';
			}
		}

		html += '</div>';

		html += '<div id="val_deep_links" style="display: none; float: left;">';
		html += '</div>';
		html += '<div id="edit_button_deep_link" style="float: right; display: none;">';
		html += '<img class="edit-field" title="'+ esyndicat.lang.edit + ' Deep Links" alt="'+ esyndicat.lang.edit + ' Deep Links" src="templates/'+ template_name +'/img/sp.gif" />';
		html += '</div>';

		obj.html(html);

		$('#edit_button_deep_link').click(function()
		{
			esyndicat.display('#divSuggestButton', 'hide');
			esyndicat.display('#saveChanges', 'show');

			esyndicat.display('#edit_button_deep_link', 'hide');
			esyndicat.display('#val_deep_links', 'hide');
			esyndicat.display('#form_deep_links', 'show');
		});
	};

	/**
	 * Show or hide deep links section
	 *
	 * @param {String} The action (show|hide)
	 */
	this.display = function(action)
	{
		esyndicat.display(objContainer, action);
	};

	/**
	 * Conversion the form
	 */
	this.conversion = function()
	{
		var html = '';
		var title = '';
		var url = '';
		var i = 0;

		$(obj).find("div[@id='form_deep_links'] div").each(function()
		{
			var thisItem = $(this);

			title = $(thisItem).find("input[@name*='title']").val();
			url = $(thisItem).find("input[@name*='url']").val();
			
			html += '<div>';
			html += title;
			html += '&nbsp;';
			html += '<a href="'+ url +'" target="_blank">'+ url +'</a>';
			html += '</div>';

			if(restore)
			{
				deepLinks[i] = {title: title, url: url};
				i++;
			}
		});

		$('#val_deep_links').html(html);

		esyndicat.display('#form_deep_links', 'hide');
		esyndicat.display('#edit_button_deep_link', 'show');
		esyndicat.display('#val_deep_links', 'show');
	};

	/**
	 * Return last form data
	 */
	this.getLastState = function()
	{
		return deepLinks;
	};
}
