/**
 * @author          ATAFOTO.studio (Jacques BODIN-HULLIN) <http://www.atafotostudio.com>
 * @copyright       (C) ATAFOTO.studio (Jacques BODIN-HULLIN)
 * @license         Tous droits réservés
 * @since           27/04/2010
 *
 * @see             http://dev.iceburg.net/jquery/jqModal/
 * @see             http://pixeline.be/blog/2008/javascript-loading-external-urls-in-jqmodal-jquery-plugin/
 * @using           JQuery
 */

var ataModal = {
    closeModal: function (hash) {
        $(hash.w).fadeOut('fast', function () {
            $('iframe', $(this)).html('').attr('src', '');
            $(this).removeClass('quoi_de_neuf');
        });
        hash.o.fadeOut('slow', function () {
            $(this).remove();
            // On retire le wmode transparent sur les flashs
            /*if (jQuery.browser.msie) {*/
                $("#KINDERCHOCOLAT").css("visibility", "visible");
            /*}*/
        });
        if (hash.refreshAfterClose === 'true') {
            /*window.location.href = document.location.href;*/
        }
    },

    openInFrame: function (hash) {
        var $trigger = $(hash.t);
        var $modalWindow = $(hash.w);
        var $overlay = $(hash.o);
        var $modalContainer = $('iframe', $modalWindow);
        var myUrl = $trigger.attr('href');
        var myTitle = $trigger.attr('title');
        var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;

        // Dimensions de la fenêtre d'affichage
        var $window = $(window);
        var maxWidth = $window.width() - 30;
        var maxHeight = $window.height() - 30;

        var isRedim = false;

        $modalContainer.html('').attr('src', myUrl);
        /*$('#jqmTitleText').text(myTitle);*/
        myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
        var queryString = (myUrl.indexOf("?")> -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;
        if (queryString != null && typeof queryString != 'undefined')
        {
            var queryVarsArray = queryString.split("&");
            for (var i = 0; i <queryVarsArray.length; i++)
            {
                if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
                {
                    newWidth = queryVarsArray[i].split("=")[1];
                    if (newWidth > maxWidth) {
                        newWidth = maxWidth;
                    }
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
                {
                    var newHeight = queryVarsArray[i].split("=")[1];
                    if (newHeight > maxHeight) {
                        newHeight = maxHeight;
                        isRedim = true;
                    }
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
                {
                    hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
                } else
                {
                    hash.refreshAfterClose = true;
                }
            }
            // let's run through all possible values: 90%, nothing or a value in pixel
            if (newHeight != 0)
            {
                /*if (newHeight.indexOf('%')> -1)*/
                /*{*/
                /*newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));*/
                /*}*/
                /*var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);*/
                var newTop = Math.floor(parseInt($(window).height() - newHeight) / 3);
            }
            else
            {
                newHeight = $modalWindow.height();
            }
            if (newWidth != 0)
            {
                /*if (newWidth.indexOf('%')> -1)*/
                /*{*/
                /*newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));*/
                /*}*/
                var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);
            }
            else
            {
                newWidth = $modalWindow.width();
            }
            // do the animation so that the windows stays on center of screen despite resizing

            if ($trigger.hasClass('thickbox-billet')) {
                $modalWindow.addClass("quoi_de_neuf");
            }

            // On récupère tous les flash et on y ajoute le mode transparent
            /*if (jQuery.browser.msie) {*/
                $("#KINDERCHOCOLAT").css("visibility", "hidden");
            /*}*/

                /*alert('largeur : ' + newWidth + ', hauteur : ' + newHeight);*/

                if (isRedim) {
                    newWidth = parseInt(newWidth) + 30;
                }

            $modalWindow.jqmShow().css({
                width: newWidth,
                height: newHeight,
                //opacity: 1,
                top: newTop,
                left: newLeft,
                marginLeft: 0
            }).fadeIn('slow');

            $modalContainer.css({
                width: newWidth + "px",
                height: parseInt(newHeight - 27) + "px"
            });
        }
    }
};
