/**
* PLY Utils
* 
* Misc utility functions & properties
*
* @author: PLY Interactive
* @dependency: none
**/

if (typeof PLY != 'object') PLY = {};
if (!PLY.Data) PLY.Data = {};
if (!PLY.UI) PLY.UI = {};

PLY.logEnabled = true;
if (!PLY.logEnabled || typeof window.console == "undefined")
{
    window.console = { log: function() { }, debug: function() { } };
}

/**
 * PLY.Data.EntityTour wraps functionality surrounding saving & writing to the current wwc tour
*/
PLY.Data.EntityTour = function()
{	
	//add an entity id to the tour
	this.add = function(id)
	{
		var data = _getCookieData();
		data.items.push(id);
		_setCookieData(data);
	}

	this.addMany = function(arrIds)
	{
		var data = _getCookieData();
		for (var i = 0; i < arrIds.length; i++)
		{
			data.items.push(arrIds[i]);
		}
		_setCookieData(data);
	}

	//get the number of entities stored in the tour
	this.getLength = function()
	{
		var data = _getCookieData();
		return data.items.length;
	}

	/*
	//get the saved name of the tour
	this.getName = function()
	{
		var data = _getCookieData();
		return data.name;
	}
	*/

	this.contains = function(entityId)
	{
		var data = _getCookieData();

		for (var i = 0; i < data.items.length; i++)
		{
			if (data.items[i] == entityId)
			{
				return true;
			}
		}
		return false;
	}

	this.getList = function()
	{
		var data = _getCookieData();
		return data.items;
	}

	/*
	//set the saved name of the tour
	this.setName = function(newName)
	{
		var data = _getCookieData();
		data.name = newName;
		_setCookieData(data);
	}
	*/

	//delete all items from the tour
	this.clear = function()
	{
		var data = _getCookieData();
		data.items = [];
		_setCookieData(data);
	}

	//internal functions for moving data to/from the cookie
	var _getCookieData = function()
	{		
		var datastring = jQuery.cookie('wwctour');

		if (datastring == null)
		{
			return { name : 'My New Tour', items : [] };
		}

		var arrValues = datastring.split('|');
		var data = { name : arrValues[0], items : arrValues[1] != '' ? arrValues[1].split(',') : [] };

		return data;
	}

	var _setCookieData = function(data)
	{
		var datastring = data.name + '|' + data.items.join(',');
		jQuery.cookie('wwctour', datastring, {path : '/'});
	}
}

/**
 * This function will take the specified element, which must have the following format:
 *
 *  <span data-name="testemail" data-domain="plyinteractive.com" class="email-link">Email Me!</span>
 *
 *  And reformat it as this: <a href="mailto:testemail@plyinteractive.com">Email Me!</span>
 *  To have the link text display the email address, set the text to {email}
 */
PLY.UI.ConstructMailTo = function(element)
{
	var address  = element.attr('data-name') + '@' + element.attr('data-domain');
	var text = element.text();

	if (text == '{email address will appear here}')
	{
		text = address;
	}
	element.replaceWith('<a href="mailto:'+address+'">'+text+'</a>');
}

$(function() {
	$('span.email-link').each(function(){
		PLY.UI.ConstructMailTo($(this));
	});
})
