/**
 * jmpopups
 * Copyright (c) 2009 Otavio Avila (http://otavioavila.com)
 * Licensed under GNU Lesser General Public License
 * 
 * @docs http://jmpopups.googlecode.com/
 * @version 0.5.1
 * 
 */

(function($) {
    var openedPopups = [];
    var popupLayerScreenLocker = false;
    var focusableElement = [];
    var setupJqueryMPopups = {
        screenLockerBackground: "#000",
        screenLockerOpacity: "0.5"
    };

    $.setupJMPopups = function(settings) {
        setupJqueryMPopups = jQuery.extend(setupJqueryMPopups, settings);
        return this;
    }

    $.openPopupLayer = function(settings) {
        if (typeof(settings.name) != "undefined" && !checkIfItExists(settings.name)) {
            settings = jQuery.extend({
                width: "auto",
                height: "auto",
                parameters: {},
                target: "",
                success: function() {},
                error: function() {},
                beforeClose: function() {},
                afterClose: function() {},
                reloadSuccess: null,
                cache: false
            }, settings);
            loadPopupLayerContent(settings, true);
            return this;
        }
    }
    
    $.closePopupLayer = function(name) {
        if (name) {
            for (var i = 0; i < openedPopups.length; i++) {
                if (openedPopups[i].name == name) {
                    var thisPopup = openedPopups[i];
                    
                    openedPopups.splice(i,1)
                    
                    thisPopup.beforeClose();
                    
                    $("#popupLayer_" + name).fadeOut(function(){
                        $("#popupLayer_" + name).remove();
                    
                        focusableElement.pop();
    
                        if (focusableElement.length > 0) {
                            $(focusableElement[focusableElement.length-1]).focus();
                        }
    
                        thisPopup.afterClose();
                        hideScreenLocker(name);
                    });
                    
                    
   
                    break;
                }
            }
        } else {
            if (openedPopups.length > 0) {
                $.closePopupLayer(openedPopups[openedPopups.length-1].name);
            }
        }
        
        return this;
    }
    
    $.reloadPopupLayer = function(name, callback) {
        if (name) {
            for (var i = 0; i < openedPopups.length; i++) {
                if (openedPopups[i].name == name) {
                    if (callback) {
                        openedPopups[i].reloadSuccess = callback;
                    }
                    
                    loadPopupLayerContent(openedPopups[i], false);
                    break;
                }
            }
        } else {
            if (openedPopups.length > 0) {
                $.reloadPopupLayer(openedPopups[openedPopups.length-1].name);
            }
        }
        
        return this;
    }

    function setScreenLockerSize() {
        if (popupLayerScreenLocker) {
            $('#popupLayerScreenLocker').height($(document).height() + "px");
            $('#popupLayerScreenLocker').width($(document.body).outerWidth(true) + "px");
        }
    }
    
    function checkIfItExists(name) {
        if (name) {
            for (var i = 0; i < openedPopups.length; i++) {
                if (openedPopups[i].name == name) {
                    return true;
                }
            }
        }
        return false;
    }
    
    function showScreenLocker() {
        /*
        if ($("#popupLayerScreenLocker").length) {
            if (openedPopups.length == 1) {
                popupLayerScreenLocker = true;
                setScreenLockerSize();
                $('#popupLayerScreenLocker').fadeIn();
            }
   
            if ($.browser.msie && $.browser.version < 7) {
                $("select:not(.hidden-by-jmp)").addClass("hidden-by-jmp hidden-by-" + openedPopups[openedPopups.length-1].name).css("visibility","hidden");
            }
            
            $('#popupLayerScreenLocker').css("z-index",parseInt(openedPopups.length == 1 ? 999 : $("#popupLayer_" + openedPopups[openedPopups.length - 2].name).css("z-index")) + 1);
        } else {
            $("body").append("<div id='popupLayerScreenLocker'><!-- --></div>");
            $("#popupLayerScreenLocker").css({
                position: "absolute",
                background: setupJqueryMPopups.screenLockerBackground,
                left: "0",
                top: "0",
                opacity: setupJqueryMPopups.screenLockerOpacity,
                display: "none"
            });
            
            
            
            $("#popupLayerScreenLocker").click(function() {
                // einkommentieren, um das popup bei einem click au�erhalb der popup-fl�che schlie�en zu lassen
                //$.closePopupLayer();
            });
        }*/
    }
    
    function hideScreenLocker(popupName) {
        if (openedPopups.length == 0) {
            screenlocker = false;
            $('#popupLayerScreenLocker').fadeOut();
        } else {
            $('#popupLayerScreenLocker').css("z-index",parseInt($("#popupLayer_" + openedPopups[openedPopups.length - 1].name).css("z-index")) - 1);
        }
   
        if ($.browser.msie && $.browser.version < 7) {
            $("select.hidden-by-" + popupName).removeClass("hidden-by-jmp hidden-by-" + popupName).css("visibility","visible");
        }
    }
    
    function setPopupLayersPosition(popupElement, animate) {
        if (popupElement) {
            if (popupElement.width() < $(window).width()) {
                var leftPosition = (document.documentElement.offsetWidth - popupElement.width()) / 2;
            } else {
                var leftPosition = document.documentElement.scrollLeft + 5;
            }

            if (popupElement.height() < $(window).height()) {
                var topPosition = document.documentElement.scrollTop + ($(window).height() - popupElement.height()) / 2;
            } else {
                var topPosition = document.documentElement.scrollTop + 5;
            }
            
            var positions = {
                left: 186 + "px",
                top: 153 + "px"
            };
            
            if (!animate) {
                popupElement.css(positions);
            } else {
                popupElement.animate(positions, "slow");
            }
                        
            setScreenLockerSize();
        } else {
            for (var i = 0; i < openedPopups.length; i++) {
                setPopupLayersPosition($("#popupLayer_" + openedPopups[i].name), true);
            }
        }
    }

    function showPopupLayerContent(popupObject, newElement, data) {
        var idElement = "popupLayer_" + popupObject.name;

        if (newElement) {
            showScreenLocker();
            
            $("body").append("<div id='" + idElement + "'><!-- --></div>");
            
            var zIndex = parseInt(openedPopups.length == 1 ? 1000 : $("#popupLayer_" + openedPopups[openedPopups.length - 2].name).css("z-index")) + 2;
        }  else {
            var zIndex = $("#" + idElement).css("z-index");
        }

        var popupElement = $("#" + idElement);
        
        popupElement.css({
            visibility: "hidden",
            width: popupObject.width == "auto" ? "" : popupObject.width + "px",
            height: popupObject.height == "auto" ? "" : popupObject.height + "px",
            position: "absolute",
            "z-index": zIndex
        });
        
        var linkAtTop = "<a href='#' class='jmp-link-at-top' style='position:absolute; left:-9999px; top:-1px;'>&nbsp;</a><input class='jmp-link-at-top' style='position:absolute; left:-9999px; top:-1px;' />";
        var linkAtBottom = "<a href='#' class='jmp-link-at-bottom' style='position:absolute; left:-9999px; bottom:-1px;'>&nbsp;</a><input class='jmp-link-at-bottom' style='position:absolute; left:-9999px; top:-1px;' />";

        popupElement.html(linkAtTop + data + linkAtBottom);
        
        setPopupLayersPosition(popupElement);

        popupElement.css("display","none");
        popupElement.css("visibility","visible");
        
        if (newElement) {
            popupElement.fadeIn();
        } else {
            popupElement.show();
        }

        $("#" + idElement + " .jmp-link-at-top, " +
          "#" + idElement + " .jmp-link-at-bottom").focus(function(){
            $(focusableElement[focusableElement.length-1]).focus();
        });
        
        var jFocusableElements = $("#" + idElement + " a:visible:not(.jmp-link-at-top, .jmp-link-at-bottom), " +
                                   "#" + idElement + " *:input:visible:not(.jmp-link-at-top, .jmp-link-at-bottom)");
                           
        if (jFocusableElements.length == 0) {
            var linkInsidePopup = "<a href='#' class='jmp-link-inside-popup' style='position:absolute; left:-9999px;'>&nbsp;</a>";
            popupElement.find(".jmp-link-at-top").after(linkInsidePopup);
            focusableElement.push($(popupElement).find(".jmp-link-inside-popup")[0]);
        } else {
            jFocusableElements.each(function(){
                if (!$(this).hasClass("jmp-link-at-top") && !$(this).hasClass("jmp-link-at-bottom")) {
                    focusableElement.push(this);
                    return false;
                }
            });
        }
        
        $(focusableElement[focusableElement.length-1]).focus();

        popupObject.success();
        
        if (popupObject.reloadSuccess) {
            popupObject.reloadSuccess();
            popupObject.reloadSuccess = null;
        }
    }
    
    function loadPopupLayerContent(popupObject, newElement) {
        if (newElement) {
            openedPopups.push(popupObject);
        }
        
        if (popupObject.target != "") {
            showPopupLayerContent(popupObject, newElement, $("#" + popupObject.target).html());
        } else {
            $.ajax({
                url: popupObject.url,
                data: popupObject.parameters,
                cache: popupObject.cache,
                dataType: "html",
                method: "GET",
                success: function(data) {
                    showPopupLayerContent(popupObject, newElement, data);
                },
                error: popupObject.error
            });
        }
    }
    
    $(window).resize(function(){
        setScreenLockerSize();
        setPopupLayersPosition();
    });
    
    $(document).keydown(function(e){
        if (e.keyCode == 27) {
            $.closePopupLayer();
        }
    });
})(jQuery);


