var container = '#ph-photos .holder';
var sizeParent = 0;
var size = 0;

var imageCount = 0; 
var loadedCount = 0;
var lastLoadedCount = 0; 
var iter = 0; 

function getImgCount()
{
  
    $(container + ' img').each(function(){
        imageCount++;     
        this.onload = imgLoad;  
           
      //  alert($(this));
    }); 
    
  //  document.title = imageCount; 
    
}

function imgLoad()
{   
    loadedCount++;
}


function getImgSize()
{
    var s = 0; 
        
    $(container + ' img').each(function(){
        
        //if (!$(this).complete)
            //return 0;
        
           
        
        s += $(this).innerWidth() + 10;
    });
    
    return s;     
}

function imgInterval()
{
//document.title += ","; 

    if (((imageCount == loadedCount || lastLoadedCount == loadedCount) && loadedCount > 0)  || iter > 10 && loadedCount == 0)
    {

        //initScroll();        
       // return;
    
        var s = getImgSize();
        if (s != size || s < 300)
        {
            size = s;         
            setTimeout("imgInterval();", 500);
            
        }
        else
        {
        //    document.title += imageCount + "-" + loadedCount;             
            initScroll();        
           // setTimeout("imgInterval();", 1000);
        }
    
    }
    else
    {
        //document.title = "PH Model Management " + imageCount + "-" + loadedCount + "-" + lastLoadedCount + "-" + iter;            
        //initScroll();     
        setTimeout("imgInterval();", 100);
        
        lastLoadedCount = loadedCount; 
        iter++; 
        
        
        
    }
}


/*

function checkImgs()
{
    if ($.browser.webkit && document.readyState != "complete")
    {    
        setTimeout('checkImgs();', 100);
    }
    else
    {
        size = 0;
        
        $(container + ' img').each(function(){
            size += $(this).innerWidth() + 10;
        });
        initScroll();
    }
}

*/


function initScroll()
{

/*
    if (size < 500)
    {     
        setTimeout("checkImgs();", 2000);    
        return; 
    }    
*/
    $(container + ' div').css('width', size + 'px');
    $(container).css('width', sizeParent + 'px');
    $(container).jScrollHorizontalPane({scrollbarHeight:18});
    $(container).siblings('.jScrollPaneTrack').css('width', size + 'px');
    
  //  document.title += "."; 
}



$(document).ready(function(){

 sizeParent = $(container).parent().outerWidth();
 //window.setInterval("imgInterval();", 500); 
 getImgCount();
 setTimeout("imgInterval();", 100);
 
 //setTimeout("checkImgs();", 2000);
 //checkImgs();
    
});
