﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("TUI.TipsCommunity.Templates.Units");

TUI.TipsCommunity.Templates.Units.TipPanel = function(element) 
{
    TUI.TipsCommunity.Templates.Units.TipPanel.initializeBase(this, [element]);
    
    this._$element = $(element);
}

TUI.TipsCommunity.Templates.Units.TipPanel.prototype = 
{
    initialize: function() 
    {
        TUI.TipsCommunity.Templates.Units.TipPanel.callBaseMethod(this, 'initialize');
        if (this.get_$imageLightBoxAnchors().length > 1)
        {
            $addHandlers(this._$element.find('div.panel-center img.prev').get(0), { 'click' : this.previousClick }, this);
            $addHandlers(this._$element.find('div.panel-center img.next').get(0), { 'click' : this.nextClick }, this);
        }
        
        this._$element.find('div.panel-center span.collapse-comments').click( Function.createDelegate(this, this.collapseCommentsClick) );
        
        this.updateCounter();
    },
    dispose: function() 
    {
        TUI.TipsCommunity.Templates.Units.TipPanel.callBaseMethod(this, 'dispose');
    },
    previousClick: function (e) 
    {
        this.get_$imageLightBoxAnchors().filter(String.format(':eq({0})', this.get_currentIndex())).hide();
        this.set_currentIndex(this.get_currentIndex() == 0 ? this.get_$imageLightBoxAnchors().length - 1 : this.get_currentIndex() - 1);
        this.get_$imageLightBoxAnchors().filter(String.format(':eq({0})', this.get_currentIndex())).show();
        this.updateCounter();
    },
    nextClick: function (e) 
    {
        this.get_$imageLightBoxAnchors().filter(String.format(':eq({0})', this.get_currentIndex())).hide();
        this.set_currentIndex(this.get_currentIndex() == this.get_$imageLightBoxAnchors().length - 1 ? 0 : this.get_currentIndex() + 1);
        this.get_$imageLightBoxAnchors().filter(String.format(':eq({0})', this.get_currentIndex())).show();
        this.updateCounter();
    },
    collapseCommentsClick: function (e) 
    {
        this._$element.find('div.comments-list').get(0).AccordionExtender.onToggleClick();
    },
    updateCounter: function() 
    {
        this._$element.find('div.panel-center span.count-current').text(this.get_currentIndex() + 1);
        this._$element.find('div.panel-center span.count-total').text(this.get_$imageLightBoxAnchors().length);        
    },
    get_$imageLightBoxAnchors: function() 
    {
        if (this._$imageLightBoxAnchors == null)
        {
            this._$imageLightBoxAnchors = this._$element.find('div.panel-center div.images a.thickbox');
        }
        return this._$imageLightBoxAnchors;
    },
    set_$imageLightBoxAnchors: function(value) 
    {
        this._$imageLightBoxAnchors = value;
    },
    get_currentIndex: function() { return this._currentIndex; },
    set_currentIndex: function(value) { this._currentIndex = value; }
}
TUI.TipsCommunity.Templates.Units.TipPanel.registerClass('TUI.TipsCommunity.Templates.Units.TipPanel', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
