/***************************************************************************************************
FILE:	core.js
AUTHOR:	Kris Felscher (me@krisfelscher.com)
DATE:	06/12/2009
DESCRIPTION:
	This file contains core javascript functionality used on KrisFelscher.com

VERSION HISTORY:
AUTHOR		DATE		VERSION		DESCRIPTION
----------------------------------------------------------------------------------------------------
KPF		06/12/2009	1.0		Initial Version

REQUIREMENTS:
----------------------------------------------------------------------------------------------------
	jQuery 1.3.2 (http://www.jquery.com)

****************************************************************************************************
Global Members
***************************************************************************************************/
var _tmplCache = {}	//used by ParseTemplate to store templates

/* ParseTemplate ***********************************************************************************
	Client side template parser that uses &lt;#= #&gt; and &lt;# code #&gt; expressions.
	and # # code blocks for template expansion.

	Parameters:
    		String str :	The text of the template to expand    
    		Object data : 	Any data that is to be merged. Pass an object and that object's 
    				properties are visible as variables.

    	Returns : string
***************************************************************************************************/
this.ParseTemplate = function(str, data) {
    var err = "";
    try {
        var func = _tmplCache[str];
        if (!func) {
            var strFunc = "var p=[],print=function(){p.push.apply(p,arguments);};"
                        + "with(obj){p.push('"
                        + str.replace(/[\r\t\n]/g, " ")
                          .replace(/'(?=[^#]*#>)/g, "\t")
                          .split("'").join("\\'")
                          .split("\t").join("'")
                          .replace(/<#=(.+?)#>/g, "',$1,'")
                          .split("<#").join("');")
                          .split("#>").join("p.push('")
                        + "');}return p.join('');";
            func = new Function("obj", strFunc);
            _tmplCache[str] = func;
        }
        return func(data);
    } catch (e) { err = e.message; }
    return "< # ERROR: " + err + " # >";
}

/* ScrollTo ****************************************************************************************
	Scrolls the page to a specific section on the page. This is useful for appending content
	via AJAX because it allows the user to move along with the new items being added.
***************************************************************************************************/
this.ScrollTo = function(selector) {
	var targetOffset = $(selector).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 500);
}

/* TimeStampToDate *********************************************************************************
	Converts an XML Timestamp to a Javascript Date Object.
***************************************************************************************************/
this.TimeStampToDate = function(xmlDate) {
    var dt = new Date();
    var dtS = xmlDate.slice(xmlDate.indexOf('T')+1, xmlDate.indexOf('.'))
    var TimeArray = dtS.split(":");
    dt.setUTCHours(TimeArray[0],TimeArray[1],TimeArray[2]);
    dtS = xmlDate.slice(0, xmlDate.indexOf('T'))
    TimeArray = dtS.split("-");
    dt.setUTCFullYear(TimeArray[0],TimeArray[1]-1,TimeArray[2]);
    return dt;
}

/* DateToTimeStamp *********************************************************************************
	Converts a JavaScript Date object to an XML Timestamp
***************************************************************************************************/
this.DateToTimeStamp = function(dt) {
	return dt.getFullYear() 
	       + "-" + (dt.getMonth() + 1)
	       + "-" + dt.getDate()
	       + "T" + dt.getHours()
	       + ":" + dt.getMinutes()
	       + ":" + dt.getSeconds()
	       + "." + dt.getTimezoneOffset() + "Z";
	       
}