$.setupJMPopups({
    screenLockerBackground: "#000000",
    screenLockerOpacity: "0.0"
});




function openBigVideoPopupBerlin() {
		$('#teaser_mobiles_internet_innerHtml').html('<table cellpadding=0 cellspacing=0>    <TR> <TD>  <STYLE type=text/css> .magenta {  color: #e20074; }  /* START: STYLES FÃœR DEN POPUP LAYER */ #mipopuplayer {  display: none; } #mipopuplayer-popup {  border: 0px solid #e20074;  position: relative;  background: transparent; } #mipopuplayer-closelink {  text-align: right;  padding-right: 6px;  padding-bottom: 3px; } #mipopuplayer-closelink div {  display: inline;  font-weight: bold;  color: #e20074; }  /* END: STYLES FÃœR DEN POPUP LAYER */  </STYLE> </TD></TR></TABLE><!-- Start: Video-Popup --></TABLE><TR> <TD>  <DIV style="DISPLAY: none" id=mipopuplayervideo> <DIV id=mipopuplayer-closelink>  <A style="FONT-WEIGHT: bold;" class=magenta onclick="$.closePopupLayer(\'MIPopupVideo\');return false;" href="#" >schlie&szlig;en&nbsp;  <DIV>  x </DIV>  </A> </DIV> <DIV id=mipopuplayer-popup>  <OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=774 height=452> <PARAM NAME="_cx" VALUE="20478"><PARAM NAME="_cy" VALUE="11959"><PARAM NAME="FlashVars" VALUE=""><PARAM NAME="Movie" VALUE="http://www.t-mobile.de/T-D1/img/display_image/0,3465,126852,00.swf"><PARAM NAME="Src" VALUE="http://www.t-mobile.de/T-D1/img/display_image/0,3465,126852,00.swf"><PARAM NAME="WMode" VALUE="Transparent"><PARAM NAME="Play" VALUE="0"><PARAM NAME="Loop" VALUE="-1"><PARAM NAME="Quality" VALUE="High"><PARAM NAME="SAlign" VALUE=""><PARAM NAME="Menu" VALUE="-1"><PARAM NAME="Base" VALUE=""><PARAM NAME="AllowScriptAccess" VALUE=""><PARAM NAME="Scale" VALUE="ShowAll"><PARAM NAME="DeviceFont" VALUE="0"><PARAM NAME="EmbedMovie" VALUE="0"><PARAM NAME="BGColor" VALUE=""><PARAM NAME="SWRemote" VALUE=""><PARAM NAME="MovieData" VALUE=""><PARAM NAME="SeamlessTabbing" VALUE="1"><PARAM NAME="Profile" VALUE="0"><PARAM NAME="ProfileAddress" VALUE=""><PARAM NAME="ProfilePort" VALUE="0"><PARAM NAME="AllowNetworking" VALUE="all"><PARAM NAME="AllowFullScreen" VALUE="false"> <embed src="http://www.t-mobile.de/T-D1/img/display_image/0,3465,126852,00.swf" quality="high" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="774" height="452"> </embed>  </OBJECT></DIV>  </DIV> </TD></TR></TABLE>');
    $.openPopupLayer({
        name: "MIPopupVideo",
        width: 780,
        height: 452,		
        topPosition: 20,
        target: "mipopuplayervideo"
    });
}   

