// JavaScript Document
var filePath = "http://cloudfront.public.digitaljuice.com/media/";
var image2Layer;
var popup2nd = false;

function closeLightBox() {
    imageLayer.set('tween', { duration: 550 }); imageLayer.tween('opacity', 1, 0);
    closebtn.set('tween', { duration: 550 }); closebtn.tween('opacity', 1, 0);
    bgLayer.set('tween', { duration: 550 }); bgLayer.tween('opacity', 0.8, 0);
    if ($('bgLayer')) {
        $('bgLayer').dispose();
    }
    if ($('imageLayer')) {
        $('imageLayer').innerHTML = "";
        $('imageLayer').dispose();
    }
    if ($('close_btn')) {
        $('close_btn').dispose();
    }
    if (popup2nd == true) {
        $(image2Layer).dispose();
        $(close2btn).dispose();
    }
}

function loadContent(_s, _t, _w, _h, _tle, _L) {

    if (_t == "mp4" || _t == "flv") {
        var popUpFile = new SWFObject("/_players/jwplayer-5.1/player-licensed.swf", "ply", _w, _h, "9");
        popUpFile.addParam("allowfullscreen", "true");
        //popUpFile.addParam("wmode","transparent");
        popUpFile.addVariable("file", _s);
        popUpFile.addVariable("skin", "/_players/jwplayer-skins/stylish.swf");
        //popUpFile.addVariable("icons", "false");
        popUpFile.addVariable("controlbar", "over");
        popUpFile.addVariable("autostart", "true");
        popUpFile.addVariable("stretching", "none");
        //popUpFile.addVariable("repeat", "always");		
        popUpFile.addVariable("backcolor", "111111");
        popUpFile.addVariable("bufferlength", ".10");
        popUpFile.addVariable("frontcolor", "CCCCCC");
        popUpFile.addVariable("lightcolor", "66CC00");
        popUpFile.write(_L);

        if (_tle === null) {
            _tle = "";
        } else {
            $(_L).innerHTML = $(_L).innerHTML + "<p style='width:" + (_w - 20) + "px; padding-top:5px;'>" + _tle + "</p>";
        }
    }
    if (_t == "swf") {
        var popUpFile = new SWFObject(_s, "ply", _w, _h, "9");
        popUpFile.write(_L);
    }
    if (_t == "jpg" || _t == "gif" || _t == "png" || _t == "JPG") {
        if (_tle === null) { _tle = ""; }
        $(_L).innerHTML = "<img src='" + _s + "' width='" + _w + "' height='" + _h + "'><p style='width:" + (_w - 20) + "px; padding-top:3px; margin-bottom:3px;'>" + _tle + "</p>";
    }
    if (_t == "div") {
        conId = _s.substring(0, _s.length - 4);
        $(_L).innerHTML = $(conId).innerHTML;
    }
    if (_t != "div" && _t != "jpg" && _t != "gif" && _t != "png" && _t != "JPG" && _t != "mp4" && _t != "flv") {

        $(_L).load(_s);
    }
}


function generateLightBox(_s, _t, _w, _h, _tle) {
	
	if ('innerWidth' in window) { 
               innerWidth = window.innerWidth;
            }
            else { 
                innerWidth =  document.documentElement.clientWidth;
            }

    bgLayer = new Element('div').setProperty('id', 'bgLayer');
    bgLayer.setStyles({ 'opacity': '0', 'visibility': 'visible', 'background-color': '#000000', padding: 0, margin: 0, 'position': 'absolute', 'z-index': '99999', 'cursor': 'pointer'}).inject(document.body).addEvent('click', closeLightBox.bind(this));
    bgLayer.setStyles({ 'left': $(document.body).getCoordinates().left, 'top': 0, 'height': Window.getScrollHeight(), 'width': innerWidth });
    
    imageLayer = new Element('div').setProperty('id', 'imageLayer');
    imageLayer.setStyles({ 'opacity': '0', 'visibility': 'visible', 'background-color': '#000000', 'position': 'absolute', 'border': '1px solid #555555', 'padding': '15px', 'z-index': '999999'}).injectInside(document.body);
    imageLayer.setStyles({ 'top': $(document.body).getScrollTop() + 50, height: 'auto', width: _w, 'left': $(document.body).getCoordinates().width / 2 - (_w / 2) - 20 });
    
    closebtn = new Element('div').setProperty('id', 'close_btn');
    closebtn.setStyles({ 'opacity': '0', 'visibility': 'visible', 'position': 'absolute', 'cursor': 'pointer', 'z-index': '999999'}).injectInside(document.body).addEvent('click', closeLightBox.bind(this));
    closebtn.setStyles({ 'top': $(imageLayer).getCoordinates().top - 12, height: 24, width: 24, 'left': ($(imageLayer).getCoordinates().left + parseInt(_w)) + 20 });
    closebtn.innerHTML = '<img src="/images/close.png" width="24" height="24" class="iepngfix">';
    
    bgLayer.set('tween', { duration: 650 }); bgLayer.tween('opacity', 0, 0.8);
    imageLayer.set('tween', { duration: 650 }); imageLayer.tween('opacity', 0, 1);
    closebtn.set('tween', { duration: 650 }); closebtn.tween('opacity', 0, 1);
    $(imageLayer).innerHTML = "<a href='http://www.macromedia.com/go/getflashplayer'>Get the Flash Player</a> to see this video.";

    loadContent(_s, _t, _w, _h, _tle, "imageLayer");
}

function pop2ndWindow(pop2nd) {
    var size = $(pop2nd).getProperty("size");
    size = size.split(" ");
    image2Layer = $('imageLayer').clone().inject('bgLayer', 'before').addEvent('click', function () { $(this).dispose(); $(close2btn).dispose(); });
    image2Layer.setStyles({ 'position': 'absolute', 'opacity': 1, 'z-index': '9999999999', 'top': $(document.body).getScrollTop() + 150, height: parseInt(size[1]), width: parseInt(size[0]), 'left': $(document.body).getCoordinates().width / 2 - (parseInt(size[0]) / 2) - 20 });

    close2btn = $('close_btn').clone().inject(image2Layer, 'after').addEvent('click', function () { $(this).dispose(); $(image2Layer).dispose(); });
    close2btn.setStyles({ 'position': 'absolute', 'opacity': 1, 'z-index': '999999999999', 'top': $(image2Layer).getCoordinates().top - 12, height: 24, width: 24, 'left': ($(image2Layer).getCoordinates().left + parseInt(size[0])) + 20 });
    popup2nd = true;
    $(image2Layer).innerHTML = "";
    var content2nd = $(pop2nd).getProperty('name');
    loadContent(content2nd, content2nd.substring(content2nd.length - 3, content2nd.length), size[0], size[1], $(pop2nd).getProperty('title'), image2Layer);

}




window.addEvent('domready', function () {
    var popUpContent = $$('[rel="popup-content"]');
    popUpContent.each(function (element) {
        element.addEvent('click', function () {
            var size = element.getProperty('size').split(" ");
            var content = element.getProperty('name');
            generateLightBox(content, content.substring(content.length - 3, content.length), size[0], size[1], element.getProperty('title'));
        });
    });

    var popUpContent = $$('[rel="play-in-place"]');
    popUpContent.each(function (element) {
        element.addEvent('click', function () {
            var size = element.getProperty('size').split(" ");
            var content = element.getProperty('name');
            element.getParent('div').setStyles({ width: size[0], height: size[1], 'border': '1px solid #3399ff' });
            loadContent(content, content.substring(content.length - 3, content.length), size[0], size[1], element.getProperty('title'), element.getParent('div'))
        });
    });

});

