
//default photo on flash off
var _ajaxLoaderSrc = '/_LAYOUTS/BigBadMediaGallery/images/gallery/ajax-loader.gif';
var _flashOn = 0;
var _photoOn = 1;
var _activePhoto  = 0;
var _flashWidth = 400;
var _flashHeight = 300
var _flashMovieTemplate = '/_layouts/bigbadmediagallery/flash/flvplayer.swf';
var _pageNum = 1;
var _thisWebId;
var _galleryImages = new Array();

$(window).load(function() {    
 var prev = _activePhoto - 1;
 var next = _activePhoto + 1;
 
 pic1 = new Image();
 pic1.src = " + _galleryImages[prev][1] + ";
 
 pic2 = new Image();
 pic2.src = " + _galleryImages[next][1] + ";
});
//Preload ALL images
$(window).load(function() 
{
    for(var i = 0; i < _galleryImages.length; i++)
    {     
        pic = new Image();
        pic.src = " + _galleryImages[i][1] + "; 
    }
});


function msngnPlayMedia ( url, showImage )
{

    var targetDiv;
    
    //depending on audio/video set the appropriate settings
    if (showImage && showImage>0)
    {              
        ensureFlashActive('stage');        
        targetDiv = 'stage';
        flashWidth = _flashWidth;
        flashHeight = _flashHeight;  

//        if you want to hide the audio player use the code below
//        ensureFlashActive('phantomPlayer');        
//        targetDiv = 'phantomPlayer';       
//        msngnShowPhoto(0);
//        flashWidth = 0;
//        flashHeight = 0;
    }
    else
    {
        ensureFlashActive('stage');        
        targetDiv = 'stage';        
        flashWidth = _flashWidth;
        flashHeight = _flashHeight;        
    }      
    
    //build out the media url
    //var flvUrl = _flashMovieTemplate + '?file=' + url + '&autoStart=true';
    //var flvUrl = "mediaURL=" + url;
    var flvUrl = "var1=" + url;
        
    //bind to the correct target 
    //alert(flvUrl);           
    var flvPlayer = new SWFObject(_flashMovieTemplate, "flvPlayer", flashWidth, flashHeight, "", "#FFFFFF","high", "", "" , "");
    flvPlayer.addVariable("var1", url);
    //so.addVariable("flashVarText", "this is passed in via FlashVars for example only");
    //so.addParam("scale", "noscale");
    flvPlayer.write(targetDiv);      

}

function msngnPlayMedia ( url, showImage, isAudio, caption, copyright )
{

    var targetDiv;
    
    //depending on audio/video set the appropriate settings
    if (showImage && showImage>0)
    {              
        ensureFlashActive('stage');        
        targetDiv = 'stage';
        flashWidth = _flashWidth;
        flashHeight = _flashHeight;  

//        if you want to hide the audio player use the code below
//        ensureFlashActive('phantomPlayer');        
//        targetDiv = 'phantomPlayer';       
//        msngnShowPhoto(0);
//        flashWidth = 0;
//        flashHeight = 0;
    }
    else
    {
        ensureFlashActive('stage');        
        targetDiv = 'stage';        
        flashWidth = _flashWidth;
        flashHeight = _flashHeight;        
    }      
    
    //build out the media url
    //var flvUrl = _flashMovieTemplate + '?file=' + url + '&autoStart=true';
    //var flvUrl = "mediaURL=" + url;
    var flvUrl = "var1=" + url;
        
    //bind to the correct target 
    //alert(flvUrl);
    //alert("isAudio " + isAudio);           
    var flvPlayer = new SWFObject(_flashMovieTemplate, "flvPlayer", flashWidth, flashHeight, "", "#FFFFFF","high", "", "" , "");
    flvPlayer.addVariable("var1", url);    
    flvPlayer.addVariable("var2", isAudio);
    flvPlayer.addVariable("var3", "/PublishingImages/audio_default.jpg");
    //so.addVariable("flashVarText", "this is passed in via FlashVars for example only");
    //so.addParam("scale", "noscale");
    flvPlayer.write(targetDiv);          
    document.getElementById('captionContainer').innerHTML = caption;
    document.getElementById('copyrightContainer').innerHTML = copyright; 
}
//<object id="msngnFlashObjectTag" width="400" height="400">
//  <param id="msngnFlashParamTag" movie="~/_layouts/bigbadmediagallery/flash/flvplayer.swf"></param>
//  <param flashvars=""></param>
//  <embed id="msngnFlashEmbedTag" src="~/_layouts/bigbadmediagallery/flash/flvplayer.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="400" height="400" flashvars=""></embed>
//</object>
    

