/*
Copyright (c) <2010> Israel Cordeiro da Fonseca, http://www.metanoianet.com

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

*/
/// <reference path="jquery-1.4.1-vsdoc.js" />
jQRepeater = function(rptClass, headerClass, itemClass, footerClass, validateRptType, execContructor)
{
    /// <summary>
    /// Generates an object of type repeater
    /// </summary>
    /// <param name="rptClass">Class name of the container </param>
    /// <param name="headerClass">Class name of the header</param>
    /// <param name="itemClass">Class name of the itemTemplate</param>
    /// <param name="footerClass">Class name of the footer</param>
    /// <param name="validateRptType">Whether true or undefined the repeater will validate whether there jqrepeater the attribute with the value true. Unnecessary if you use the jQuery way "jQuery('#myContainer').jQRepeater('header', 'item', 'footer')"</param>    
    /// <param name="execContructor">Whether true or undefined the repeater will start the builder. Use if you need to change the header, footer or item template at runtime.</param>

    this.validatebyType = '';
    if (validateRptType == undefined || validateRptType == true)
        this.validatebyType = '[jqrepeater=true]';
    if (typeof rptClass === "string")
    {

        this.Name = rptClass;
        this.Container = jQuery('.' + this.Name + this.validatebyType);
    }
    else
    {

        this.Name = '';
        this.Container = rptClass;
    }

    this.Html = this.Container.html();
    this.headerClass = headerClass;
    this.itemClass = itemClass;
    this.footerClass = footerClass;
    this.IndicatorIni = "(%";
    this.IndicatorEnd = "%)";
    this.Header = null;
    this.Footer = null;
    this.ItemTemplate = null;

    this.BindTemplate = function(template, DataItem, CurrentIndex, TotalItems, DataList)
    {
        /// <summary>
        /// Bind the template with the content and returns the result
        /// </summary>
        /// <param name="template">Html template</param>
        /// <param name="DataItem">Item</param>
        /// <param name="CurrentIndex">Current Index of DataList</param>
        /// <param name="TotalItems">Total Items</param>
        /// <param name="DataList">Original DataList</param>
        
        if (template == undefined || template == null)
            return '';
        else
            template = unescape(template);

        if (DataItem == null)
            DataItem = '';

        var rt = '';
        var idx = 0;
        for (var i = 0; i < template.length; i++)
        {
            i = template.indexOf(this.IndicatorIni, i);
            if (i < 0)
            {
                rt += template.substring(idx, template.length);
                break;
            }
            rt += template.slice(idx, i);
            rt += eval(template.slice(i + this.IndicatorIni.length, template.indexOf(this.IndicatorEnd, i)));
            i = idx = (template.indexOf(this.IndicatorEnd, i) + this.IndicatorEnd.length);
        }
        return rt;
    }

    this.Bind = function(DataList)
    {
        /// <summary>
        /// Runs the repeater, as it exists in every template
        /// </summary>
        /// <param name="DataList">Array containing the items</param>
        this.DataList = DataList;
        var lenFor = DataList.length;
        var htmlItem = '';
        this.CurrentIndex = 0;
        if (this.Header != undefined && this.Header.length > 0)
            htmlItem = this.BindTemplate(this.Header,null, 0, DataList.length, DataList);

        this.Container.hide(1);

        if (this.ItemTemplate != undefined && this.ItemTemplate.length > 0)
        {
            for (var i = 0; i < lenFor; i++)
            {
                this.CurrentIndex = i + 1;
                htmlItem += this.BindTemplate(this.ItemTemplate, DataList[i], i, DataList.length, DataList);
            }
        }

        if (this.Footer != undefined && this.Footer.length > 0)
            htmlItem += this.BindTemplate(this.Footer, null, 0, DataList.length, DataList);

        this.Container.html(htmlItem);

        this.Container.show(1);
    }

    this.ReturnTemplate = function(templateClass, templateContainer)
    {
        /// <summary>
        /// Returns the contents of the template informed through classes
        /// </summary>

        return templateContainer.find('.' + templateClass).html();
    }

    this.Clear = function()
    {
        /// <summary>
        /// Clears the contents of the repeater
        /// </summary>
        this.Container.text('');
    }

    this.InitTemplates = function()
    {
        /// <summary>
        /// Reads the template and generates the header, footer and item if any, should be performed before the bind ()
        /// </summary>
        if (this.headerClass != undefined && this.headerClass.length > 0)
            this.Header = this.ReturnTemplate(this.headerClass, this.Container);

        if (this.footerClass != undefined && this.footerClass.length > 0)
            this.Footer = this.ReturnTemplate(this.footerClass, this.Container);

        if (this.itemClass != undefined && this.itemClass.length > 0)
            this.ItemTemplate = this.ReturnTemplate(this.itemClass, this.Container);
    }

    if (execContructor == undefined || execContructor == true)
        this.InitTemplates();

    return this;

}

jQuery.fn.extend(
{
    jQRepeater: function(headerClass, itemClass, footerClass)
    {
        /// <summary>
        /// Generates an object of type repeater
        /// </summary>        
        /// <param name="headerClass">Class name of the header</param>
        /// <param name="itemClass">Class name of the itemTemplate</param>
        /// <param name="footerClass">Class name of the footer</param>
        return new jQRepeater(this, headerClass, itemClass, footerClass, false, true);
    }
}
);

