﻿
$(document).ready(function(){  
    $(".teaserpopup").JabaTeaserPopup();
});
function tp_showIframe(){
	$("#tp_window").css({display:"block"});
}


(function($){
    var m_isOnPopup = false;
    var m_current = null;
    var m_contectW = 750;
    var m_contectH = 400;

    var m_config = {
        type : 'popup'
//            small_textheight : "2.5em",     // text area height when it is the smallest
//            large_textheight : "5.0em",     // text area height when it is the largest
//            css : {
//                target_image : "en_img",    // the target image class to enlarge. (usally 'img' tag)
//                target_text : "en_text"     // the target text area class to enlarge. (usally 'p'/'div' tag)
//            },
        };
    
    // main initializer
    $.fn.JabaTeaserPopup = function(settings) {  
        m_config = $.extend(m_config, settings); 


		function mouseoutHandler(obj)
		{
		    $(obj).attr("teaseron", "false");
		    m_current = null;
		    window.setTimeout(function() {tp_remove(obj);}, 100);
        }

		function mouseoverHandler(obj)
		{
		    $(obj).attr("teaseron", "true");
		    m_current = obj;
		    window.setTimeout(function() {showTeaser(obj);}, 0);
		}
		
        function showTeaser(obj)
        {
            m_isOnPopup = false;
            
            var offset = $(obj).offset();
            var top = (offset.top - m_contectH/2 > 0)? offset.top - m_contectH/2 : 0;
            var left = offset.left + $(obj).width();
            
            // check the height
            try{
                var scrollTop = $("body").get(0).scrollTop;
                var clientHeight = $("body").get(0).clientHeight;
                
                if ($("html").get(0).scrollTop > scrollTop)
                    scrollTop = $("html").get(0).scrollTop
                
                if ($("html").get(0).clientHeight < clientHeight)
                    clientHeight = $("html").get(0).clientHeight

                if (top + m_contectH + 20> scrollTop + clientHeight)
                    top = scrollTop + clientHeight - m_contectH-20;
                
                if (top < scrollTop)
                    top = scrollTop;
                    
            }catch(e){}
            
            
            if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
                $("body","html").css({height: "100%", width: "100%"});
                //$("html").css("overflow","hidden");
                if (document.getElementById("tp_HideSelect") === null) {//iframe to hide select elements in ie6
                    $("body").append("<iframe id='tp_HideSelect' scrolling='no'></iframe><div id='tp_overlay'></div><div id='tp_window'></div>");
                    $("#tp_overlay").click(tp_remove);
                }
                }else{//all others
                if(document.getElementById("tp_overlay") === null){
                    $("body").append("<div id='tp_overlay'></div><div id='tp_window'></div>");
                    $("#tp_overlay").click(tp_remove);
                }
            }

		    if(tp_detectMacXFF()){
			    $("#tp_overlay").addClass("tp_overlayMacFFBGHack");//use png overlay so hide flash
		    }else{
			    $("#tp_overlay").addClass("tp_overlayBG");//use background and opacity
		    }
            $("#tp_overlay").addClass("tp_overlayBG");//use background and opacity
            
            caption = "test";

            $("#tp_iframeContent").remove();
            $("#tp_window").append("<iframe frameborder='0' hspace='0' src='"+
                $(obj).attr("teaserurl") + "?startingpath=" + encodeURI($(obj).attr("href")) +
                //$(obj).attr("href") +
                //"/samplecontent1.html?test=1" + 
                "' id='tp_iframeContent' name='tp_iframeContent" +
                Math.round(Math.random()*1000)+"' onload='tp_showIframe()' style='width:" +
                (m_contectW + 5)+"px;height:" +
                (m_contectH + 5)+"px;' > </iframe>");

            $("#tp_window").css("left", left + "px");
            $("#tp_window").css("top", top + "px");
           

            $("#tp_window").mouseover(function () {
		        m_isOnPopup = true;
            });
            $("#tp_window").mouseout(function () {
                m_isOnPopup = false;
		        window.setTimeout(function() {tp_remove("popupmouseout");}, 100);
		        return false;
            });

            $("#tp_closeWindowButton").click(tp_remove);
            
		    tp_position();
		    if($.browser.safari){//safari needs help because it will not fire iframe onload
			    $("#tp_window").css({display:"block"});
		    }
           	
            document.onkeyup = function(e){ 	
                if (e == null) { // ie
	                keycode = event.keyCode;
                } else { // mozilla
	                keycode = e.which;
                }
                if(keycode == 27){ // close
	                tp_remove();
                }	
            };
            	
        }
    
        function tp_remove(obj) {
            if (obj == "popupmouseout") {
                if (m_isOnPopup == true) return;
                if (m_current != null) return;
            } else {
		        // check whether the current popup is for the mouseout element.
                if (typeof($(obj).attr("teaserurl")) == "undefined" ||
                    $("#tp_iframeContent").length == 0 ||
                    $("#tp_iframeContent").attr("src").indexOf($(obj).attr("teaserurl")) < 0)
                    return;

                if ($(obj).attr("teaseron") == "true") return;
                if (m_isOnPopup == true) return;
            }            
 	        $("#tp_imageOff").unbind("click");
	        $("#tp_closeWindowButton").unbind("click");
	        $("#tp_window").hide();
	        window.setTimeout(function () {
	            $('#tp_window,#tp_overlay,#tp_HideSelect').trigger("unload").unbind().remove();
	            }, 100);
	        //$("#tp_window").fadeOut("fast",function(){$('#tp_window,#tp_overlay,#tp_HideSelect').trigger("unload").unbind().remove();});
	        if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		        $("body","html").css({height: "auto", width: "auto"});
		        $("html").css("overflow","");
	        }
	        document.onkeydown = "";
	        document.onkeyup = "";
	        return false;
        }

        function tp_position() {
        //    $("#tp_window").css({marginLeft: '-' + parseInt((200 / 2),10) + 'px', width: 200 + 'px'});
        //	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        //		$("#tp_window").css({marginTop: '-' + parseInt((200 / 2),10) + 'px'});
        //	}
        }

        function tp_init(domChunk){
	        $(domChunk).click(function(){
	        var t = this.title || this.name || null;
	        var a = this.href || this.alt;
	        var g = this.rel || false;
	        tp_show(t,a,g);
	        this.blur();
	        return false;
	        });
        }		
	    return this.each(function(){
            $(this).mouseover(function () {
				mouseoverHandler(this);
                });
                
            $(this).mouseout(function () {
				mouseoutHandler(this);
                });
                
	    });

    }
   
    
})(jQuery);



function tp_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}


