/*
 * jqModalVideo - Customized Video Popup
 * Version: 1, 20091002
 * Copyright (c) Marine Benjamin, 
 * http://www.marineb.net 
 */

 
var jqmCenterModal=function(e){

    var modalWindow = e.data;

    var modalHeight=modalWindow.outerHeight();
    var iframeWidth=parseInt($('iframe',modalWindow).css('width'));
    if($.browser.msie && parseInt($.browser.version.substr(0,1))<8){
        var modalWidth=iframeWidth+34;
    }
    else{
        var modalWidth=modalWindow.outerWidth();
    }
    
    var windowHeight=$(window).height();
    var windowWidth=$(window).width();

    modalWindow.css(
        {
            'top':modalHeight>=windowHeight?0:Math.round((windowHeight-modalHeight)/2),
            'left':modalWidth>=windowWidth?0:Math.round((windowWidth-modalWidth)/2)
        }
    );

};

var jqmOnShowModal=function(hash){

    // w: (jQuery object) The window element.
    var modalWindow = hash.w;
    // t: (DOM object) The triggering element
    var modalTrigger = $(hash.t);

    var targetUrl = modalTrigger.attr('href');
    var modalTitle = modalTrigger.attr('title');
    var iframeWidth=parseInt($('iframe',modalWindow).css('width'));
    
    $('h4',modalWindow).html(modalTitle); // $(selector, context)
    
    var modalHeight=modalWindow.outerHeight();
    if($.browser.msie && parseInt($.browser.version.substr(0,1))<8){
        var modalWidth=iframeWidth+34;
    }
    else{
        var modalWidth=modalWindow.outerWidth();
    }

    var modalStartHeight=Math.round(modalHeight/4);
    var modalStartWidth=Math.round((2*modalWidth)/3);

    var modalEndHeight=modalWindow.height();
    if($.browser.msie && parseInt($.browser.version.substr(0,1))<8){
        var modalEndWidth=iframeWidth+20; // Borders and paddings
    }
    else{
        var modalEndWidth=modalWindow.width();
    }
    
    var windowHeight=$(window).height();
    var windowWidth=$(window).width();

    if($.browser.msie && parseInt($.browser.version.substr(0,1))==6){
        modalWindow.css('position','absolute');
        $(window).scrollTop(0).scrollLeft(0);
    }
    
    modalWindow.css(
        {
            'height':modalStartHeight,
            'width':modalStartWidth,
            'top':modalStartHeight>=windowHeight?0:Math.round((windowHeight-modalStartHeight)/2),
            'left':modalStartWidth>=windowWidth?0:Math.round((windowWidth-modalStartWidth)/2),
            'opacity':0.5,
            'display':'block'
        }
    ).jqmShow().animate(
        {
            'height':modalEndHeight,
            'width':modalEndWidth,
            'top':modalHeight>=windowHeight?0:Math.round((windowHeight-modalHeight)/2),
            'left':modalWidth>=windowWidth?0:Math.round((windowWidth-modalWidth)/2),
            'opacity':1
        }, 
        300,
        'swing',
        function()
        {
            $('iframe',modalWindow).attr('src',targetUrl);
            $('iframe a',modalWindow).attr('href',targetUrl);
            $(document).keydown(function(c) {
				if (c.keyCode == 27) {
                    modalWindow.jqmHide();
					$(document).unbind("keydown");
				}
			});
            $(window).bind("resize scroll",modalWindow,jqmCenterModal);
        }
    );

};

var jqmOnHideModal=function(hash){

    // w: (jQuery object) The window element.
    var modalWindow = hash.w;

    $(window).unbind("resize scroll");
    $('iframe a',modalWindow).attr('href','');
    $('iframe',modalWindow).attr('src','');
    $('h4',modalWindow).html('');
    modalWindow.css({'display':'none','opacity':0})
    hash.o.remove();

};

$(document).ready(function() {
    $('#videoPopup').jqm({'trigger':'#videoTrigger','overlay':30,'toTop':true,'onShow':jqmOnShowModal,'onHide':jqmOnHideModal});
});
