/**
 * Callback class for kino.de.
 * This is used in coverflow players when klicking the image in the center.
 */

if ('undefined' === typeof Emv.Kinode.Callback) {

    Emv.Kinode.Callback = {};
}

Emv.Kinode.Callback.Fullpage = function(forwardUrl) {

    this.forwardUrl = forwardUrl;

    this.imageLoaded = function(event) {

        this.lastEvent = event;
    };

    this.imageClicked = function(event) {

        this.routeToFullPage(event);
    };

    this.routeToFullPage = function(event) {

        location.href = this.forwardUrl + '?imageId=' + event.params.imageId;
    };
};

Emv.Kinode.Callback.ZoomDialog = function(overlayId) {

    this.overlayId = overlayId;
    this.lastEvent = null;
    this.imgMode   = 'b600x400';

    this.imageClicked = function(event) {

        this.showOverlayForEvent(event);
    };

    this.showImageForLastEvent = function() {

        this.showOverlayForEvent(this.lastEvent);
    };

    this.imageLoaded = function(event) {

        this.lastEvent = event;
    };

    this.showOverlayForEvent = function(event) {

        imgUrl      = event.params.imageUrl;
        imgCredit   = (event.params.copyright !== '') ? ' (Foto: ' + event.params.copyright +')' : '';
        imgHeadline = unescape(event.params.text + imgCredit); 

        // Replace default image size coming from fpc to boxed format.
        imgUrl = imgUrl.replace(new RegExp('[bfwmh0-9]*\.jpg',  'gi'), this.imgMode + '.jpg');

        var dialog = new Emv.Dialog();
        dialog.showImagePopup(imgUrl, imgHeadline, '', '');

//        var overlaySubtitle = document.getElementById(this.overlayId + '_subtitle');
//        if (overlaySubtitle) {
//
//            overlaySubtitle.innerHTML = unescape(event.params.text + copy);
//        }
    };
};

/**
 * Called from coverflows and redirects to fotoshow detailpage.
 * 
 * @param integer|null entityId 
 * @param string|null title
 * @param string type
 */
Emv.Kinode.Callback.FullPicture = function(entityId, title, type) {

    /**
     * Callback variables.
     * 
     * @var mixed
     */
    this.entityId     = entityId;
    this.title        = title;
    this.type         = type;
    this.firstImageId = null;
    this.lastEvent    = null;

    /**
     * Run when a image was clicked.
     * 
     * @param object event
     */ 
    this.imageClicked = function(event) {

        // Check for first gallery image and title when entityId and/or title is null  
        if (event.params.imageId.indexOf('#') !== -1) {

            imageData         = event.params.imageId.split('#');
            this.entityId     = imageData[0];
            this.firstImageId = imageData[1];

            if (/\/([\w-]+)\/\d+$/i.exec(event.params.link)) {

                this.title = RegExp.$1;
            }
        } else {

            this.firstImageId = event.params.imageId;
        }

        // Finally redirect to fotoshow-detailpage
        self.location.href = '/' + this.type
                           + '/' + this.title
                           + '/fotoshow/' + this.entityId
                           + (this.firstImageId !== null ? '/' + this.firstImageId : '');

        return;
    };

    /**
     * Called image was loaded, but nothing clicked.
     * 
     * @param object event
     */
    this.imageLoaded = function(event) {

        this.lastEvent = event;
    };

    /**
     * Call imageClicked-method with current event data.
     * 
     * @param object event
     */
    this.showImageForLastEvent = function(event) {

        this.imageClicked(this.lastEvent);
    };
};
