var indexes=new Array(); //{element,heights,currentIndex}
var viewportHeight  = getViewportHeight(); 
var isReady=false;
var isIE= ($.browser.msie==true);
var offset=isIE?0:13;



    function getViewportHeight()
    {
      //alert("_--"+(window.outerHeight ? window.outerHeight : $(window).height()))
     return  window.innerHeight ? window.innerHeight -offset : $(window).height()-offset;
      //return $('body').height();
    }                 
                               
    function setHorizontalPos()
    {
      var elements=$('.project');
      
      for(var n=0;n<elements.length;n++)
      {
       $(elements[n]).css('left',860*n+50*n+'px');
      }
      
      $('#projectsHolder').css('width',(860*n)+'px')
    }
    
    function onResize()
    {
      viewportHeight = getViewportHeight();
  

        for(var n=0;n<indexes.length;n++)
        {
         var element=indexes[n].element;
         $(element).css('top',(viewportHeight-getOffset(n,indexes[n].currentIndex))+'px');
        }  

    }
    
    function getOffset(id,index)
    {
      var o=0;
      for(var n=0;n<=index;n++)
      {
        o+=indexes[id].heights[n];
      }
      return o;
    }
    
    function createBoxes()
    {
      for(var n=0;n<$('.project').length;n++)
      {
       // var name='project'+n;
       // var element=$('div[name="'+name+'"]');
        
        var element=$('#project'+n);
        
        if(element.children().length>1){
        $('body').append('<div id="lol'+n+'" class="nav"></div>')
        var box=$('#lol'+n);
       box.css('left',644+860*n+50*n-(isIE?59:43)+"px");
        }

        var obj=new Object();
        obj.element=element;
        obj.heights=new Array();
        obj.currentIndex=element.children().length-1;
        
        if(element.children().length>1){box.append('<ul id="">');}
        
        for(var m=0;m<element.children().length;m++)
        {
         obj.heights.push($(element.children()[m]).height());
         if(element.children().length>1){box.append('<li onclick="doScrollPlz('+n+','+m+')"><img src="img/square.gif" alt="'+m+'" border="0"/></li>');}
        }
        if(element.children().length>1){box.append('</ul>');}
        indexes.push(obj);
        
      }
    }

    
    function doScrollPlz(id,index)
    {
      var element=indexes[id].element;
      indexes[id].currentIndex=index;
      
      $(element).animate({ top : (viewportHeight-getOffset(id,indexes[id].currentIndex))+'px' }, 1500, 'easeInOutExpo');

    }
    
    function onLoad()
    {
      
      $('#site').css('display','block');
      //$('#loading').css('display','none');
      $('#loading').animate({opacity: '0'}, 1500, 'easeInOutExpo',function(){
        
        $('#loading').remove();
      });

       setHorizontalPos();
       createBoxes();
       onResize();
       
    }
    
$(document).ready(function(){

  if(!isReady)
  {
    isReady=true;

                  $(window).bind('resize',onResize);
                  $(window).bind('load',onLoad);
                }
                   });