function openBigVideoPopupMediapark() {


		$('#teaser_mobiles_internet_innerHtml').html('<table cellpadding=0 cellspacing=0>    <TR> <TD>  <STYLE type=text/css> .magenta {  color: #e20074; }  /* START: STYLES FÃœR DEN POPUP LAYER */ #mipopuplayer {  display: none; } #mipopuplayer-popup {  border: 0px solid #e20074;  position: relative;  background: transparent; } #mipopuplayer-closelink {  text-align: right;  padding-right: 6px;  padding-bottom: 3px; } #mipopuplayer-closelink div {  display: inline;  font-weight: bold;  color: #e20074; }  /* END: STYLES FÃœR DEN POPUP LAYER */  </STYLE> </TD></TR></TABLE><!-- Start: Video-Popup --></TABLE><TR> <TD>  <DIV style="DISPLAY: none" id=mipopuplayervideo> <DIV id=mipopuplayer-closelink>  <A style="FONT-WEIGHT: bold;" class=magenta onclick="$.closePopupLayer(\'MIPopupVideo\');return false;" href="#" >schlie&szlig;en&nbsp;  <DIV>  x </DIV>  </A> </DIV> <DIV id=mipopuplayer-popup>  <OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=774 height=452> <PARAM NAME="_cx" VALUE="20478"><PARAM NAME="_cy" VALUE="11959"><PARAM NAME="FlashVars" VALUE=""><PARAM NAME="Movie" VALUE="http://www.t-mobile.de/T-D1/img/display_image/0,3465,123575,00.swf"><PARAM NAME="Src" VALUE="http://www.t-mobile.de/T-D1/img/display_image/0,3465,123575,00.swf"><PARAM NAME="WMode" VALUE="Transparent"><PARAM NAME="Play" VALUE="0"><PARAM NAME="Loop" VALUE="-1"><PARAM NAME="Quality" VALUE="High"><PARAM NAME="SAlign" VALUE=""><PARAM NAME="Menu" VALUE="-1"><PARAM NAME="Base" VALUE=""><PARAM NAME="AllowScriptAccess" VALUE=""><PARAM NAME="Scale" VALUE="ShowAll"><PARAM NAME="DeviceFont" VALUE="0"><PARAM NAME="EmbedMovie" VALUE="0"><PARAM NAME="BGColor" VALUE=""><PARAM NAME="SWRemote" VALUE=""><PARAM NAME="MovieData" VALUE=""><PARAM NAME="SeamlessTabbing" VALUE="1"><PARAM NAME="Profile" VALUE="0"><PARAM NAME="ProfileAddress" VALUE=""><PARAM NAME="ProfilePort" VALUE="0"><PARAM NAME="AllowNetworking" VALUE="all"><PARAM NAME="AllowFullScreen" VALUE="false"> <embed src="http://www.t-mobile.de/T-D1/img/display_image/0,3465,123575,00.swf" quality="high" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="774" height="452"> </embed>  </OBJECT></DIV>  </DIV> </TD></TR></TABLE>');
    $.openPopupLayer({
        name: "MIPopupVideo",
        width: 780,
        height: 452,		
        topPosition: 20,
        target: "mipopuplayervideo"
    });
}   

function openStaticPopup(flash) {   
   
        switch (flash) {
        	case 'berlin':
					default:
						openBigVideoPopupBerlin();
        	break;

        	case 'mediapark':
						openBigVideoPopupMediapark();
        	break;
        }
    
};








/* Code um Popup-Layer einmal pro Stunde automatisch anzeigen zu lassen.
$( document ).ready( function() {
    
    // zeige den layer einmal pro stunde
    if(document.cookie.indexOf('layershown=true') == -1) {

        var a = new Date();
        a = new Date(a.getTime() +1000*60*60);
        document.cookie = 'layershown=true; expires='+a.toGMTString()+';'+document.cookie;

        openStaticPopup();

    }

});
*/