﻿/* Lightbox */


var lightbox = function(videoUrl, pdf, tracking) {
    var lbObj;
    var lbOyObj
    var lbWrObj;
    var pageObj;
    var noFlashMessage = '<p>Hello, you either have JavaScript turned off or an old version of Adobe\'s Flash Player.<br/> <a href="http://www.adobe.com/go/getflashplayer">Click here to download the latest Flash player.</a></p>';

    init = function() {
        //create lightbox
        lbObj = document.createElement('div');
        lbObj.id = 'lightbox';
        pageObj = document.getElementById('stage');
        pageObj.appendChild(lbObj);
        lbObj = document.getElementById('lightbox');
        lbObj.innerHTML = '<div id="lb-wrapper"><span href="#" id="lb-close">Close window</span><a href="' + pdf + '" id="lb-pdf" target="_blank">Download recipe card</a><div id="videoFrame"><div id="videoContent">' + noFlashMessage + '</div></div></div><div id="lb-overlay"></div>';

        if (pdf === '' || pdf === undefined || window.location.href.indexOf(pdf) != -1) document.getElementById('lb-pdf').style.display = 'none';

        //assign dom objects
        lbWrObj = document.getElementById('lb-wrapper');
        lbOyObj = document.getElementById('lb-overlay');

        //create overlay opacity
        lbOyObj.style.opacity = 6 / 10;
        lbOyObj.style.filter = 'alpha(opacity=' + 6 * 10 + ')';

        //run functions
        setFullScreen();
        loadFlash();
        bindClose();
        addEvent(window, 'resize', setFullScreen);
    }

    // positionWrapper
    function positionWrapper(diAry) {
        var lbWrObj = document.getElementById('lb-wrapper');
        lbWrObj.style.width = 599 + 'px';
        lbWrObj.style.top = getScrolledY() + 100 + 'px';

        lbWrObj.style.marginLeft = lbWrObj.style.marginRight = (diAry[0] - 599) / 2 + 'px';
    }

    // getScrolled
    function getScrolledY() {
        var scrOfY = 0;
        if (typeof (window.pageYOffset) == 'number') {
            //Netscape compliant
            scrOfY = window.pageYOffset;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            scrOfY = document.body.scrollTop;
        } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            //IE6 standards compliant mode
            scrOfY = document.documentElement.scrollTop;
        }
        return scrOfY;
    }

    //getWindowSize
    function getWindowSize() {
        var diAry = new Array();
        if (window.innerHeight && window.scrollMaxY) // Firefox
        {
            //diAry[0] = window.innerWidth + window.scrollMaxX;
            if (window.innerWidth < 955) {
                diAry[0] = 955
            } else if (window.innerWidth < window.innerWidth + window.scrollMaxX) {
                diAry[0] = window.innerWidth - 17; //subtract for scrollbars
            } else {
                diAry[0] = window.innerWidth + window.scrollMaxX - 17; //subtract for scrollbars
            }
            diAry[1] = window.innerHeight + window.scrollMaxY;
        }
        else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
        {
            //diAry[0] = document.body.scrollWidth;
            if (document.body.clientWidth < 955) {
                diAry[0] = 955
            } else if (document.body.clientWidth < document.body.scrollWidth) {
                diAry[0] = document.body.clientWidth
            } else {
                diAry[0] = document.body.scrollWidth
            }
            diAry[1] = document.body.scrollHeight;
        }
        else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        {
            //diAry[0] = document.body.offsetWidth + document.body.offsetLeft;
            if (document.body.offsetWidth < 955) {
                diAry[0] = 955
            } else if (document.body.offsetWidth < document.body.offsetWidth + document.body.offsetLeft) {
                diAry[0] = document.body.offsetWidth;
            } else {
                diAry[0] = document.body.offsetWidth + document.body.offsetLeft;
            }
            diAry[1] = document.body.offsetHeight + document.body.offsetTop;
        }
        return diAry;
    }

    //setFullscreen
    function setFullScreen() {
        var diAry = getWindowSize();

        lbOyObj.style.width = lbObj.style.width = diAry[0] + 'px';
        lbOyObj.style.height = lbObj.style.height = diAry[1] + 'px';

        positionWrapper(diAry);
    }

    //loadFlash
    loadFlash = function() {
        var flashvars = {
            flvPath: videoUrl,
            linkTo: pdf,
            linkToBtn: "/trynation/library/images/common/extraneous/replay.png",
            startTrack: "('DCS.dcsuri','/trynation/recipe_video/" + tracking + "/start.flashPage','WT.ti','Try Nation - Recipe video - " + tracking + " - Start','WT.cg_n','Try Nation','WT.cg_s','Recipe Video','WT.cg_t','Start','WT.cg_u','','DCSext.trynation_recipe','')",
            stopTrack: "('DCS.dcsuri','/trynation/recipe_video/" + tracking + "/end.flashPage','WT.ti','Try Nation - Recipe video - " + tracking + " - End','WT.cg_n','Try Nation','WT.cg_s','Recipe Video','WT.cg_t','End','WT.cg_u','','DCSext.trynation_recipe','')",
            linkToTarget: "_self",
            allowFullScreen: "true"
        };
        var attributes = {};
        var params = {
            menu: "false",
            wmode: "window",
            scale: "noscale",
            allowFullScreen: "true",
            allowScriptAccess: "always"
        };
        swfobject.embedSWF("/trynation/library/flash/player00.swf", "videoContent", "572", "320", "9.0.0", "/trynation/library/js/swfobject2.1/expressInstall.swf", flashvars, params, attributes);
    }

    // bindClose
    function bindClose() {
        document.getElementById('lb-close').onclick = function() {
            deleteWindow();
        }
        lbOyObj.onclick = function() {
            return function(e) {
                deleteWindow();
                return false;
            };
        } (); ;
    }

    // deleteWindow
    function deleteWindow() {
        pageObj.removeChild(lbObj);
        removeEvent(window, 'resize', setFullScreen);
        return false;
    }

    // generic eventbinding
    function addEvent(obj, type, fn) {
        if (obj.attachEvent) {
            obj['e' + type + fn] = fn;
            obj[type + fn] = function() { obj['e' + type + fn](window.event); }
            obj.attachEvent('on' + type, obj[type + fn]);
        } else
            obj.addEventListener(type, fn, false);
    }
    function removeEvent(obj, type, fn) {
        if (obj.detachEvent) {
            obj.detachEvent('on' + type, obj[type + fn]);
            obj[type + fn] = null;
        } else
            obj.removeEventListener(type, fn, false);
    }

    init();

}