var presIndex = 1;
var log_id = 0; 
function getPresIndex() { return presIndex; }
function playPresentation(id, playurl, sp2, mediaType)
{	
    var url = playurl + "?id=" + id; 
    var width = screen.width;
    var height = screen.height;
    var left, top;
    if (mediaType == 1) // PowerCam flash version
    {
    	left = (width - 1014) / 2;
        top = 0;
        window.open(url, null, "width=1014, height=675, left=" + left + ", top=" + top + ", menubar=no, resizable=no, status=yes, titlebar=no, toolbar=no");
        return;
	}
	if (mediaType == 5) // PowerCam v5 version
	{
	    top = 0; 
	    left = 0;  
	   
	    // standard size
        if ((width == 1024 && height == 768) || (width == 1280 && height == 1024))        
        {         	
            window.open(url, null, "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + ", menubar=no, resizable=yes, status=yes, titlebar=no, toolbar=no");
            return;
        }
	    
 	    // 50px: powercam inner title, 80px IE url and status bar height
        width = screen.availWidth - 10;  //1024      
        height = Math.floor(((screen.availWidth - 270)/4)*3) + 50;        
        if (height + 80 > screen.availHeight) //wide screen PC
        {            
            height = screen.availHeight - 80;
            width = Math.floor(((height - 50)*4)/3) + 270;
            left = (screen.width - width) / 2;      
        } 
       
        window.open(url, null, "width=" + width + ", height=" + height + ", left=" + left + ", top=0, menubar=no, resizable=yes, status=yes, titlebar=no, toolbar=no");
        return;    
    }
	   
    if (sp2 == 1) 
    {    	
        // new window template for XP/SP2        
        // 1280x1024
        if (width >= 1280 && height >= 1024)
        {        	
            left = (width - 1270) / 2;
            top = 0;
            window.open(url, null, "width=1270, height=864, left=" + left + ", top=" + top + ", menubar=no, resizable=no, status=yes, titlebar=no, toolbar=no");
            return;
        }
        
        // 1024x768
        if (width >= 1024 && height >= 768)
        {        	
            left = (width - 1014) / 2;
            top = 0;
            window.open(url, null, "width=1014, height=675, left=" + left + ", top=" + top + ", menubar=no, resizable=no, status=yes, titlebar=no, toolbar=no");
            return;
        }
        
        // 800x600
        left = top = 0;
        if (width > 790) left = (width - 790) / 2;       
        window.open(url, null, "width=790, height=507, left=" + left + ", top=" + top + ", menubar=no, resizable=no, status=yes, titlebar=no, toolbar=no");
        return ;
    }    
    
    
    if (typeof(pptFixedSize) != "undefined")
    {
	    if (pptFixedSize == "1")   // fixed play window,     
	    {       
			if  (width >= 1024 && height >= 768)
	            window.open(url + "&fixedSize=1", null, "left=0, top=0, width=1024, height=768");
	        else
	            window.open(url + "&fixedSize=1", null, "left=0, top=0, width=800, height=600");   
	 
	        return;
	    }
    }
    
    //---------------------------------------------   
    // special screen size
    // 1280x1024
    if (width >= 1280 && height >= 1024)
    {
        left = (width - 1280) / 2;
        top = 0;
        window.open(url, null, "width=1280, height=1024, left=" + left + ", top=" + top);
        return;
    }
    
    // 1024x768
    if (width >= 1024 && height >= 768)
    {
        left = (width - 1024) / 2;
        top = 0;
        window.open(url, null, "width=1024, height=768, left=" + left + ", top=" + top);
        return;
    }
    
    // 800x600
    left = top = 0;
    if (width > 800) left = (width - 800) / 2;    
    window.open(url, null, "width=800, height=600, left=" + left + ", top=" + top);
}

var playXmlDoc;
function closeWindow() 
{ 
	if (log_id == 0) return;	
	
    try { playXmlDoc = new XMLHttpRequest(); }
    catch(e)
    {
    	var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
        for (var i=0; i<xmlHttpVersions.length && !playXmlDoc; i++) 
        {
            try { playXmlDoc = new ActiveXObject(xmlHttpVersions[i]); }
            catch (e) {}
        }    
	}
	
	playXmlDoc.open("GET", saveUrl + "?log_id=" + log_id + "&timestamp=" + new Date().getTime(), true);    
	playXmlDoc.setRequestHeader('If-Modified-Since', '0');
    playXmlDoc.send(null); 
}


function goPlay(contentID)
{  	
    try { playXmlDoc = new XMLHttpRequest(); }
    catch(e)
    {
    	var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
        for (var i=0; i<xmlHttpVersions.length && !playXmlDoc; i++) 
        {
            try { playXmlDoc = new ActiveXObject(xmlHttpVersions[i]); }
            catch (e) {}
        }    
	}
	playXmlDoc.onreadystatechange = processMetadata; 
    playXmlDoc.open("GET", xmsUrl + "/content/http_get_metadata.php?id=" + contentID, false);
    playXmlDoc.setRequestHeader('If-Modified-Since', '0');
    playXmlDoc.send(null);
  
} 

function processMetadata() 
{
	
    if (playXmlDoc.readyState == 4) 
    {
	    if (playXmlDoc.status == 200) // only if OK
	    {	        
	        delX = "@@";   //format: 0@@123@@../data/...@@1
	        
		    metaData = playXmlDoc.responseText; 
		    //metaData = bytes2BSTR(metaData);    
  		    
		    valArray = metaData.split(delX);  
		    if (valArray.length == 1)
		    {
		    	alert("no permission");
		    }
		    else
		    {		    	
    		    var isPowerCam = valArray[0];
    		    var contentID = valArray[1];
    		    var dataPath = valArray[2];
    		    var isSP2 = parseInt(valArray[3], 10);
    		    var isFlash = parseInt(valArray[4], 10);    		   
    		    if (isPowerCam == 1)
    		    {    		    	
    		    	playPresentation(contentID, xmsUrl + "/content/play.php", isSP2, isFlash);
    		    }
    		    else
    		    {
    		    	window.location.href = xmsUrl + "/content/show.php?id=" + contentID;
				}	
			}                  
        } 
	    else 
	    {
		    alert("There was a problem retrieving the data:\n" + playXmlDoc.statusText);
	    }
    }
} 

function goTodoShow(id)
{
    try
    {
        window.open("/xms/project/todo_log.php?newWin=1&id=" + id); 
    } catch (e) {}
}
