﻿/**
*	Javascript Tools based on jquery
*
*	@date		2009-02-12
*	@author		Michal Gondar
*	@copyright	Live Nation (Music) UK
*   @require    jquery
*   @jquery     jquery-1.3.1.min.js
*
*   This is javascript class smooth for show / hide content
*   used on news, itckets
*/


/*
*   <div id="ticket-holder">
*       <a name=""></a>
*       <h3></h3>
*       <div class="ticket-description fieldActive">
*           <h4></h4>
*       </div>
*       <div class="ticket-body">
*
*           ** CONTENT **
*
*       </div>
*   </div>
*/


/**
*   @use for    tickets,news
*   @function   open/hide 
*/
var jsShowHideObj = {

    // main div for all content on what is applayed this effect
    mainHolder: null,
    // class of each post
    descriptionClass: null,
    // class of each post
    bodyClass: null,
    // array of clickable links
    aDescps: new Array(),
    // array of post body what ll be hidden
    aBodies: new Array(),
    // intereg, first detect opened box by class
    first: 0,

    // initialization function
    init: function(mainHolder, descriptionClass, bodyClass) {
        this.mainHolder = $('#' + mainHolder);
        this.aDescps = $('#' + mainHolder + ' .' + descriptionClass);
        this.aBodies = $('#' + mainHolder + ' .' + bodyClass);

        $("<span>+ </span>").prependTo($('#' + mainHolder + ' .' + descriptionClass));

        this.makeLinks();
        this.fastHideAllPosts();
        //this.openFirstPost();
    },

    // hide all post bodies inside holder
    hideAllPosts: function() {
        this.aBodies.slideUp();
        this.aDescps.removeClass('fieldActive');
    },

    // hide all post bodies inside holder
    fastHideAllPosts: function() {
        this.aBodies.hide();
        this.aDescps.removeClass('fieldActive');
    },

    // open all post bodies inside holder
    fastOpenAllPosts: function() {
        this.aBodies.show();
        this.aDescps.addClass('fieldActive');
    },

    // open first post
    openFirstPost: function() {
        this.openPost(this.first);
    },

    // open post by selected i in array
    openPost: function(i) {
        $(this.aBodies[i]).slideDown("slow");
        $(this.aDescps[i]).addClass('fieldActive').find("span").html("- ");
    },

    // close post by selected i in array
    closePost: function(i) {
        $(this.aBodies[i]).slideUp();
        $(this.aDescps[i]).removeClass('fieldActive').find("span").html("+ ");
    },

    // create clicking descriptions and functionality
    makeLinks: function(holder) {
        for (i = 0; i < this.aDescps.length; i++) {
            this.aDescps[i].indicator = i;
            $(this.aDescps[i]).click(function() {
                // if active
                if ($(this).hasClass('fieldActive')) {
                    jsShowHideObj.closePost(this.indicator);
                } else {
                    //jsShowHideObj.hideAllPosts();
                    jsShowHideObj.openPost(this.indicator);
                }
                return false;
            });
            $(this.aDescps[i]).css('cursor', 'pointer');
            if ($(this.aDescps[i]).hasClass('fieldActive')) {
                this.first = i;
            }
        }

    }
};
