var douwe_objPopUp; //reference to the content in an inline popup
var douwe_timeronResizePopUp; // reference to the timer of the heartbeat that checks if the popup still fits into the view every 300 miliseconds
var douwe_timerLeftPopup; // reference to the timer which monitors the left offset, which prevents the popup exceeding the left edge.
var douwe_popupLeft; // left margin offset

//config BlockUI
$.blockUI.defaults = {
    // styles for the overlay iframe
    overlayCSS:  { backgroundColor: '#251101', cursor: 'auto', opacity: 0.5	 },
    // styles for the message when blocking the entire page
    pageMessageCSS:    { margin:'-50px 0 0 -240px',width:'480px', top:'50%', left:'50%', cursor:'auto'},
    // styles for the message when blocking an element
    elementMessageCSS: { width:'250px', padding:'10px', textAlign:'center'},
    // allow body element to be stetched in ie6
    ie6Stretch: 1
};

function newContentInlinePopUp(params){//the params is an object that always contains a property type.. this determines what kind of content is going to be passed
	//callback          
	switch(params.type){
        case "videolink": 
                $.blockUI("<div id='video_popup'></div>",{margin:'-50px 0 0 -197px',height:'344px',width:'394px', padding: '1px', border:'solid white 1px'});//{margin:'-50px 0 0 -484px',width:'959px', height:'393px', padding: '1px', border:'solid white 1px'}
                var flashvars = {};
                flashvars.videoSource = params.href;
 				flashvars.videoTitle = params.title;
				var flashparams = {};
				flashparams.menu = "false";
				flashparams.bgcolor = "#333333";
				flashparams.base = "/Style%20Library/Senseo/static/flash";
                flashparams.quality = "high";
                flashparams.seamlesstabbing = "false";
                var attributes = {};
 				attributes.id = "video_popup";
				swfobject.embedSWF("/Style%20Library/Senseo/static/flash/preload.swf?mMovieName=/Style%20Library/Senseo/static/flash/videoplayer.swf", "video_popup", "394", "344", "8", "/Style%20Library/Senseo/static/flash/expressInstall.swf", flashvars, params, attributes);                
                break;
        case "imagelink":
                $.blockUI("<div id='foto_popup'><img src='"+params.href+"' onload='syncPositionPopupToImageSize(this)'/></div>",{margin:'-50px 0 0 -100px',height:'auto',width:'auto', padding: '1px', border:'solid white 1px'});//{margin:'-50px 0 0 -484px',width:'959px', height:'393px', padding: '1px', border:'solid white 1px'}
                break;
	}
	//position popup vertically
	douwe_objPopUp = $("#blockMsg")
	var intHeightPopUp = douwe_objPopUp.height();
	var intHeightView = document.documentElement?document.documentElement.clientHeight:document.body.clientHeight;
	if(intHeightPopUp<=intHeightView-50){//does popup fit into view??
		if(douwe_objPopUp[0].style.top=="50%")douwe_objPopUp.css({marginTop:-douwe_objPopUp.height()/2});//adjust margintop to new height of popup in all the browsers that support position:fixed correctly (and where blockUI makes use of it which you can see by the fact that top=50%)
		else{}//do nothing because vertical positioning is done in IE6 by javascript expressions in the css which take the new height into account
		douwe_timeronResizePopUp = setInterval(onResizePopUp,300);//start checking the popup for resize events
	}
	else{//don't use position fixed because you want to be able scroll the popup so set position to absolute and position popup just below top of the view
		switchToAbsolutePositioning();
	}
	
		// monitor left offset, negative margin could position left edge outside of the window, which is bad.
		clearInterval(douwe_timerLeftPopup);
		douwe_timerLeftPopup = setInterval(monitorLeftPosition, 500);
				
    //provide printing styles via print.css
    $("body").addClass("printinlinepopup")
    //add close button to pop-up
	$('#blockMsg').prepend("<div class='closepopup' onclick='clearInterval(douwe_timeronResizePopUp);$.unblockUI();$(\"body\").removeClass(\"printinlinepopup\")'><img src='/Style%20Library/Senseo/static/images/buttons/closepopup.png'/></div>");
	return false;
}

function switchToAbsolutePositioning(){//switch the way the popup is positioned in the view from static to absolute
    if(!($("embed",douwe_objPopUp[0]).length>0 &&  navigator.userAgent.toLowerCase().indexOf('firefox')!=-1)){//when there is flash in the layer and we reside in FF then don't change the position of the layer to absolute because that stops the flash
        var intScrollTopView = self.pageYOffset?self.pageYOffset:(document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop
        try{douwe_objPopUp[0].style.removeExpression("top")}catch(e){}//remove simulation of position:fixed in IE6     
				douwe_objPopUp.css({"position":"absolute","marginTop":0,"top":intScrollTopView+20});
    }
    else
    {
        douwe_objPopUp.css({"top":"30px","left":"auto","right":"0px","margin":"0px"})//to make sure the close button is visible
    }
	  clearInterval(douwe_timeronResizePopUp);
}

function monitorLeftPosition() {
		var viewWidth = document.documentElement? document.documentElement.clientWidth : document.body.clientWidth;
		var popWidth = douwe_objPopUp[0].offsetWidth;
		var mLeft = parseInt(popWidth/2, 10);
		if(viewWidth < popWidth) {
		  mLeft = parseInt(viewWidth/2, 10);
		}
		
		if(mLeft != douwe_popupLeft) {
			douwe_popupLeft = mLeft;
			douwe_objPopUp.css({"marginLeft": (- douwe_popupLeft)+"px" });
		}
}

function onResizePopUp(){//heartbeat that checks every 300 miliseconds if the popup still fits in the view (onresize doesn't work in mozilla :o( )anyone a better solution for this? (let me know at thomas@tpm-webapplicaties.nl!)
	var intScrollTopView = self.pageYOffset?self.pageYOffset:(document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop
    var intHeightPopUp = douwe_objPopUp[0].offsetHeight;
	var intHeightView = document.documentElement?document.documentElement.clientHeight:document.body.clientHeight;
   // console.log(intHeightPopUp+douwe_objPopUp[0].offsetTop+50 + " ---- " +(intHeightView))
	if(intHeightPopUp+douwe_objPopUp[0].offsetTop+10>intHeightView){switchToAbsolutePositioning()}
}
