/* 
 * --------------------------------------------------------------------------------------------
 * Common javascript utilities for tampabay.com
 * --------------------------------------------------------------------------------------------
 */

function udrInitUser() {
    var _div = $('#udr-user-container');

    $.getJSON('/components/account/user/jsonProfile', function(data) {
        _div.html('');
        if (data.id > 0) {
            // user is logged in
            _div.append(
                $('<span/>').addClass('user-welcome-container').html("Welcome ").append(
                    $('<a/>').attr('href','/components/account/user/getprofile').html(data.loginName)
                )
            ).append(
                $('<a/>').attr('href',"/components/account/logout").html('Log out')
            );
        } else {
            _div.append(
                $('<a/>').attr('href',"/components/account/login").html('Login')
            ).append(' or ').append(
                $('<a/>').attr('href',"/components/account/registration").html('Register')
            );
        }
    });

    $('#udr-user-container').fadeIn('slow');
}


// define our object
function tpc() {}

/** 
 * Gets a query string parameter from the URL. If not found, the calling function
 * can specify a default value with the second argument
 * 
 * @param key key to find within the query string
 * @param _def default value if query string key is not found
 * 
 * @return value of key combination in query string, _def if not found
 */
tpc.param = function(key, _def) {
	if (_def == null) 
		_def = '';
	
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	
	return (qs == null) ? _def : qs[1];		 		
}
	
/**
 * Trims a string
 * 
 * @param s
 * @return
 */
tpc.trim = function(s) {
    try {
        return s.replace(/^\s*|\s*$/g, "");
    } catch (error) {
        return s;
    }		
};
	

/*
 * Email to Friend functionality for an article
 */
tpc.emailArticle = function() {
	tpc_e_init();
	$('#email-article').modal();
}


/* 
 * --------------------------------------------------------------------------------------------
 * Simple JSON Restful Request
 * 
 * A simple class for making HTTP requests using dynamically generated script tags and JSON
 * --------------------------------------------------------------------------------------------
 */
function tpcjsonreq(url) {
	this.url = url;
	this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
	this.head = document.getElementsByTagName('head').item(0);
	this.scriptId = 'JScriptID' + tpcjsonreq.scriptCounter++;
}

// static script id counter
tpcjsonreq.scriptCounter = 1;

/*
 * Creates a script object tag
 */
tpcjsonreq.prototype.buildScriptTag = function() {
	this.scriptObj = document.createElement("script");
	this.scriptObj.setAttribute("type", "text/javascript");
	this.scriptObj.setAttribute("charset", "utf-8");
	this.scriptObj.setAttribute("src", this.url + this.noCacheIE);
	this.scriptObj.setAttribute("id", this.scriptId);
}
/*
 * Deletes the <script> tag from the dom
 */
tpcjsonreq.prototype.removeScriptTag = function() {
	this.head.removeChild(this.scriptObj);
}
/*
 * Adds the <script> tag to the dom
 */
tpcjsonreq.prototype.addScriptTag = function() {
	this.head.appendChild(this.scriptObj);
}

