﻿


var currentMediaIndex=new Array();

 var swfContainer= new Array();
 var swfObj = new Array();
 
var img = new Array();

var isLarge = new Array();

//var largeWidth;//800;//640;
//var largeHeight;//600;//480;

//var smallWidth;
//var smallHeight;

var gutterWidth = 20;

var defaultArticleWidth = 680;
var articleWidth = new Array();



function calculateSizes(mvIndex)
{
//obtain container (article) width
var mediaDiv = document.getElementById(OuterMediaContainerId[mvIndex]);
var elArticle  = mediaDiv.parentNode;
articleWidth[mvIndex] = xWidth(elArticle);

isLarge[mvIndex] = 0;

//incase get width fails
if (articleWidth[mvIndex]==0) articleWidth[mvIndex] = defaultArticleWidth;

setSizes(mvIndex);
}






function setSizes(mvIndex)
{
var thumbDiv = document.getElementById(ThumbnailContainerId[mvIndex]);
var bodyDiv = document.getElementById(BodyContainerId[mvIndex]);
var mediaDiv = document.getElementById(OuterMediaContainerId[mvIndex]);

var bodyWidth;

if (mediaData[mvIndex] == null) //show only body
   {
        mediaDiv.style.display = 'none';
        thumbDiv.style.display = 'none';
        bodyWidth = articleWidth[mvIndex];
     //   bodyDiv.style.width = articleWidth[mvIndex] + 'px';
   }
else
   {

        //set media display to large if there's not text
        if (Trim(bodyDiv.innerHTML) == '') 
            {isLarge[mvIndex] = 1;removeToggleLink();}

        if (isLarge[mvIndex] == 1)
            {
                bodyDiv.style.display = 'none';
                bodyWidth=0;
            }
        else
            {

                bodyDiv.style.display = '';
                bodyWidth = (articleWidth[mvIndex] - getWidth(mvIndex) - gutterWidth );
              //  bodyDiv.style.width = (articleWidth[mvIndex] - getWidth(mvIndex) - gutterWidth ) + 'px';
            }

        mediaDiv.style.width = getWidth(mvIndex) + 'px';
        thumbDiv.style.width = getWidth(mvIndex) + 'px';
    }

bodyDiv.style.width = bodyWidth + 'px';



//set the map size for place page if it exists
var mapDiv = xGetElementById('map');
if (mapDiv!=null)
     {
         mapDiv.style.width = bodyWidth + 'px'; 
         mapDiv.style.height = Math.round((3 * bodyWidth) / 4) + 'px';
         initMap();     
     }


}

function Trim(str)
{  

while(str.charAt(0) == " " || str.charAt(0) == "\n")
  {  
  str = str.substring(1);

  }
  while(str.charAt(str.length-1) == " " || str.charAt(str.length-1) == "\n")
  {  str = str.substring(0,str.length-1);

  }
  return str;
}




function getWidth(mvIndex)
{if (isLarge[mvIndex] == 1)
{return articleWidth[mvIndex];}
else
{return Math.round((articleWidth[mvIndex] / 2) - (gutterWidth / 2));}
}

function getHeight(mvIndex)
{
return Math.round((3 * getWidth(mvIndex)) / 4);
}



function toggleLarge(mvIndex)
{

var mediaDiv = document.getElementById(OuterMediaContainerId[mvIndex]);
//swfobject.removeSWF('swf');

if (isLarge[mvIndex]==1) 
{isLarge[mvIndex] = 0;
mediaDiv.setAttribute('title','Click to Enlarge');
}
else
{
isLarge[mvIndex] = 1;
mediaDiv.setAttribute('title','Click to view article');
}

//alert('r');
setSizes(mvIndex);
showMedia(mvIndex,currentMediaIndex[mvIndex]);

}




function removeToggleLink()
{
//elToggleLarge = document.getElementById('lnkToggleLarge');
//elToggleLarge.style.display='none';
}

function showMedia(mvIndex,index)
{

if (mediaData[mvIndex] ==null)
{
removeToggleLink();
}
else
{

currentMediaIndex[mvIndex] = index;

var data = mediaData[mvIndex][index].split("|*|");

var type = data[0];
var sPath = data[1];

var width = getWidth(mvIndex);
var height = getHeight(mvIndex);



var embedMedia1 = document.getElementById(MediaContainerId[mvIndex]);

var swfContainerId = 'swfContainer_'+ mvIndex;


//remove any image if present
if (img[mvIndex]!=null && img[mvIndex].parentNode!=null ) {img[mvIndex].parentNode.removeChild(img[mvIndex]);}


embedMedia1.style.width = width + 'px';
embedMedia1.style.height = height + 'px';

//remove swfobject
var swfObjId = 'swf_' + mvIndex;
if (document.getElementById(swfObjId)!=null)
{swfobject.removeSWF(swfObjId);}

embedMedia1.innerHTML = '';

switch(type)
{
case "image":




sPath = imageHandler + '&pad=true' + '&imageFile=' + sPath + '&width=' + width + '&height=' + height;//append size info for image resizer


img[mvIndex] = new Image();

img[mvIndex].src = sPath
//img[mvIndex].style.visibility='hidden';//hide until centered
//img[mvIndex].onload = function(){return centreImage(mvIndex);}

embedMedia1.appendChild(img[mvIndex]);



  break;    
case "audio":



CreateSwfContainer(mvIndex);

var flashvars = {src: sPath, autostart: "yes"};
var params = {};
var attributes = {id:swfObjId};

swfobject.embedSWF("../ClientScripts/media/emff_lila_info.swf", swfContainerId, width, height, "9.0.0", false, flashvars, params, attributes);
  
  
  break;
  case "flash":

CreateSwfContainer(mvIndex);


 var flashvars = {};
var params = {autostart: "yes"};
var attributes = {id: swfObjId};
swfobject.embedSWF(sPath, swfContainerId, width, height, "9.0.0", false, flashvars, params, attributes);

  break;
  case "flashVideo":
  
CreateSwfContainer(mvIndex);
 
var flashvars = {};
var params = {};
var attributes = {id: swfObjId};
swfobject.embedSWF("../ClientScripts/media/flvPlayer.swf", swfContainerId, width, height, "9.0.0", false, flashvars, params, attributes);

  break;
  
default:
  
  }
}
}

function CreateSwfContainer(mvIndex)
{

if (swfContainer[mvIndex]==null)
{
var swfContainerId = 'swfContainer_'+ mvIndex;
 swfContainer[mvIndex] = document.createElement('div');
 swfContainer[mvIndex].setAttribute('id',swfContainerId);
 }
 var embedMedia1 = document.getElementById(MediaContainerId[mvIndex]);
embedMedia1.appendChild(swfContainer[mvIndex]);
}


function centreImage(mvIndex)
{

var imgWidth = xWidth(img[mvIndex]);
var imgHeight = xHeight(img[mvIndex]);

img[mvIndex].style.position = 'relative';

var width = getWidth(mvIndex);
var height = getHeight(mvIndex);

if (imgHeight > 0 )
{img[mvIndex].style.top = Math.round((height - imgHeight) / 2) + 'px';}

if (imgWidth > 0 && isLarge==1 ) 
{img[mvIndex].style.left = Math.round((width - imgWidth) / 2) + 'px';}

img[mvIndex].style.visibility='visible';

}
