/****************************************************************************
 *  Preview Window
 *  MUST INCLUDE widgets_array.js with this file
 *  Must INCLUDE MMCUtilities.js with this file
 ****************************************************************************/

/*
 * Constructor
 * Input
 * _title = windows title [string]
 * _showPrintPreviewLink = show the print preview link [boolean]
 *  
 *  optional
 *  @width - defaults to 658px, width of MCC Preview window
 *  @top - pass in new top position for Preview window
 *  @left - pass in new left position for Preview window
 */
function PreviewWindow(_title, _showPrintPreviewLink)
{
    var title = _title;
    var showPrintPreviewLink = _showPrintPreviewLink;
    var top = (document.documentElement.scrollTop || document.body.scrollTop) + 50;
    var left = 100;
    var href;
    
    var width = 658;
    if(arguments.length == 3)
	{
		width = arguments[2];
	}
	else if(arguments.length == 4)
	{
		width = arguments[2];
		top = arguments[3];
	}
	else if(arguments.length == 5)
	{
		width = arguments[2];
		top = arguments[3];
		left = arguments[4];
	}

    this.getTitle = function() { return title; };
    this.setTitle = function(value) { title = value; };
    this.getShowPrintPreviewLink = function() { return showPrintPreviewLink; };
    this.setShowPrintPreviewLink = function(value) { showPrintPreviewLink = value; };
    this.getTop = function() { return top; };
    this.setTop = function(value) { top = parseInt(value); };
    this.getLeft = function() { return left; };
    this.setLeft = function(value) { left = parseInt(value); };
    this.getHref = function() { return href; };
    this.setHref = function(value) { href = value; };
    this.getWidth = function() { return width; };
    this.windowHandle;
    this.contentHandle;

    //remove 
    while(this.exists())
        this.remove();
    
    //redraw
    this.draw();
}

