﻿



var videoPlayer = new Array();
var currentMediaIndex=new Array();

 var swfContainer= new Array();
 var swfObj = new Array();
 
var img = new Array();

var slideshowid = new Array();

var isPreviewSmall = new Array();
var isPreview = new Array();
var hasText = new Array();

//var isPreviewLarge = new Array();

var isLarge = new Array();

var fixedHeight = new Array();

var currentThumb = null;
var currentThumbContainer = null;

var mvIndexes = new Array();

var playerWidth=0;
var playerHeight=0;

//var largeWidth;//800;//640;
//var largeHeight;//600;//480;

//var smallWidth;
//var smallHeight;

//var gutterWidth = 20;

//var defaultArticleWidth = 680;
//var articleWidth = new Array();


//$(window).resize(function(){
//  alert("Stop it!");
//});

function appendMvindexes(mvIndex)
{

    mvIndexes[mvIndexes.length] = mvIndex;
}



function initMedia(mvIndex)
{

    calculateSizes(mvIndex);
 
  if (mediaData[mvIndex] !=null)
        {
           
            embedPlayer(mvIndex,0);
  
        
        }
        

}

function calculateSizes(mvIndex)
{

    var bodyDiv = document.getElementById(BodyContainerId[mvIndex]);

    if (hasText[mvIndex]==0) 
          isLarge[mvIndex] = 1;
    else
        isLarge[mvIndex] = 0;

    setSizes(mvIndex);    
}






function setSizes(mvIndex)
{

    var bodyDiv = document.getElementById(BodyContainerId[mvIndex]);

  //  var divArticleTitle = document.getElementById('articleTitle');

    if (mediaData[mvIndex] == null) //show only body
       {
           playerContainer(mvIndex).parentNode.style.display = 'none';
           bodyDiv.style.width = '50%';//articleWidth[mvIndex];
       }
    else {
     

            if (isLarge[mvIndex] == 1)
            {

               bodyDiv.style.display = 'none';
               playerContainer(mvIndex).parentNode.style.width = '100%';
         //      divArticleTitle.style.display='block';

            }
            else
            {
              
               bodyDiv.style.display = '';
               bodyDiv.style.width = '32%';
               playerContainer(mvIndex).parentNode.style.width = '62%';
              
          //     divArticleTitle.style.display = 'none';
           }

            playerWidth = xWidth(playerContainer(mvIndex).parentNode);
            playerHeight = Math.round(playerWidth * 0.7);

       //    playerContainer(mvIndex).parentNode.style.height = playerHeight + 'px';
           playerContainer(mvIndex).style.height =  playerHeight + 'px';

      
       }

    
       

}

//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 toggleLarge(mvIndex,lnkid)
{

    var lnk = document.getElementById(lnkid);

    if (isLarge[mvIndex]==1) 
    {   isLarge[mvIndex] = 0;lnk.src="http://www.utrophia.net/style/images/buttons/larger.gif";  }
    else
    { isLarge[mvIndex] = 1; lnk.src = "http://www.utrophia.net/style/images/buttons/smaller.gif"; }

    setSizes(mvIndex);

    //you tube
    var swfContainerId = 'swfContainer_'+ mvIndex;
    var container = document.getElementById(swfContainerId);
    if (container !=null && container != undefined)
        container.setAttribute('style', 'z-index:1;width:' + playerWidth + 'px;height:' + playerHeight + 'px;');
    


    //flowplayer
    var player = $f(playerContainer(mvIndex));
    player.width = playerWidth;
    player.height = playerHeight;
    player.getPlugin("controls").bottom = 0;


    
    //refresh content if image
    if (mediaData[mvIndex][currentMediaIndex[mvIndex]].type == "image" || mediaData[mvIndex][currentMediaIndex[mvIndex]].type == "audioslide")
        showMedia(mvIndex, currentMediaIndex[mvIndex]);
        


}




function startSlideshow(mvIndex,lnk)
{
//var lnk = document.getElementById(lnkid);
lnk.onclick = function(){stopSlideshow(mvIndex,lnk);}


lnk.src = "http://www.utrophia.net/style/images/buttons/btnpause.gif";
lnk.style.borderColor= '#fab6ce';
slideshowid[mvIndex] = window.setInterval("nextImage(" + mvIndex + ")",5000);

}
function test()
{
alert('test');
}


function stopSlideshow(mvIndex,lnk)
{
window.clearInterval(slideshowid[mvIndex]);
lnk.src = "http://www.utrophia.net/style/images/buttons/btnplay.gif";
lnk.style.borderColor= 'transparent';
//lnk.onclick = "startSlideshow(mvIndex,lnkid)";
lnk.onclick = function(){startSlideshow(mvIndex,lnk);}



}

function nextImage(mvIndex)
{
if (currentMediaIndex[mvIndex] < thumbCount(mvIndex)-1)
{
showMedia(mvIndex,currentMediaIndex[mvIndex]+1);
}
else
{
showMedia(mvIndex,0);
}
}

