﻿/// <reference path="~/Scripts/jquery-1.3.2-vsdoc.js" />

jQuery.fn.expando = function(options) {

    return this.each(function() {

        settings = jQuery.extend({
            width: 50,
            cutofftext: 200
        }, options);

        var plugin = this;

        var toggleOn = true;
        var fullText = $(plugin).find("div.SpecialsTextContent").html();
        var text = $(plugin).find("div.SpecialsTextContent").text();
        var index = text.indexOf(" ", settings.cutofftext);
        if (index <= 0) {
            index = settings.cutofftext;
        }
        var shortText = text.substr(0, index);

        function toggleOnOff() {
            $(plugin).find("div.SpecialsTextContent").children().remove();
            var label = "";
            var id = "";
            if (toggleOn) {
                $(plugin).find("div.SpecialsTextContent").html("<p>" + shortText + " ... </p>");
                label = "View more...";
                id = "Open";
            }
            else {
                $(plugin).find("div.SpecialsTextContent").html(fullText);
                label = "Close";
                id = "Close";
            }

            //Inject an image into the div
            var image = $("<span class=\"SpecialsTextButton_" + id + "\">" + label + "</span>");
            $(plugin).find("div.SpecialsTextContent").append(image);
            image.click(toggleOnOff);

            toggleOn = !toggleOn;
        }

        toggleOnOff();
    });
};

$(function() {
    $("div.SpecialsText").expando();
});