function msngnShowPhoto( imageIdx )
{
    _activePhoto = imageIdx;
    if(document.getElementById('phantomPlayer'))
        document.getElementById('phantomPlayer').innerHTML = '';
    if(document.getElementById('stage'))    
        document.getElementById('stage').innerHTML = '';
    
    //if ($("#galleryMainImage").length == 0)
    //{
     //   document.getElementById('stage').innerHTML = '';
     //   img = document.createElement('img');
     //   img.setAttribute('id', 'galleryMainImage');
     //   img.setAttribute('src', _galleryImages[imageIdx][1]);
     //   img.setAttribute('class', 'mainImage');
     //   ensurePhotoActive();
     //   document.getElementById('stage').appendChild(img);  
        
    //}else{ 
    
        var src = _galleryImages[imageIdx][1];
        if ($("#galleryMainImage").length == 0) 
        {
            var img = "<img class='mainImage' id='galleryMainImage' src='" + src + "' />";
            $("#stage").append(img);
            
            } 
            else 
            {
                var img = "<img id='hiddenImage' src='" + src + "' style='position: absolute; left: -999em' />";
                $("#stage").append(img);
                var width = $('#hiddenImage').width();
                $("#galleryMainImage").attr("src", src);
                $('#hiddenImage').remove();
            }
         

    //}
    
    document.getElementById('captionContainer').innerHTML = _galleryImages[imageIdx][3];
    document.getElementById('copyrightContainer').innerHTML = _galleryImages[imageIdx][4]; 
    
    if ( imageIdx > 0 )
    {
        //document.getElementById('introTextContainer').style.display = 'none';
        phantom = document.getElementById('phantomPlayer'); 
                    
        if (phantom.hasChildNodes())
            phantom.removeChild(phantom.firstChild);        
    }
    
    //set the navigation
    prevLink = document.getElementById('galleryPrevLink');
    nextLink = document.getElementById('galleryNextLink');
    bar = document.getElementById('spnLnkNextBar');
    
    prevLink.href = 'javascript:msngnShowPhoto(' + (imageIdx-1) + ');';
    nextLink.href = 'javascript:msngnShowPhoto(' + (imageIdx+1) + ');';
    
    //default visibility
    prevLink.style.display = 'inline';
    nextLink.style.display = 'inline';
    bar.style.display  = 'inline';            
      
    //if showing first image            
    if ( imageIdx == 0 )
    {
        prevLink.style.display = 'none';
        nextLink.style.display = 'inline';
        bar.style.display  = 'none';
    }

    //if showing last image
    if ( imageIdx == _galleryImages.length-1 )
    {
        nextLink.style.display = 'none';
        bar.style.display  = 'none';
    }     
}


function ensureFlashActive(target)
{
    
    //clear the target out
    document.getElementById('phantomPlayer').innerHTML = '';
    document.getElementById('stage').innerHTML = '';
    
//    if ( _flashOn == 0 )
//    {
        //document.getElementById(target).style.textAlign = 'center';
        document.getElementById('captionContainer').innerHTML = '';
        document.getElementById('copyrightContainer').innerHTML = '';            
        document.getElementById('picNavContainer').style.display = 'none';
        document.getElementById('pageNavContainer').style.display = 'none';
        document.getElementById('captionDiv').style.display = 'inline';
        
        if ( document.getElementById('galleryMainImage') )
            document.getElementById('galleryMainImage').style.display = 'none';
        
        _flashOn = 1;
        _photoOn = 0;        
//    }   

    
}

function ensurePhotoActive()
{
//    if ( _photoOn == 0 )
//    {           
        //clear the target out
        //document.getElementById('stage').innerHTML = '';
        
        //document.getElementById('stage').style.textAlign = 'center';
        document.getElementById('picNavContainer').style.display = 'block';
        document.getElementById('pageNavContainer').style.display = 'block';
        document.getElementById('captionDiv').style.display = 'block';
        
        //if ( document.getElementById('galleryMainImage') )
        //    document.getElementById('galleryMainImage').style.display = 'none';
        
        
        _flashOn = 0;
        _photoOn = 1;
//    }  
}