function previousImage(mvIndex)
{
if (currentMediaIndex[mvIndex] > 0 )
{
showMedia(mvIndex,currentMediaIndex[mvIndex]-1);
}
else
{
showMedia(mvIndex,thumbCount(mvIndex)-1);
}
}


//function preloadImages(mvIndex)
//{

//var sPath;
//for (index=0;index<=thumbCount(mvIndex)-1;index++)
//{
//var data = mediaData[mvIndex][index].split("|*|");
//if (data[0] == "image")
//{

//if (isLarge[mvIndex]) {sPath = data[1];} else {sPath = data[2];}
//objImage = new Image();
//objImage.src = sPath;
////objImage.onLoad = imageLoaded();

//}
//}


//}


//function imageLoaded()
//{
//alert('loaded');
//}

function setCurrentItem(mvIndex,index)
{

   if (currentThumb != null)
        currentThumb.style.borderColor='';

    currentThumb = getThumb(mvIndex,index);

    if (currentThumb != null)
        currentThumb.style.borderColor='#fab6ce';
        
        
      if (currentThumbContainer != null)
      {
        currentThumbContainer.style.backgroundColor ='';
         currentThumbContainer.style.color ='';
        
        }

    currentThumbContainer = getThumbContainer(mvIndex,index);

    if (currentThumbContainer != null)
    {
        currentThumbContainer.style.backgroundColor ='#fbe0e4';
          currentThumbContainer.style.color ='#000000';
        }
        
       
}


function showMedia(mvIndex, index) {


    setMedia(mvIndex, index, true);


}

function removeYoutube(mvIndex) {


  
    var swfContainerId = 'swfContainer_' + mvIndex;
    var swfObjId = 'swf_' + mvIndex;
    
    var swf = swfobject.getObjectById(swfObjId);

    if (swf != null && swf != undefined) 
        swfobject.removeSWF(swfObjId);

    if (document.getElementById(swfContainerId) != null)
        playerContainer(mvIndex).removeChild(document.getElementById(swfContainerId));
        


}

function insertYoutube(mvIndex, sPath) {

    var player = $f(playerContainer(mvIndex));

    if (!player.isHidden()) {
        player.hide();player.mute();
    }

    removeYoutube(mvIndex);


    var width = xWidth(playerContainer(mvIndex));
    var height = Math.round(width * 0.7);

    var swfContainerId = 'swfContainer_' + mvIndex;
    var swfObjId = 'swf_' + mvIndex;

    var swfContainer = document.createElement('div');
    swfContainer.setAttribute('id', swfContainerId);
    swfContainer.setAttribute('style', 'z-index:1;overflow:hidden;');
    playerContainer(mvIndex).appendChild(swfContainer);


    swfobject.embedSWF(sPath, swfContainerId, width, height, "9.0.0", false, {}, { wmode: "opaque" }, { id: swfObjId });
    
}

function setMedia(mvIndex,index,play) {



   
   var clipData = mediaData[mvIndex][index];


   var player = $f(playerContainer(mvIndex));


   playerContainer(mvIndex).setAttribute('title', clipData.name);

    setCurrentItem(mvIndex, index);

    currentMediaIndex[mvIndex] = index;
    
       
    var sPath;
    
    if (isLarge[mvIndex])
        sPath = clipData.pathLarge;
    else
        sPath = clipData.pathMedium;


    if (clipData.type == "youtube") 
        insertYoutube(mvIndex, clipData.pathLarge);
    else 
    {

        if (player.isHidden()) 
        { 
            removeYoutube(mvIndex);
            player.show();
            player.height = playerHeight;//does not set hieght after call to 'show()
            player.getPlugin("controls").bottom = 0;
          
         }
            
    
      switch (clipData.type) {

          case "audioslide":

              player.getPlugin("controls").fadeIn();
              player.getPlugin("myContent").fadeOut();

              var sPath2;

              if (isLarge[mvIndex])
                  sPath2 = mediaData[mvIndex][index + 1].pathLarge;
              else
                  sPath2 = mediaData[mvIndex][index + 1].pathMedium;

              player.setPlaylist([{ url: sPath, duration: 0,autoPlay:true }, { url: sPath2, image: false,autoPlay:true}]);//problem with stream if audio clip is not set to autoplay
              
            break;

        case "audio":

            player.getPlugin("controls").fadeIn();

            var text = player.getPlugin("myContent");
            text.setHtml("<h3>Playing: " + clipData.name + "</h3>");
            text.fadeIn(1000);

            player.setPlaylist([{ url: sPath,autoPlay:false}]);

            break;
                
        case "video":
           
            player.getPlugin("controls").fadeIn();
            player.getPlugin("myContent").fadeOut();

            player.setPlaylist([{ url: sPath}]);
             
            break;

        case "image":
         
            player.getPlugin("controls").fadeOut();
            player.getPlugin("myContent").fadeOut();
            
            player.setPlaylist([{ url: sPath,duration:5000}]);
            
            break;

        case "flash":
        
            player.getPlugin("controls").fadeIn();
            player.getPlugin("myContent").fadeOut();

             player.setPlaylist([{ url: sPath}]);
            
            break;
        }
  
    
	player.unmute();


        if (play)
            player.play(0);
        else
            player.pause();
  
    
  }
    
  	

   

}

