var jQ = jQuery.noConflict();
jQ(document).ready(function(){    
    jQ("body").append("<div id=\"modalWindow\" class=\"jqmWindow\"><div id=\"jqmTitle\"><button class=\"jqmClose\">close</button><span id=\"jqmTitleText\">Title of modal window</span></div><iframe id=\"jqmContent\" src=\"\"></iframe></div>");
    
    //thickbox replacement
    var closeModal = function(hash)
    {
        var _modalWindow = jQ(hash.w);

        //jQ('#jqmContent').attr('src', 'blank.html');
        _modalWindow.fadeOut('2000', function()
        {
            hash.o.remove();
            //refresh parent

            //if (hash.refreshAfterClose === 'true')
            //{
                window.location.href = document.location.href;
            //}
        });
     };
     
     var openInFrame = function(hash)
     {
        var _trigger = jQ(hash.t);
        var _modalWindow = jQ(hash.w);
        var _modalContainer = jQ('iframe', _modalWindow);
        var _height = jQ(window).height() - 150;

        var myUrl = _trigger.attr('href') + "?modal=true&height=" + _height;
        var myTitle = _trigger.attr('title');
        var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
        _modalContainer.html('').attr('src', myUrl);
        
        jQ('#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')
//                {
//                    var newWidth = queryVarsArray[i].split("=")[1];
//                }
//                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
//                {
//                    var newHeight = queryVarsArray[i].split("=")[1];
//                }
//                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
//                {
//                    // if true, launches a "refresh parent window" order after the modal is closed.
//                    hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
//                } else
//                {
//                    hash.refreshAfterClose = false;
//                }
//            }
//            // 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(jQ(window).height()) * (parseInt(newHeight) / 100));
//                }
//                var newTop = Math.floor(parseInt(jQ(window).height() - newHeight) / 2);
//            }
//            else
//            {
//                newHeight = _modalWindow.height();
//            }
//            
//            if (newWidth != 0)
//            {
//                if (newWidth.indexOf('%') > -1)
//                {
//                    newWidth = Math.floor(parseInt(jQ(window).width() / 100) * parseInt(newWidth));
//                }
//                var newLeft = Math.floor(parseInt(jQ(window).width() / 2) - parseInt(newWidth) / 2);
//            }
//            else
//            {
//                newWidth = _modalWindow.width();
//            }

//     // do the animation so that the windows stays on center of screen despite resizing
//     
//        newWidth = 20;
//            _modalWindow.css({width:0, height:0}).jqmShow().animate({
//                width: newWidth,
//                height: newHeight,
//                top: newTop,
//                left: newLeft,
//                marginLeft: 0,
//                opacity: 1
//            }, 'slow').show();
//        }
//        else
//        {
            // don't do animations
            
            
            _modalWindow.jqmShow().css({top: 25});
            
            jQ("iframe", _modalWindow).css({width: 755, height: _height});
            
            RF_initScrollbars("iframe#jqmContent", ".jqmWindow");
            _modalWindow.show();
//        }
    }

//_modalWindow.jqmShow();


     jQ('#modalWindow').jqm({
        overlay: 70,
        modal: true,
        trigger: '.modal-link',
        target: '#jqmContent',
        onHide: closeModal,
        onShow: openInFrame
    });
}); 



