var ajaxPopup;
var divPopupContent;
var divPopupWorking;
var divPopupClose;
var popupVideo = false;
var popupOpen = false;

function getNewXMLHttp() {
	xmlhttp = null;

    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = null;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = null;
        	}
		}
    }
    
    return xmlhttp;
}

function initPreview() {
	divPopupContent = document.getElementById('popup-data');
	divPopupWorking = document.getElementById('popup-working');
	divPopupClose = document.getElementById('popup-close');
	divPopupContent.innerHTML = '';
	ajaxPopup = getNewXMLHttp();
}


function displayPreview(project, video) {
	initPreview();
	popupVideo = video;
	divPopupContent.style.display = 'none';

	document.getElementById('popup').style.display = 'block';
	
	var myFx = new Fx.Elements($$('#popup #bg-fill'), {
		onComplete: function(){
        	var sizeMorph = new Fx.Morph('layer1', {
        		onComplete: function() {
        			popupOpen = true;
        			divPopupClose.style.display = 'block';
        			divPopupWorking.style.display = 'block';
        			url = "/popup-get-project.php";
					str = "project=" + escape(project);
							
					ajaxPopup.open("POST", url, true);
					ajaxPopup.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	    
					ajaxPopup.onreadystatechange = displayPreviewComplete;	
					ajaxPopup.send(str);
        		}, 
        		duration: 300, transition: Fx.Transitions.Sine.easeOut
        		} );
			sizeMorph.start({
				width: '899px',
				height: '598px',
				marginLeft: '-450px',
				marginTop: '-299px'
			});	
    	},
    	duration: 300, transition: Fx.Transitions.Sine.easeIn
	});
	myFx.start({ '0': { 'opacity': [0,0.8] } });

	return false;
}


function displayPreviewComplete() {
	if(ajaxPopup.readyState != 4) return;	
		
	var text = String(ajaxPopup.responseText);
	divPopupContent.innerHTML = text;
	
	if(popupVideo == true) {
		var obj = {      
	  
		    clip: { 
		        autoPlay: true, 
		        autoBuffering: false,
		        scaling: 'fit'
		    }, 
		    
		    canvas: {
		    	backgroundColor:'#000000', 
		        backgroundGradient: 'none'
		    }, 
		    
		    plugins:  { 		       
		        controls: {  
		        	opacity: 1,		        
		        	fontColor: '#b5121b',           
		            backgroundGradient: 'none', 
		            backgroundColor: '#e8e6e0',
		             
		            all:false, 
		            scrubber:true, 
		            play:true, 
		            mute:false,
		            volume: true,
		            fullscreen: true,
		            
		            // custom colors 
		            bufferColor: '#0c0000', 
		            bufferGradient: 'low',
		            
		            progressColor: '#b5121b',
		            progressGradient: 'low',             
		            
		            buttonColor: '#0c0000', 
		            buttonOverColor: '#0c0000', 
		             
		            // custom height 
		            height: 30, 
		             
		            // setup auto hide 
		            autoHide: 'always', 
		             
		            // a little more styling              
		            width: '95%',  
		            bottom: 10, 
		            left: '50%', 
		            borderRadius: 10 
		        }         
		    } 
		   };
		
		var params = { src: '/Assets/flowplayer/flowplayer-3.0.7.swf', wmode: 'transparent', width: 699, height: 598 };
		flowplayer("player", params, obj);
		 
	} else {
		var myGallery = new gallery( $('popup-content'), { 
			timed: false,
			showArrows: true,
			showCarousel: false,
			embedLinks: false,
			useHistoryManager: false,
			useThumbGenerator: false,
			showInfopane: true,
			slideInfoZoneSlide: false,
			fadeInfoZoneSlide: false,
			titleSelector: "span",
			slideInfoZoneOpacity: 1
		} );
	}		
	divPopupWorking.style.display = 'none';
	divPopupContent.style.display = 'block';	
}

function closePreview() {
	if(!popupOpen) return;
	
	initPreview();
	
	divPopupContent.style.display = 'none';
	divPopupWorking.style.display = 'none';
	divPopupClose.style.display = 'none';
    divPopupContent.innerHTML = ''; 			

	var sizeMorph = new Fx.Morph('layer1', {
     	onComplete: function() {
     		var myFx = new Fx.Elements($$('#popup #bg-fill'), {
				onComplete: function(){
        			document.getElementById('popup').style.display = 'none';
        			popupOpen = false;
    			},
    			duration: 300, transition: Fx.Transitions.Sine.easeOut
			});
			myFx.start({ '0': { 'opacity': [0.8, 0] } });	
     	}, 
     	duration: 500, transition: Fx.Transitions.Sine.easeOut
     	} );
	sizeMorph.start({
		width: ['898px', '0px'],
		height: ['599px', '0px'],
		marginLeft: ['-449px', '0px'],
		marginTop: ['-299px', '0px']
	});
}

if (document.addEventListener) {       
   document.addEventListener("keypress",keypress,false);              
} else if (document.attachEvent) {       
   document.attachEvent("onkeypress", keypress);
} else {
   document.onkeypress= keypress;
}
    
function keypress(e)
{
	if (!e) e= event;
	//alert(e.keyCode);
   
	switch(e.keyCode){
		case 27:
			closePreview();
		default:
			break;
	}
}