function playerContainer(mvIndex)
{
return document.getElementById(MediaContainerId[mvIndex]);
}


function embedPlayer(mvIndex,index) {
    



//embedMedia1.style.width = '100%'; 


 $f(
       playerContainer(mvIndex)
        ,  
        
        { src: "../ClientScripts/media/flowplayer.commercial-3.1.1.swf",  bgcolor: "#FFFFFF",  wmode: 'opaque' }
       ,   
        { key: '#$a34b0b2fdafb4da7946'
        ,
          onLoad: function() {      setMedia(mvIndex, index, false);  }
          ,

            clip: { autoPlay: false, autoBuffering: true}
     
    ,
//     
//        // a clip event is defined inside clip object
//          
//        }
//    }
    
  
      
        

            canvas: {backgroundColor:'#FFFFFF', backgroundGradient: 'none', opacity:0.0 }
            ,
            plugins: { 
                audio: {url: '../ClientScripts/media/flowplayer.audio-3.1.0.swf'} 
                ,
                controls: {url: '../ClientScripts/media/flowplayer.controls-3.1.1.swf', bottom: 0, opacity: 0.6, 
     
                            // styling properties (will be applied to all plugins) 
                            backgroundColor: '#2d3e46', 
                            backgroundGradient: 'low', 
                     
                            // controlbar specific settings 
                            timeColor: '#f27c8a', 
                            progressColor: '#f27c8a', 
                            bufferColor:'#fbe0e4',
                            autoHide: 'always',
                            hideDelay: 2000,
                          //  all: false, 
                          //  play: true, 
                          //  scrubber: true, 
                     
                            // tooltips (since 3.1) 
                            tooltips: {buttons: true, fullscreen: 'Enter fullscreen mode'} 
                            }
                 ,
                 myContent: { 
     
                            // location of the plugin 
                            url: '../ClientScripts/media/flowplayer.content-3.1.0.swf', 
                     
                            // display properties 
                            top: 50, 
                            width: 300, 
                            borderRadius: 10,          
                            html: "<h3>Playing: " + name + "</h3>",
                            opacity:0
                            
                     
                            }
                             
                 }  
         
         
              }
        ); 
        
        
        
    
}


function thumbCount(mvIndex)
{
var thumbDiv = document.getElementById(ThumbnailContainerId[mvIndex]);
var thumbs = thumbDiv.getElementsByTagName('img');
return thumbs.length;

}


function getThumb(mvIndex,index)
{

var thumbDiv = document.getElementById(ThumbnailContainerId[mvIndex]);

var thumbs = thumbDiv.getElementsByTagName('img');
 for(var i=0;i<thumbs.length; i++){

 if (i==index ) return thumbs[i];

 
    }
    return null;

}

function getThumbContainer(mvIndex,index)
{

var thumbDiv = document.getElementById(ThumbnailContainerId[mvIndex]);

var thumbContainers = thumbDiv.getElementsByTagName('div');
 for(var i=0;i<thumbContainers.length; i++){

 if (i==index ) 
 {
 if (thumbContainers[i].style.width=="100%")//to avoid highlighting background if in thumbnail mode
 return  thumbContainers[i];
 else
 return null;
 }

 
    }
    return null;

}




function PrepareSwfContainer(mvIndex)
{

if (swfContainer[mvIndex]==null)
{
        var swfContainerId = 'swfContainer_'+ mvIndex;
         swfContainer[mvIndex] = document.createElement('div');
         swfContainer[mvIndex].setAttribute('id',swfContainerId);
          swfContainer[mvIndex].setAttribute('style','z-index:1;overflow:hidden;');
        //var im = new Image();
        //im.src = "/seedsprouts/utrophia/images/white.jpg";
        //im.width=1000;im.height=1000;
        //swfContainer[mvIndex].appendChild(im);
        //swfContainer[mvIndex].InnerHTML =  '<img src="/seedsprouts/utrophia/images/white.jpg" alt="media" width="1000" height="1000" />'
         var embedMedia1 = document.getElementById(MediaContainerId[mvIndex]);
        embedMedia1.appendChild(swfContainer[mvIndex]);
        
 }
 else //if it exists, make sure its empty
 {
         if (document.getElementById('swf_' + mvIndex)!=null)
        {
        swfobject.removeSWF('swf_' + mvIndex);
   //   swfContainer[mvIndex].removeChild(document.getElementById('swf_' + mvIndex));
      
    //    swfContainer[mvIndex].InnerHTML = '';
   //     alert(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';

}

function showHide(mvIndex,elID,lnk)
{
var el = document.getElementById(elID);
//lnk.innerHTML = (el.style.display != 'none' ? 'show thumbnails' : 'hide thumbnails' );
el.style.display = (el.style.display != 'none' ? 'none' : '' );
}