PreviewWindow.prototype = 
{
    /*
     * Draws the preview window and adds it into the DOM
     * called from this.show();
     */
    draw : function()
    {
        var showPrintPreviewLink = this.getShowPrintPreviewLink();
        var width = this.getWidth();

        var wrapperDiv = document.createElement('div');
        wrapperDiv.id = 'piecePreview';
        wrapperDiv.style.width = width + 'px';
        
        var titleP = document.createElement('p');
        if(showPrintPreviewLink)
        {
            titleP.className = 'title';
            titleP.style.width = width - 170 + 'px';
        }
        else
        {
            titleP.className = 'titleNoPrintPreview';
            titleP.style.width = width - 33 + 'px';
        }
        titleP.style.backgroundImage = 'url(' + globalMediaPath + '/Campaigns/MMCMovePreview.img)';
        titleP.style.backgroundRepeat = 'no-repeat';
        titleP.innerHTML = '<b>' + this.getTitle() + '</b>';
        wrapperDiv.appendChild(titleP);
        
        var closeP = document.createElement('p');
        if(showPrintPreviewLink)
            closeP.className = 'close';
        else
            closeP.className = 'closeNoPrintPreview';
        
        if(showPrintPreviewLink)
        {
            var printPreviewA = document.createElement('a');
            printPreviewA.id = 'printPreviewLink';
            printPreviewA.href = '#';
            printPreviewA.onclick = function(_name) { return function() { _name.printPreview();return false; }}(this);
            printPreviewA.innerHTML = '<img src="' + globalMediaPath + '/Campaigns/PrintPreview-off.img" width="136" height="22" alt="Print Preview" onmouseover="doHighlight(this);" onmouseout="undoHighlight(this);" id="printPreviewButton"/>';
            closeP.appendChild(printPreviewA);
        }

        var closeA = document.createElement('a');
        closeA.href = '#';
        closeA.id = 'previewClose';
        closeA.onclick = function(_name) { return function() { _name.close();return false; }}(this);
        closeA.innerHTML = '<img src="' + globalMediaPath + '/Campaigns/MMCClosePreview-off.img" width="29" height="26" alt="Close" onmouseover="doHighlight(this);" onmouseout="undoHighlight(this);"/>';
        closeP.appendChild(closeA);
        wrapperDiv.appendChild(closeP);

        var previewImageDiv = document.createElement('div');
        previewImageDiv.id = 'previewImage';
        previewImageDiv.style.width = this.getWidth() - 10 + 'px';
        previewImageDiv.innerHTML = '<img src="' + globalMediaPath + '/Campaigns/MMCLoading.img" width="32" height="32" alt="Loading..."/>';
        wrapperDiv.appendChild(previewImageDiv);

        document.getElementsByTagName('body')[0].appendChild(wrapperDiv);

        this.windowHandle = document.getElementById('piecePreview');
        this.contentHandle = document.getElementById('previewImage');
    },
    /*
     * toggles on or off the print preview button 
     *
     * if you show the preview window then want to show the print preview button afterwards use this function
     */
    togglePrintPreviewButton : function(_show)
    {
        var show = _show;
        
        var pList = document.getElementById('piecePreview').getElementsByTagName('p');
		var width = this.getWidth();
        
        if(show)
        {
            if(!document.getElementById('printPreviewLink'))
            {
                var printPreviewA = document.createElement('a');
                printPreviewA.id = 'printPreviewLink';
                printPreviewA.href = '#';
                printPreviewA.onclick = function(_name) { return function() { _name.printPreview();return false; }}(this);
                printPreviewA.innerHTML = '<img src="' + globalMediaPath + '/Campaigns/PrintPreview-off.img" width="136" height="22" alt="Print Preview" onmouseover="doHighlight(this);" onmouseout="undoHighlight(this);" id="printPreviewButton"/>';
                document.getElementById('previewClose').parentNode.insertBefore(printPreviewA, document.getElementById('previewClose'));
            }
        
            for(var i=0;i<pList.length;i++)
            {
                if(pList[i].className == 'titleNoPrintPreview')
                {
                    pList[i].className = 'title';
                    pList[i].style.width = width - 170 + 'px';
                }
                if(pList[i].className == 'closeNoPrintPreview')
                {
                    pList[i].className = 'close';
                }
            }
            
            this.setShowPrintPreviewLink(true);
        }
        else
        {
            if(document.getElementById('printPreviewLink'))
                document.getElementById('printPreviewLink').parentNode.removeChild(document.getElementById('printPreviewLink'));
        
            for(var i=0;i<pList.length;i++)
            {
                if(pList[i].className == 'title')
                {
                    pList[i].className = 'titleNoPrintPreview';
                    pList[i].style.width = width - 33 + 'px';
                }
                if(pList[i].className == 'close')
                {
                    pList[i].className = 'closeNoPrintPreview';
                }
            }
            
            this.setShowPrintPreviewLink(false);
        }
    },
    /*
     * Checks for existence of preview window
     * returns boolean
     */
    exists : function()
    {
        if(document.getElementById('piecePreview'))
            return true;
        else
            return false;
    },
    /*
     * Removes the preview window from the dom
     */
    remove : function()
    {
        if(document.getElementById('piecePreview'))
            document.getElementById('piecePreview').parentNode.removeChild(document.getElementById('piecePreview'));
    },
    /*
     * Shows the preview window.  If it doesn't exist, draws it, otherwise just shows it
     *
     * call this first, it shows the preview window at a smaller height with the loading screen
     */
    show : function()
    {
        this.windowHandle.style.top = this.getTop() + 'px';
        this.windowHandle.style.left = this.getLeft() + 'px';
        this.contentHandle.style.textAlign = 'center';
        this.contentHandle.innerHTML = '<img src="' + globalMediaPath + '/Campaigns/MMCLoading.img" width="32" height="32" alt="Loading..."/>';
        this.windowHandle.style.display = 'block';
    },
    /*
     * Loads html into the preview Window
     * Input
     * _html - html to load in preview window
     * _height - height to make preview window, 0 - scale to fit browser window [int]
     * _closeElementsArray - elements in the HTML to load that should close the preview window (optional)
     */
    load : function(_html, _height, _closeElementsArray)
    {
        var html = _html;
        var height = _height;
        var closeElementsArray = _closeElementsArray;

        if(height != 0)
            this.contentHandle.style.height = height + 'px';
        else
        {
			var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
			this.contentHandle.style.height = (parseFloat(windowHeight) - 200) + 'px';
        }
        this.contentHandle.style.textAlign = 'left';
        this.contentHandle.innerHTML = html;
        
        //set up all the onclicks for the close elements
        for(var i=0;i<closeElementsArray.length;i++)
        {
            document.getElementById(closeElementsArray[i]).onclick = function(_name) { return function() { _name.close();return false; }}(this);
        }
    },
    /*
     * Print Preview link, must have previously set href
     */
    printPreview : function()
    {
		var url = '../MarketingMaterials/RedirectFile.flx?selectionHref=' + this.getHref() + '&amp;type=personal';
		window.open(url,'showPdf','alwaysRaised=yes,resizable=yes,scrollbars=yes,location=no,menubar=yes,width=750,height=800');
    },
    /*
     * Close preview window and remove from DOM
     */
    close : function()
    {
        this.windowHandle.parentNode.removeChild(this.windowHandle);
        this.windowHandle = null;
        this.contentHandle = null;
    }
}
