﻿var Toto = {
    _initiator: null,
    _elToto: null,
    _totoWidth: null,
    _totoHeight: null,
    _totoLeft: null,
    _totoTop: null,
    _initiatorTop: null,
    _initiatorLeft: null,
    _initiatorHeight: null,
    _docHeight: null,
    _docWidth: null,
    _scrollTop: null,
    _scrollLeft: null,
    _totoPointer: null,
    _offsetTop: null,
    _offsetLeft: null,
    _offsetPointer: null,

    bindToto: function() {
        jQuery("a[JQ=8], img[JQ=8]").hoverIntent({
            sensitivity: 3,
            interval: 500,
            over: function(e) {
                Toto.getTotoPop(e, jQuery(this).attr('JQD'), this)
            },
            timeout: 50,
            out: function() {
                $('toto').setStyle({ display: 'none' });
            }
        });
    },
    unBindToto: function() {
        jQuery("a[JQ=8], img[JQ=8]").unbind('mouseenter mouseleave');

    },
    getTotoPop: function(e, mediaId, initiatorId) {
        _elToto = $('toto');
        _offsetTop = -62;
        _offsetLeft = 20;
        _offsetPointer = 0;


        var dimensions = _elToto.getDimensions();
        _totoWidth = dimensions.width;
        _totoHeight = dimensions.height;
        _totoLeft = -9500;
        _totoTop = -9500;
        jQuery(_elToto).css({ left: _totoLeft + 'px', top: _totoTop + 'px' });
        _initiator = $(initiatorId);
        Toto.setTotoPosition();
        Toto.getTotoXML(mediaId, e, (_initiator.tagName == 'A'));
    },

    setTotoPosition: function() {
        var dimensions = _initiator.getDimensions();
        var initiatorWidth = dimensions.width;
        _initiatorHeight = dimensions.height;


        var docDimensions = document.viewport.getDimensions();
        _docWidth = docDimensions.width;
        _docHeight = docDimensions.height;

        var initiatorPos = _initiator.viewportOffset();
        _initiatorTop = initiatorPos.top;
        _initiatorLeft = initiatorPos.left;

        //  var docScroll = document.viewport.getScrollOffsets();
        _scrollTop = jQuery(window).scrollTop(); // docScroll.top
        _scrollLeft = jQuery(window).scrollLeft();

        _totoTop = (_initiatorTop + _scrollTop) + _offsetTop
        _totoLeft = (_initiatorLeft + _scrollLeft) + _offsetLeft

        if (_initiatorLeft + initiatorWidth + _totoWidth > _docWidth) {
            _totoLeft = _totoLeft - _totoWidth;
            _totoPointer = 'aTRight';
        } else {
            _totoLeft = _totoLeft + initiatorWidth - 30;
            _totoPointer = 'aTLeft';
        }

        return true;
    },
    getTotoXML: function(Id, e, bimg) {
        FilmAjax.GetToTo(Id, bimg, function(r) {

            if (r.error != null) {
                alert(response.error);
                window.status = ''
                return false;
            }
            if (r.value == "") {
                alert("Action Failed");
                window.status = ''
                return false;
            }
            var pointerSuffix = '';
            var oTop = $('toTopBody');
            var oBody = $('toBody');
            var oFoot = $('toBodyFoot');

            var oToto = JSON.parse(r.value)
            oTop.innerHTML = oToto.Head
            oBody.innerHTML = oToto.Body
            oFoot.innerHTML = oToto.Footer

            var dimensions = _elToto.getDimensions();
            _totoWidth = dimensions.width;
            _totoHeight = dimensions.height;

            if ((_totoTop - _scrollTop) < -10) {

                pointerSuffix = 'Flip';
                _offsetPointer = 0;
                if (_initiatorTop > 0) {
                    _offsetPointer = (_totoTop - _scrollTop - 10) * -1;
                }
                _totoTop = _scrollTop - 10;
            }

            // alert('_scrollTop: ' + _scrollTop)
            if ((_totoTop + _offsetPointer + 10) > (_scrollTop + _initiatorTop + _initiatorHeight)) {
                _offsetPointer = 15;
            }
            if (((_totoTop - _scrollTop) + _totoHeight) > _docHeight) {
                _offsetPointer = ((_totoTop + _totoHeight) - _docHeight) - _scrollTop;
                _totoTop = _totoTop - ((_totoTop + _totoHeight) - _docHeight) + _scrollTop + 10;
            }

            jQuery('#arrow').removeClass().addClass(_totoPointer + pointerSuffix);
            jQuery(_elToto).css({ left: _totoLeft + 'px', top: _totoTop + 'px' });
            jQuery('#arrow').css({ top: _offsetPointer + 'px' });
            jQuery(_elToto).show();

        });

    }
}


function checkTotoPopup(chk) {
    if (CPUtility.readCookie('TotoDesc')) {
        $(chk).checked = false;
    }
    else {
        $(chk).checked = true;
    }
}

function ShowHideToto(chk, lab) {

    if (CPUtility.readCookie('TotoDesc')) {
        CPUtility.eraseCookie('TotoDesc');
    }
    else CPUtility.createCookie('TotoDesc', 1, 1000);
    checkTotoPopup(chk, lab);
}
jQuery(document).ready(function() {
    Toto.bindToto();
});
//jQuery('toto').hide()