﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../js-docs/jquery-1.2.6-vsdoc.js" />
/// <reference path="./Helpers.js" />
/// <reference path="./Timer.js" />
/// <reference path="./ClientEventPool.js" />
/// <reference path="./FeatureBox.js" />

Type.registerNamespace("NextDigital.CityOfBallarat.Web.scripts");

NextDigital.CityOfBallarat.Web.scripts.FeatureBox = function(element) {
    NextDigital.CityOfBallarat.Web.scripts.FeatureBox.initializeBase(this, [element]);
    this._servicePath = '/services.asmx';
    this._firstLoadMethod = 'GetFeature';
    this._nextMethod = 'GetNextFeature';
    this._prevMethod = 'GetPrevFeature';
    this._featureRootId = 0;
    this._currentId = 0;
    this._updateInterval = 30000;
    this._timer = null;
}

NextDigital.CityOfBallarat.Web.scripts.FeatureBox.prototype = {
    initialize: function() {
        NextDigital.CityOfBallarat.Web.scripts.FeatureBox.callBaseMethod(this, 'initialize');
        // Add custom initialization here
        this._timer = new NextDigital.CityOfBallarat.Web.scripts.Timer(this._updateInterval);
        this._timer.add_tick(Function.createDelegate(this, this.nextFeature));

        if (this._featureRootId !== 0) {
            Sys.Net.WebServiceProxy.invoke(this._servicePath, this._firstLoadMethod, false, { featureRootId: this._featureRootId }, Function.createDelegate(this, this._showFeature));
        }

        this._tojQuery().find('#next').click(Function.createDelegate(this, this.nextFeature));
        this._tojQuery().find('#prev').click(Function.createDelegate(this, this.prevFeature));

        ClientEventPool.addEvent('stopFeatureBox', Function.createDelegate(this, this._stop));
        ClientEventPool.addEvent('startFeatureBox', Function.createDelegate(this, this._start));
    },
    dispose: function() {
        //Add custom dispose actions here
        NextDigital.CityOfBallarat.Web.scripts.FeatureBox.callBaseMethod(this, 'dispose');
    },
    _tojQuery: function() {
        ///	<summary>
        ///		Converts the current object into a jQuery object
        ///	</summary>
        ///	<returns type="jQuery" />
        return $('#' + this.get_element().id);
    },
    nextFeature: function() {
        this._timer.set_enabled(false);
        Sys.Net.WebServiceProxy.invoke(this._servicePath, this._nextMethod, false, { featureRootId: this._featureRootId, currFeatureId: this._currentId }, Function.createDelegate(this, this._showFeature));
        return false;
    },
    prevFeature: function() {
        this._timer.set_enabled(false);
        Sys.Net.WebServiceProxy.invoke(this._servicePath, this._prevMethod, false, { featureRootId: this._featureRootId, currFeatureId: this._currentId }, Function.createDelegate(this, this._showFeature));
        return false;
    },
    set_featureRootId: function(val) {
        if (this._featureRootId !== val) {
            this._featureRootId = val;
            Sys.Application.raisePropertyChanged("featureRootId");
        }
    },
    get_featureRootId: function() {
        return this._featureRootId;
    },
    _showFeature: function(feature, instance) {
        var f = Sys.Serialization.JavaScriptSerializer.deserialize(feature);
        var j = this._tojQuery();
        //j.hide('slow', Function.createDelegate(this, function() {
            var heading = j.find('#featureHeading');
            heading.text(f.Text);

            if (f.MoreUrl != null && f.MoreUrl != '') {
                heading.attr("href", f.MoreUrl);
                heading.attr("target", f.Target);
            }
            else {
                heading.attr("href", "#");
                heading.attr("target", "");
            }

            var desc = j.find('#featureDesc');
            if (f.description != null && f.description != '') {
                if (f.description.length > 150) {
                    f.description = f.description.substr(0, 147) + '...';
                }
                desc.text(f.description);
                desc.css("display", "block");
            } else {
                desc.text("");
                desc.css("display", "none");
            }

            var img = j.find('#featureImage');
            if (f.ImageUrl !== null && f.ImageUrl !== '') {
                img.attr('src', f.ImageUrl);
                if (!f.ImageAlt || f.ImageAlt.length === 0) {
                    img.attr('alt', 'Feature Image');
                } else {
                    img.attr('alt', f.ImageAlt);
                }
                img.parent().show();
            } else {
                img.parent().hide();
                img.attr('src', '');
            }
            var subHeading = j.find('#more');
            var imgLink = j.find('#imgLink');
            if (f.MoreUrl !== null && f.MoreUrl !== '') {
                subHeading.attr('href', f.MoreUrl);
                subHeading.attr('target', f.Target);

                imgLink.attr('href', f.MoreUrl);
                imgLink.attr('target', f.Target);
            } else {
                subHeading.attr('href', "#");
                subHeading.attr('target', "");

                imgLink.attr('href', "#");
                imgLink.attr('target', "");
            }

            var featureSubHeading = j.find("#featureSubHeading");            
            if (f.sub_heading != null && f.sub_heading != '') {
                subHeading.text(f.sub_heading);
                subHeading.css("display", "block");
                featureSubHeading.css("display", "block");
            } else {
                subHeading.text("");
                subHeading.css("display", "none");
                featureSubHeading.css("display", "none");
            }

            this._set_currentId(f.Id);

            //j.show('slow');
            this._timer.set_enabled(true);

            j.effect('slide');

        //}));
    },
    _set_currentId: function(val) {
        if (this._currentId !== val) {
            this._currentId = val;
            Sys.Application.raisePropertyChanged("currentId");
        }
    },
    get_currentId: function() {
        return this._currentId;
    },
    _stop: function() {
        this._timer.set_enabled(false);
    },
    _start: function() {
        this._timer.set_enabled(true);
    }
}
NextDigital.CityOfBallarat.Web.scripts.FeatureBox.registerClass('NextDigital.CityOfBallarat.Web.scripts.FeatureBox', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
