/**
 *
 *
 * Created by: Becklyn GmbH, Jannik Zschiesche <jz@becklyn.com>
 * Date: 30.06.2011
 * Time: 5:44 PM
 */

$(
        function ()
        {
            Show_State.init();
            Index_Popup.init(popupImages);

            $('#resetCookie').click(
                function ()
                {
                    Index_Popup.resetSeenState();
                }
            ).css("cursor", "pointer");
        }
);


var Show_State = {
    $element: null,

    init: function () {
        this.$element = $('#showState');
    },

    setToYes: function () {
        this.$element.html("yes");
    },

    setToNo: function () {
        this.$element.html("no");
    }
};


/**
 * Handles the index popup
 */
var Index_Popup =
{
    /**
     * The jQuery-Handler of the wrapping background
     * @private
     * @type jQuery
     */
    $popupBackgroundWrap: null,


    /**
     * The data of the image to use
     * @private
     * @type Object
     */
    imageData: "",


    /**
     * The image jQuery-handler
     * @private
     * @type jQuery
     */
    $imageHandler: null,


    /**
     * Flag, whether the popup is visible
     * @private
     * @type Boolean
     */
    popupIsVisible: false,


    /**
     * The name of the cookie
     * @private
     * @type String
     * @constant
     */
    COOKIE_NAME: "indexPopupSeen",


    /**
     * Initializes the component
     *
     * @param popupImages
     */
    init: function (popupImages)
    {
        this._hasAlreadySeenPopup() ? Show_State.setToNo() : Show_State.setToYes();

        // do nothing, if is called with an empty array
        if ((0 == popupImages.length) || !$.isArray(popupImages) || this._hasAlreadySeenPopup())
        {
            return;
        }

        this.imageData = this._getRandomImage(popupImages);
        this._preloadImage();
    },



    /**
     * Returns a random image (= entry) of the given array
     *
     * @private
     * @param popupImages Array all available images
     * @return returns a random entry of the array
     * @type string
     */
    _getRandomImage: function (popupImages)
    {
        var randomIndex = Math.floor(Math.random() * popupImages.length);
        return popupImages[randomIndex];
    },



    /**
     * Preloads the image and starts the application afterwards
     * @private
     */
    _preloadImage: function ()
    {

        var self = this;

        this.$imageHandler = $('<img />').load(
                function ()
                {
                    self._startApplication();
                }
        ).attr("alt", "Popup Image").attr("src", this.imageData.image);
    },



    /**
     * Starts the application: builds the popup, shows the popup, registers the listeners, etc...
     * @private
     */
    _startApplication: function ()
    {
        this._preparePopup();
        this._showPopup();
        this._registerClickHandler();
    },



    /**
     * Prepares the popup
     * @private
     */
    _preparePopup: function ()
    {
        this.$popupBackgroundWrap = $('<div id="index-popup-wrap"></a></div>"');
        $('body').append(this.$popupBackgroundWrap);
    },



    /**
     * Sets the image to the element and adds the texts
     * @private
     */
    _setImageAndText: function ()
    {
        this.$popupBackgroundWrap.append( this.$imageHandler );
        var $textWrap = $('<div id="index-popup-wrap-text"></div>');

        var $content = $('<div class="content"></div>');
        $content.html(this.imageData.text);
        $textWrap.append($content);

        var $enter = $('<span class="enter-text"></span>');
        $enter.html('Enter //');
        $textWrap.append($enter);

        this.$popupBackgroundWrap.append($textWrap);
    },



    /**
     * Shows the popup
     * @private
     */
    _showPopup: function ()
    {
        location.href = "http://muladhara.ch/#!prettyPhoto/0/";
        this.popupIsVisible = true;
        this._setPopupToSeen();
    },



    /**
     * Registers the click handler to close the popup
     * @private
     */
    _registerClickHandler: function ()
    {

        var self = this;

        this.$popupBackgroundWrap.click(
                function ()
                {
                    self._hidePopup();
                }
        );
    },



    /**
     * Hides the popup
     * @private
     */
    _hidePopup: function ()
    {
        this.popupIsVisible = false;
        this.$popupBackgroundWrap.fadeOut("normal");
    },



    /**
     * Returns, whether the user has already seen the popup
     * @private
     */
    _hasAlreadySeenPopup: function ()
    {
        return ("yes" == $.cookie(this.COOKIE_NAME));
    },



    /**
     * Sets the popup to "seen"
     * @private
     */
    _setPopupToSeen: function ()
    {
        Show_State.setToNo();
        if (!this._hasAlreadySeenPopup())
        {
            // the cookie is valid for 7 days
            var validForDays = 7;
            $.cookie(this.COOKIE_NAME, "yes", { expires: validForDays });
        }
    },



    /**
     * Resets the seen state
     */
    resetSeenState: function ()
    {
        Show_State.setToYes();
        $.cookie(this.COOKIE_NAME, null);
    }
};
