﻿/**
*	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="fieldDescription fieldActive">
*           <h4></h4>
*       </div>
*       <div class="fieldBody">
*
*           ** 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);

		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() {
		var firstID = this.getPostIdFromUrl();
		if (firstID)
			if (this.openPostByID(firstID))
		{ }
		else
			this.openPost(this.first);
		else
			this.openPost(this.first);
	},

	// get post number from url
	getPostIdFromUrl: function() {
		var pathArray = window.location + '';
		pathArray = pathArray.split('#');
		if (pathArray[1] == undefined)
			return false;
		else
			return pathArray[1];
	},

	// open post by selected ID
	openPostByID: function(id) {
		for (i = 0; i < this.aDescps.length; i++) {
			if (this.aDescps[i].id.substring(2) == id) {
				$(this.aBodies[i]).slideDown("slow");
				$(this.aDescps[i]).addClass('fieldActive');
				return true;
			}
		}
		return false;
	},

	// open post by selected i in array
	openPost: function(i) {
		$(this.aBodies[i]).slideDown("slow");
		$(this.aDescps[i]).addClass('fieldActive');
	},

	// close post by selected i in array
	closePost: function(i) {
		$(this.aBodies[i]).slideUp();
		$(this.aDescps[i]).removeClass('fieldActive');
	},

	// 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);
				}
				// deep linking
				window.location = "#" + this.id.substring(2);
				return false;
			});
			$(this.aDescps[i]).css('cursor', 'pointer');
			if ($(this.aDescps[i]).hasClass('fieldActive')) {
				this.first = i;
			}
			// deep linking
			this.aDescps[i].id = "JS" + this.aDescps[i].id;
		}

	}
};



// without deep linking
var jsShowHideObj1 = {

    // 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 );

        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' );
    },

    // close post by selected i in array
    closePost: function( i ) {
        $( this.aBodies[i] ).slideUp();
        $( this.aDescps[i] ).removeClass( 'fieldActive' );
    },

    // 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' ) ) {
                    jsShowHideObj1.closePost(this.indicator);
                } else {                
                    jsShowHideObj1.hideAllPosts();
                    jsShowHideObj1.openPost( this.indicator );
                }
                return false;
            });
            $( this.aDescps[i] ).css( 'cursor', 'pointer' );
            if( $( this.aDescps[i] ).hasClass( 'fieldActive' ) ) {
                this.first = i;
            }
        }
        
    }   
};
