﻿var LightBox = function(box) {
    var numeric = /[^0-9]+/g;
    var self = this;
    var videoVisible = true;

    this.show = function() {
        this.mask.css('opacity', 0.7);
        this.mask.fadeIn("fast");
        this.mask.height(document.body.clientHeight + 20);
        this.box.fadeIn("slow");
        this.visible = true;
    };
    this.hide = function() {
        this.mask.fadeOut("slow");
        this.box.fadeOut("fast");
        this.visible = false;
    };
    this.toggle = function() {
        if (this.visible)
            this.hide();
        else
            this.show();
    };
    this.buildMask = function() {
        if (this.mask)
            return this.mask;
        this.mask = this.createElement("div", { id: "mask" });
        jQuery(this.mask).hide();
        document.body.appendChild(this.mask);
        this.mask = jQuery(this.mask);
    };
    this.createElement = function(tag, opts) {
        var ele = document.createElement(tag);
        jQuery.extend(ele, opts || {});
        return ele;
    };
    this.centerBox = function() {
        var center = this.calculateBoxPosition();
        this.box.css(center);
        this.box.css("left", "50%");
        this.box.css("margin-left", "-313px");
    };
    this.calculateBoxPosition = function() {
        var boxHeight = parseInt(this.box.css("height").replace(numeric, ''));
        var boxWidth = parseInt(this.box.css("width").replace(numeric, ''));
        var center = this.findWindowCenter();
        var left = center.width - boxWidth / 2;
        var top = $(window).height() < boxHeight + 25 ? 20 : center.height - boxHeight / 2;
        return { left: left + "px", top: top + "px" };
    };
    this.findWindowCenter = function() {
        return { height: $(window).height() / 2, width: $(window).width() / 2 };
    };
    this.floatCorner = function() {
        this.corner.css("float", "right");
    };
    function handleCloseButton(e) {
        self.close();
        self.hide();
        if (self.href)
            window.location = self.href;

    }

    function handleReplayButton(e) {
        if (!videoVisible)
            self.swap();
        else
            self.start();
    }

    this.getFlashElement = function() {
        self.flashElement = self.box.find('object')[0];
       
    };

    this.stop = function() {
        if(self.flashElement)
        self.flashElement.reset();
    };

    this.start = function() {
        if(self.flashElement)
        self.flashElement.replay();
    };

    this.close = function() {
        try {
            if (videoVisible) {
                self.flashElement.close();
            }
        } catch(e) {}
    };
    this.swap = function() {

        if (videoVisible) {
            self.flashElement.style.display = 'none';
            self.imageContainer.show();
            videoVisible = false;
        }
        else {
            self.flashElement.style.display = 'block';
            self.imageContainer.hide();
            videoVisible = true;
        }
    };

    this.buildMask();
    this.box = jQuery(box);
    this.closeButton = this.box.find('.closeButton');
    this.replayButton = this.box.find('.replayBtn');
    //this.videoContainer = this.box.find('#videoContainer');
    this.imageContainer = this.box.find('#imageContainer');

    this.closeButton.click(handleCloseButton);
    this.replayButton.click(handleReplayButton);
    this.visible = false;
    document.body.appendChild(box);
    this.centerBox();
    this.corner = this.box.find('.corner');
    this.floatCorner();

};
