$(document).ready(function() { 
  $(".openDemo").click(function(){
   	var currDemo = $(this).attr("rel").split('|');
   	var currDemoPath = "/nl-be/swf/"+currDemo[1]+".swf";
   	var currDemoDiv = currDemo[1]+"-flash";
   	
   	var closeBtn = currDemo[1]+"-close";

   	openDivCenter(currDemo[1], closeBtn);
   	
   	loadFlashObject(currDemoDiv, currDemoPath, currDemo, "491", "300", "9", "#000000");
		
   });
   
 function openDivCenter(divID,closeBtnID)
{
    var toShow="";
    // Build the background overlay div
    toShow += "<div class='site_overlay'></div>";
    $('body').append(toShow);

    $('div.site_overlay').css('height',$(document).height()).bind('click',function(){
	closeDiv(divID);
    });

    $('div.site_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
	showPopupDivCenter(divID);
	$('div.site_overlay').css("z-index","9000");
	$('#'+divID).css("z-index","9500");
    });

	
    if(closeBtnID!=""){
        $("#"+closeBtnID).bind('click',function(){
	        closeDiv(divID);
        });
    }
}  
   
function loadFlashObject(divID, swfPath, swfID, w, h, version, bgcolor)
{
     var ua  = navigator.userAgent.toLowerCase();
     var mac = ua.indexOf("mac") != -1; ;
     var safari = jQuery.browser.safari;

     var so = new SWFObject(swfPath, swfID, w, h, version, bgcolor);
     
     so.addParam("wmode", "transparent");
     so.write(divID);
}   
   
 function showPopupDivCenter(divID) 
{   
    var toShow = $("#"+divID); 
    toShow.css({"z-index":"9000","visibility":"visible"});
    centerLyr(divID);
    rePositionLyr(true,divID);
}  
   
 function closeDiv(divID)
{
    $('div.site_overlay').remove();
    rePositionLyr(false,divID);
    var toHide = $("#"+divID); 
    toHide.css({"z-index":"0","visibility":"hidden"});
    if(divID=="all-products"){
	toHide.css("width", "1px");
	toHide.css("height", "1px");
    }
} 
 function centerLyr(divID)
{
    $('#'+divID).css("position","absolute");
 
    var newTop = ( $(window).height() - $('#'+divID).height() ) / 2+$(window).scrollTop();
    if(newTop<0) newTop = 0;

    var newLeft = ( $(window).width() - $('#'+divID).width() ) / 2+$(window).scrollLeft();
    if(newLeft<0) newLeft = 0;

    $('#'+divID).css("top", newTop + "px");
    $('#'+divID).css("left", newLeft + "px");
} 

function rePositionLyr(status,divID) 
{
    switch(status) {
	case true:
		$(window).bind('resize', function() {
			centerLyr(divID);
		});
		//scroll is not used here
		//$(window).bind('scroll', function() {
		//	centerLyr(divID);
		//});
	    break;
	case false:
		$(window).unbind('resize');
		//scroll is not used here
		//$(window).unbind('scroll');
	    break;
    }
}
 });
