var fadeTime = 300;
var activeItem = 0;
var previousActiveItem = -1;
var clickedItem = 5;
var currentLightboxEl = null;
var animating = false;
var fadeInTime = 1500;
var interFadeTime = 500;
var currentLightboxTop = 0;
var isIPhone = false;

$(document).ready(function() {
      Cufon.replace('p');

      if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
         isIPhone = true;
      }

      if(!isIE6 && !isIPhone) {
         fadeIn(); 
      }

      if(!isIPhone) {
         initAnimations();
      }
         $('a[rel=lightbox]').colorbox( { opacity: 0 } ); 

      noCenterLightbox = false;
      if(isIPhone) {
         $('body').addClass('iphone');
         noCenterLightbox = true;
         //$('a[rel=lightbox]').attr('target', '_blank');
      }

      initMain();
      initMenu();
   });

function fadeIn() {
   $('#nav').css('opacity', '0');
   $('.section').css('opacity', '0');
   $('#footer').css('opacity', '0');
   fadeInEl($('#nav'));
   setTimeout("fadeInEl($('#portfolio'))", interFadeTime);
   setTimeout("fadeInEl($('.section'))", interFadeTime*2);
   setTimeout("fadeInEl($('#footer'));", interFadeTime*2);
}

function fadeInEl(el) {
   el.animate({opacity:1}, fadeInTime);
}

function lightboxClicked(direction) {
    var nextEl;
    switch(direction) {
    case 1:
        nextEl = $(currentLightboxEl).next('a');
        if(!nextEl.length) {
            nextEl = $('#portfolio a:first');
        }
        break;
    case -1:
        nextEl = $(currentLightboxEl).prev('a');
        if(!nextEl.length) {
            nextEl = $('#portfolio a:last');
        }
        break;
    }

    $(currentLightboxEl).animate({opacity: 0.40}, fadeTime);
    $(nextEl).animate({opacity: 1}, fadeTime);

    $('#portfolio a.current').removeClass('current');
    $(nextEl).addClass('current');

    if($(nextEl).offset().top != currentLightboxTop) {
        currentLightboxTop = $(nextEl).offset().top;
        //scrollWindowTo(currentLightboxTop - 18);
    }

    currentLightboxEl = nextEl;
}

function scrollWindowTo(pos) {
    //$('html, body').animate({scrollTop: pos}, 'fast', function() { window.scrollTo(0, pos); });
    window.scrollTo(0, pos);
}

function lightboxOpened(el) {
    $(el).addClass('current');
    $(el).css('opacity', '1');
    currentLightboxEl = el;
    $('#cboxPrevious').css('position', 'absolute');
}

function lightboxClosed() {
    $('#portfolio a.current').removeClass('current');
    $(currentLightboxEl).animate({opacity: 0.40}, fadeTime);
    currentLightboxTop = $(currentLightboxTop).offset().top;
}

function initMain() {
    updateActiveItem();
    if(isIE6) {
        setInterval(documentScrolled, 50);
    } else {
        $(window).scroll(documentScrolled);
    }
    $(window).resize(resize);
    setTimeout('resize()', 50);
    setTimeout('hackfix()', 5);

    if(!isIPhone) {
      $('.section .text').click(function() { 
            gotoSection($(this).parent().attr('id'));
         });
      $('.section .text').css('cursor', 'pointer');
   }
}

function hackfix() {
    documentScrolled();
    var navs = ($('#nav a'));
    gotoSection($(navs[activeItem]).attr('targetid'));
}


function resize() {
    addBottomPadding();
}

function addBottomPadding() {
    var sectionHeight = $('.section:last').height();
    var availableSpace = $(window).height() - 65;

    $('.section:last').css('margin-bottom', availableSpace - sectionHeight + "px");
}

function initMenu() {
    $('#menu li a').click(function() {
            var sectionId = $(this).attr('targetId');
            gotoSection(sectionId);
            //clickedItem = $(this).attr('count') - 1;
            //updateActiveItem();
            return false;
        });
}

function ieGetOffset(id) {
    var offset = 0;
    var seen = false;
    $('.slide').each(function() {
            if($(this).attr('id') == id) {
                seen = true;
            } else {
                if(!seen) {
                    //offset += 399;
                }
            }
        });
    return offset;
}

function gotoSection(id) {
        var sectionDiv = $('#'+id);
        var offset = sectionDiv.offset()
        //$(document).scrollTop(offset.top - topOffset);
        animating = true;
        $('html,body').animate({scrollTop:(offset.top - 18)}, 500, "linear", function() { animating = false; });
        $('.section.current').removeClass('current');
        sectionDiv.addClass('current');
}

function documentScrolled() {
    var count = -1;
    $('.section').each(function() {
            var offsetTop = $(this).offset().top;
            var scrollTop = $(document).scrollTop() + 19;
            if(scrollTop > offsetTop) {
                count++;
            }
        });
    if(count == -1) { count++; }
    activeItem = count;
    updateActiveItem();
}

function initAnimations() {
    $('div#portfolio a').hover(function() {
        if(!$(this).is('.current')) { 
            $(this).animate({opacity: 1}, fadeTime);
        }
    }, function() {
        if(!$(this).is('.current')) { 
            $(this).animate({opacity: 0.40}, fadeTime);
        }
    });

    $('div.text-section div.text').hover(function() {
        if(!$(this).parent().is('.current')) { 
            $(this).animate({opacity: 1}, fadeTime);
        }
    }, function() {
        if(!$(this).parent().is('.current')) { 
            $(this).animate({opacity: 0.5}, fadeTime);
        }
    });

    $('a[rel=lightbox]').click(function() {
        lightboxOpened(this);
    });
}

function updateActiveItem() { 
   if(isIPhone) {
      return false;
   }

    if(animating) {
        return false;
    }

    if(activeItem == previousActiveItem) {
        return false;
    } else {
        previousActiveItem = activeItem;
    }

    var count = 0;
    $('#menu li a').each(function() {
            if(count == activeItem) {
                $(this).addClass('active');
            } else {
                $(this).removeClass('active');
            }
            count++;
        });
    count = 0;
    $('div.section').each(function() {
          $(this).removeClass('current');
          if(count == activeItem) {
             $(this).addClass('current');
             $(this).children('div.text').animate({opacity: 1}, fadeTime);
          } else {
             $(this).children('div.text').animate({opacity: 0.5}, fadeTime);
          }
          count++;
       });
}

function openCopyright() {
    window.open('copyright.html', 'copyright', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=0, width=547, height=421');
}  