function drawGallery()
{
    var thumbsTable = document.getElementById('galleryImagesContainer');
    var rowIndex = 0;
    var picsPerRow = 5;
    var row; 
    var cell;
    var img;
    var tBody;
    
    //clear the gallery
    if ( thumbsTable.hasChildNodes() )
        thumbsTable.removeChild( thumbsTable.firstChild );
    
    tBody = document.createElement('tbody');
    
    lowerB = _pageNum * 20 - 20;
    upperB = (lowerB + 20) > _galleryImages.length ? _galleryImages.length : (lowerB + 20);
    
    for ( i = lowerB; i < upperB; i++)
    {
 
        if (i % picsPerRow == 0 )
        {
            row = document.createElement('tr');           
        }   

        //build the thumbnail image
        img = document.createElement('img');
        img.setAttribute( 'src', _galleryImages[i][0]);
        
        //build the anchor
        a = document.createElement('a');
        a.setAttribute ('href' , 'javascript:msngnShowPhoto(' + i + ');');
        a.appendChild (img);
        
        cell = document.createElement('td');
        cell.appendChild(a);
        
        row.appendChild(cell);
        
        if ( i % picsPerRow == 0 || i == _galleryImages.length-1)
        {
            tBody.appendChild( row );
        }        
    }
    
    thumbsTable.appendChild(tBody);
    //alert( thumbsTable.innerHTML);
    
    //set the paging 
    numLeft = _galleryImages.length - (_pageNum * 20) > 20 ? 20 : _galleryImages.length - (_pageNum * 20);
    
    pageNav = '';    
    if ( _galleryImages.length  > 1 )
    {
        if ( _pageNum > 1 )
        {
            pageNav += '<a href="javascript:msngnClick()" onclick="_pageNum=' + (_pageNum-1) + '; drawGallery();" >Previous 20 Photos</a> |';        
        }
        
        pageNav += '<span>' + (lowerB + 1) + ' -- ' + upperB + '</span> (of ' + _galleryImages.length  + ')';
        
        if ( numLeft > 0 )
        {
             pageNav += ' | <a href="javascript:msngnClick()" onclick="_pageNum=' + (_pageNum+1) + '; drawGallery();" >Next ' + numLeft + ' Photos</a>';
        }
    }         
    document.getElementById('pageNavContainer').innerHTML = pageNav;
    
    //display the first image as the default
    if ( _galleryImages.length > 0 )
        msngnShowPhoto(0);
            
    
}
addOnloadEvent(drawGallery);

function drawLHN()
{
    lhn = recurseLHN( LHN[0][1] );    
    lhn.setAttribute('class', 'navGallery');
    document.getElementById('navigation').appendChild(lhn);
}
//addOnloadEvent(drawLHN);


function recurseLHN( parentId )
{
    var ul = document.createElement('ul');
    var parentToFind =   parentId;
      
    var lhnIdx = 0;    
      
    while (lhnIdx < LHN.length)
    {
        var thisParentId = LHN[lhnIdx][0];
        var thisId  = LHN[lhnIdx][1]; 
        var thisLinkText = LHN[lhnIdx][2];
        var thisHref = LHN[lhnIdx][3];

        if ( thisParentId == parentToFind )
        {
            var a = document.createElement('a');
            a.innerHTML = thisLinkText;
            a.setAttribute('href', thisHref);
            
            var li = document.createElement('li');
            li.appendChild(a);
            
            //fetch all nodes who has this guy as parent
            var children = recurseLHN( thisId );
            
            if ( children.innerHTML.length > 0 )
            {
                node = document.createElement('ul');
                node.innerHTML = children.innerHTML;
                
                li.appendChild(node);
                li.setAttribute('class', 'active');
            }
            
            ul.appendChild(li);     
        }
        
        lhnIdx++;
    }  
        
    return ul;        
}

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}


function showEmailForm()
{
 if ($("#stage #emailFriendForm").length == 0) {
 
document.getElementById('galleryImagesContainer').style.display = 'none';
 
document.getElementById('introTextContainer').style.display = 'none';
 
document.getElementById('picNavContainer').style.display = 'none';
 
document.getElementById('pageNavContainer').style.display = 'none';
 
document.getElementById('captionDiv').style.display = 'none';
 

 
//move the emialForm to the stage
 
emailForm = document.getElementById('emailFriendForm');
 
emailForm.style.visibility = 'visible';
 
document.getElementById('stage').appendChild(emailForm);
 }
    
    //clear the stage
    //if ( stageId = document.getElementById('stage').hasChildNodes() )
    //{
    //    stageId = document.getElementById('stage').firstChild.id;
        
    //    if ( stageId && stageId != 'emailFriendForm' )
    //    {
    //        document.getElementById('stage').removeChild( document.getElementById('stage').firstChild );
    //        document.getElementById('galleryImagesContainer').style.display = 'none';
    //        document.getElementById('introTextContainer').style.display = 'none';
    //        document.getElementById('picNavContainer').style.display = 'none';
    //        document.getElementById('pageNavContainer').style.display = 'none';
    //        document.getElementById('captionDiv').style.display = 'none';
            
            //move the emialForm to the stage
     //       emailForm = document.getElementById('emailFriendForm');
     //       emailForm.style.visibility = 'visible';
     //       document.getElementById('stage').appendChild(emailForm);
     //   }
    //}
    //else
    //{
            //move the emialForm to the stage
     //       emailForm = document.getElementById('emailFriendForm');
     //       emailForm.style.visibility = 'visible';
     //       document.getElementById('stage').appendChild(emailForm);    
    //}
    
    
}

function msngnClick()
{
    //nada
}