var timing = 600;
var scr_height = 1200;
var scr_width = 2000;
var step = 0;
var pos;
var left;
var limit = 8;
var anim = 0;
var content_position = 0;
var c_pos;
var c_height;
var c_width;
var content_top_margin = 60;
var content_anim = 0;
var content_highlighted = 0;
//var c_opacity_min0 = 0.21; //default at step 1
var c_opacity_min = 0.45; //at all other steps
var c_opacity_max = 0.90; //at maximized state
var slow_animation = 15000;
var slow_animation_delay = 4000;
var intro_cta_delay = 4000;
var intro_cta_fadein = 3000;

var enable_livecam = 0;

function scroll(direction)
{
    if(content_anim)
    {
      return false;
    }
    
    if(step>1 && step<limit-1)
    {
      //alert('removing effect fish #'+(step+1));
      $('#effect'+(step+1)).stop().fadeOut('normal',function(){$(this).remove()});

      /* //disabling as this caused some problems
      if(step==3)
      {
        //alert('removing also 3rd effect here');
        $('#effect'+(step+1)+'b').stop().fadeOut('normal',function(){
        $(this).remove()
        //alert('3rd effect removed');
        });
      }
      */
    }
    
    //alert(step);

    if(direction=='+1')
    {
        if(step==limit-1 || anim)
        {
          return false;
        }
        
        anim = 2;
        content(0);

        $('#bg').animate({bottom: '+='+scr_height}, timing, function() {
            //complete
            anim--;
        });
        $('#fishes').animate({left: '-='+scr_width}, timing, function() {
            //complete
            anim--;
            content(step+1);
        });
        
        if(step==0)
        {
          $('.nav.prev').fadeIn('slow');
          $('.flag').animate({right: '+=34'}, timing, function() {
            //complete
          });
          $('.nav.next').animate({right: '+=19', top: '+=29'}, timing, function() {
            //complete
          });
        }
        
        if(step==1)
        {
          $('.nav.top').fadeIn('slow');
          $('.flag').animate({right: '+=34'}, timing, function() {
            //complete
          });
        }

        if(step==limit-2)
        {
          $('.nav.next').fadeOut();
        }
  
        step++;
    }
    else if(direction=='-1')
    {
        if(step==0 || anim)
        {
          return false;
        }

        anim = 2;
        content(0);
        step--;

        if(step==0)
        {
          $('.nav.prev').fadeOut('slow');
          $('.flag').animate({right: '-=34'}, timing, function() {
            //complete
          });
          $('.nav.next').animate({right: '-=19', top: '-=29'}, timing, function() {
            //complete
          });
        }

        if(step==1)
        {
          $('.nav.top').fadeOut('slow');
          $('.flag').animate({right: '-=34'}, timing, function() {
            //complete
          });
        }

        if(step==limit-2)
        {
          $('.nav.next').fadeIn();
        }
  
        $('#bg').animate({bottom: '-='+scr_height}, timing, function() {
            //complete
            anim--;
        });

        pos = $('#fish0'+(step+1)).position();
        $('#fish0'+(step+1)).css('left',pos.left+2*scr_width);
        $('#fish0'+(step+3)).hide();
        $('#fishes').animate({left: '-='+(scr_width)}, timing, function() {
            //complete
            left = $('#fishes').position();
            $('#fishes').css('left',left.left + 2*scr_width);
            $('#fish0'+(step+1)).css('left',pos.left);
            $('#fish0'+(step+3)).show();
            anim--;
            content(step+1);
        });
    }
    else if(direction=='=0' && anim == 0)
    {
        step=0;
        content(0);
        $('.nav.next').fadeIn();
        $('.nav.top').fadeOut('slow',function() {
          $('.nav.prev').fadeOut('slow',function() {
            $('.nav.next').animate({right: '-=19', top: '-=29'}, timing, function() {
              //complete
            });
            $('.flag').animate({right: '-='+(2*34)}, timing, function() {
              //complete
            });
          });
        });

        anim = 2;

        $('#bg').animate({bottom: -6000}, 2*timing, function() {
            //complete
            $('#fishes').fadeIn('slow');
            anim--;
            content(1);
        });
        $('#fishes').fadeOut('slow', function() {
            //complete
            $('#fishes').css('left',0);
            anim--;
        });
    }
    return false;
}


$(document).ready(function() {
  //alert('ready');
  
  if($.browser.msie && $.browser.version=="6.0") alert("I'm the annoying IE6, and I do not support this jQuery code... static version will be ready soon");

  if($.browser.msie)
  {
    //alert('');
  }

  $('#fishes img.fish:eq('+(limit-1)+')').click(function() {
    scroll('=0');
  });
  
  $('#fishes img.fish:lt('+(limit-1)+')').click(function() {
    scroll('+1');
  });

  $('.flag').click(function() {
    var href = $(this).attr('href');
    $('body').fadeOut('fast', function() {
      window.location = href;  
    });        
    return false;
  });

  $('.flag').fadeIn();
  $('.nav.next').fadeIn();
  $('#fishes').fadeIn('fast',function(){  
    resize_content();
    content(1);
  });
  
  $(window).resize(function() {
    resize_content();
  });

  //$('#content').bind('mouseover',function(){
  //  expand_content();
  //});
  
  /*
  $('#content').bind('mouseout',function(){
    if(content_anim || !content_position || anim)
    {
      return false;
    }
    content_anim = 2;
    //var c_pos2 = $('#content').position();
    //var c_height2 = $('#content').height();
    $('#content').animate({height: c_height, width: c_width}, timing, function() {
      //complete
      content_anim--;
      $('#content').removeClass('border');
    });
    var op2 = c_opacity_min;
    if(step==0)
    {
      op2 = c_opacity_min0;
    }
    $('#content_bg').animate({height: c_height, width: c_width, opacity: op2}, timing, function() {
      //complete
      content_anim--;
    });
    content_position=0;
  });
  */

  //$.fx.off = false;
  
});


function expand_content()
{
  if(content_anim || content_position || anim || !step)
  {
    return false;
  }
  
  //alert($('#content').height());
  //alert($('#content_inner').height());

  //alert($('#content_inner').height());
  //alert($('#content').height());
  
  if($('#content_inner').height() > $('#content').height() + 30)
  {
    content_anim = 2;
    c_pos = $('#content').position();
    c_height = $('#content').height();
    c_width = $('#content').width();
    $('#content').animate({height: '+='+(c_pos.top-content_top_margin), width: $('#container').width() - 80}, timing, function() {
      //complete
      content_anim--;
    });
    $('#content_bg').animate({height: '+='+(c_pos.top-content_top_margin), width: $('#container').width() - 80, opacity: c_opacity_max}, timing, function() {
      //complete
      content_anim--;
      //$('#controls').fadeIn();
      $('#nav').fadeOut();
      $('#close').fadeIn();
      //$('#close').fadeIn('normal',function(){
        //alert(1);
        //$('#close').css('zIndex',1234);
      //});
    });
  }
  else
  {
    $('#content_bg').animate({opacity: c_opacity_max}, timing, function() {
      //complete
      $('#nav').fadeOut();
      $('#close').fadeIn();
    });
  }
  
  $('#content_mask').fadeIn();
  $('#content_mask').bind('click',function(){close_content();});  

  $('#content').addClass('border');
  $('#content a[rel=lightbox]').lightBox();

  content_position = 1;
}


function highlight_content(highlight)
{
  if(content_anim || content_position || anim || !step)
  {
    //alert('do nothing 1');
    return false;
  }
  
  if(content_highlighted && highlight==1)
  {
    //alert('do nothing 2');
    return false;
  }
  
  if(!content_highlighted && highlight==0)
  {
    //alert('do nothing 3');
    return false;
  }
  
  /*
  if($('#content_inner').height() > $('#content').height() + 30)
  {
    content_anim = 2;
    c_pos = $('#content').position();
    c_height = $('#content').height();
    c_width = $('#content').width();
    $('#content').animate({height: '+='+(c_pos.top-content_top_margin), width: $('#container').width() - 80}, timing, function() {
      //complete
      content_anim--;
    });
    $('#content_bg').animate({height: '+='+(c_pos.top-content_top_margin), width: $('#container').width() - 80, opacity: c_opacity_max}, timing, function() {
      //complete
      content_anim--;
      //$('#controls').fadeIn();
      $('#nav').fadeOut();
      $('#close').fadeIn();
      //$('#close').fadeIn('normal',function(){
        //alert(1);
        //$('#close').css('zIndex',1234);
      //});
    });
  }
  else
  {
    $('#content_bg').animate({opacity: c_opacity_max}, timing, function() {
      //complete
      $('#nav').fadeOut();
      $('#close').fadeIn();
    });
  }
  */
  
  //$('#content_mask').fadeIn();
  //$('#content_mask').bind('click',function(){close_content();});  

  if(highlight==1)
  {
    //alert('highlight -> 1');

    $('#content').addClass('border');
    $('#content a[rel=lightbox]').lightBox();
  
    //$('#content').append('<div class="expand">')
    $('#content').append('<a class="expand" href="#" title="expand">')
    
    if($.browser.opera)
    {
      $('#content .expand').css('right',25);
    }
    //$('#content .expand').click(function() {

    $('#content').click(function() {
      $('#content .expand').fadeOut();
      expand_content();
    });

    //content_position = 1;
    content_highlighted = 1;
  }
  else
  {
    //alert('highlight -> 0');

    $('#content').removeClass('border');
    //$('#content a[rel=lightbox]').lightBox();
  
    $('#content').unbind('click');
    $('#content .expand').remove();

    content_highlighted = 0;
  }
}


function close_content()
{
    if(content_anim || !content_position || anim)
    {
      return false;
    }

    //$('#controls').fadeOut('normal',function(){

     $('#close').blur();
     $('#close').fadeOut('normal',function(){

      //$('#nav').fadeIn();
      content_anim = 2;
      //var c_pos2 = $('#content').position();
      //var c_height2 = $('#content').height();

      /*
      $('#content').animate({height: c_height, width: c_width}, timing, function() {
        //complete
        content_anim--;
        $('#content').removeClass('border');
      });
      var op2 = c_opacity_min;
      if(step==0)
      {
        op2 = c_opacity_min0;
      }
      $('#content_bg').animate({height: c_height, width: c_width, opacity: op2}, timing, function() {
        //complete
        content_anim--;
      });
      */
      $('#content_bg').fadeOut('fast',function(){
      
        content_anim--;
        $('#content').fadeOut('normal',function(){
      
          content_anim--;
  
          $('#content').css('visibility','hidden'); //must hide it and then set display to block otherwise scrollTop has no effect
          $('#content').show();
          //alert($('#content').scrollTop());
          $('#content').scrollTop(0);

          $('#nav').fadeIn();
          $('#content_mask').fadeOut();  
          $('#content_mask').unbind('click');  
        });
      });

      content_position=0;
    });
}


var intro_pos = false;

function resize_content()
{
  var h1 = $('#fishes img.fish:eq('+step+')').outerHeight();
      h1 += $('#fishes img.fish:eq('+step+')').position().top;
      h1 += 60;
      
  //alert(h1);
  
  //if(h1==60) //only a hack if image loads slowly
  if(h1<=85) //only a hack if image loads slowly
  {
    h1 = 269; 
  }
      
  if(step==0)
  {
    h1 += 30;
  }
  if(step==5)
  {
    h1 -= 40;
  }
  if(step==6)
  {
    //h1 -= 117;
    h1 -= 60;
  }
  if(step==7)
  {
    h1 -= 50;
  }

  var h2 = $('#container').height();
      h2 = h2 - h1;
      
  //alert('#intro0'+(step+1));
  //var w1 = $('#intro0'+(step+1)).position();
  //alert(w1.left);

  $('#content').css('height',h2+2);
  $('#content_bg').css('height',h2);
  
  if(intro_pos==false)
  {
    if($('#intro0'+(step+1)).css('display') == 'none')
    {
      $('#intro0'+(step+1)).css('visibility','hidden');
      $('#intro0'+(step+1)).css('display','block');
    }
    intro_pos = $('#intro0'+(step+1)).position();
    //intro_pos.left -= (step * scr_width);
    if($('#intro0'+(step+1)).css('visibility') == 'hidden')
    {
      $('#intro0'+(step+1)).css('display','none');
      $('#intro0'+(step+1)).css('visibility','visible');
    }
  }
  
  //alert(intro_pos.left);
  $('#intro0'+(step+1)).css('width',$('#container').width() - intro_pos.left + (step * scr_width) - 30);
  //alert($('#container').width() - intro_pos.left + (step * scr_width) - 40);

  var intro_h = $('#intro0'+(step+1)).height();
  
  //alert(intro_h);
  
  if(intro_h < h1 - 20)
  {
    $('#content').css('width',$('#container').width() - 80);
    $('#content_bg').css('width',$('#container').width() - 80);
  }
  else
  {
    $('#content').css('width',intro_pos.left - 60 - (step * scr_width));
    $('#content_bg').css('width',intro_pos.left - 60 - (step * scr_width));
  }
  
  //alert('resized');
}


function content(step)
{
  if(step==0)
  {
    $('#content_bg').css('opacity',c_opacity_min);
    $('#content_bg').fadeOut('fast');
    $('#content').fadeOut('fast');
    intro_pos = false;
    content_position = 0;
    
    $('#container .cta').stop().stop().fadeOut('fast',function(){$(this).remove()});
  }
  else
  {
    $('#content').empty().remove();
    $('#content_bg').after('<div id="content">');

    /*
    $('#content').bind('mouseover',function(){
      expand_content();
    });
    */

    content_highlighted = 0;

    $('#content').bind('mouseover',function(){
      //alert('content mouseover');
      highlight_content(1);
    });
    
    $('#content').bind('mousemove',function(){
      //alert('content mousemove');
      highlight_content(1);
    });

    /*
    $('#content').bind('mouseout',function(){
      //alert('content mouseout');
      highlight_content(0);
    });
    */

    $('.intro').hide();
    //if(step==1)
    //{
      //$('#content_bg').css('opacity',c_opacity_min0);
    //}
    anim++;
    $('#content').attr('class', 'step'+step);
    $('#content_bg').attr('class', 'step'+step);
    $('#content_bg').fadeIn('normal',function(){$('#content').fadeIn('normal',function(){
      //alert($('#content').height());
      //alert($('#content_inner').height());
      //alert('content fadein ended');
      
      if(step==1)
      {
        show_intro_cta();
      }

      anim--;
    });});
    
    //$('#content').html($('#sc_0'+step).html());
    $('#content').html('<div id="content_inner">'+$('#sc_0'+step).html()+'</div>');
    //$('#content a').lightBox(); // Select all links in object with gallery ID
    //$('#content a[@rel*=lightbox12412]').lightBox(); // Select all links in object with gallery ID
    //lightbox[1_2005-2006]

    if(step==1)
    {
        $('#content_inner').click(function() {
            scroll('+1');
        });
    }

    resize_content();
    
    if(step==4)
    {
      //alert('step is grunge');
      $('#intro04 h2').html('Grunge');
    }

    $('#intro0'+step).fadeIn();
  }
  if(step==limit)
  {
    //alert('last');
    $('#water_bg').fadeIn();
  }
  else
  {
    $('#water_bg').fadeOut('fast');
  }
  
  if(step>2 && step<limit)
  {
    //alert('loading effect fish');
    
    if($('#effect'+step).length)
    {
      //alert('effect fish #'+step+' already exists')
    }
    else
    {
      //alert('effect fish #'+step+' does not exist, creating')
      $('#fishes').append('<img id="effect'+step+'">');
      //alert($('#fish0'+step).attr('src'));
      //var s1 = $('#fish0'+step).attr('src');
      //alert(s1);
      //var s2 = s1.replace(/\./g, "2.");
      //alert(s2);
      
      /* //disabling as this caused some problems
      if(step==4)
      {
        //alert('grunge has 3rd effect');
        $('#effect'+step).hide().attr('src',$('#fish0'+step).attr('src').replace(/\./g, "2.")).css('position','absolute').css('left',$('#fish0'+step).position().left).css('top',$('#fish0'+step).position().top).delay(slow_animation_delay).fadeIn(slow_animation,function(){
          $('#fishes').append('<img id="effect'+step+'b">');
          $('#effect'+step+'b').hide().attr('src',$('#fish0'+step).attr('src').replace(/\./g, "3.")).css('position','absolute').css('left',$('#fish0'+step).position().left).css('top',$('#fish0'+step).position().top);
          $('#effect'+step).fadeOut(slow_animation);
          $('#effect'+step+'b').fadeIn(slow_animation,function(){
            //alert('3rd effect animated');
            $('#intro0'+step+' h2').html('Zombie');
          });
          $('#effect'+step+'b').click(function() {
            scroll('+1');
          });
        });
      }
      else
      {
      */
        $('#effect'+step).hide().attr('src',$('#fish0'+step).attr('src').replace(/\./g, "2.")).css('position','absolute').css('left',$('#fish0'+step).position().left).css('top',$('#fish0'+step).position().top).delay(slow_animation_delay).fadeIn(slow_animation,function(){
          //the slow animation of the fish has ended
        });
      //}
      
      $('#effect'+step).click(function() {
        scroll('+1');
      });
  
    }
  }
}


function show_intro_cta()
{
  //alert($('#container').width());
  //alert($('#container').height());

  //var ci_pos = $('#content_inner').position();
  //alert(ci_pos.left);
  //alert(ci_pos.top);
  //alert($('#content.inner').position().top);
  
  //alert($('#content_inner').width());
  //alert($('#content_inner').height());
  //alert($('.intro').width());
  //alert($('.intro').height());
  
  var h_taken = $('#content').position().top + $('#content_inner').height();
  var h_space = $('#container').height() - h_taken;
  
  //alert(h_space);
  var cta_pos_x,cta_pos_y;  

  if(h_space > 200)
  {
    cta_pos_x = $('.intro').position().left;
    cta_pos_y = h_taken + 50
  }
  else
  {
    cta_pos_x = $('#content_inner').position().left + $('#content_inner').width() + 30;
    //cta_pos_y = $('.intro').height() + $('.intro').position().top;
    cta_pos_y = $('#content').position().top + 70;
  
    //alert(cta_pos_x + '/' + cta_pos_y);  
  }
  
  //alert($('.cta').width());
  
  //alert(h_taken);
  $('#container').append('<a class="cta" href="#" onclick="scroll(\'+1\');return false;">');
  $('.cta').css('left',cta_pos_x);
  $('.cta').css('top',cta_pos_y);
  
  $('.cta').delay(intro_cta_delay).fadeIn(intro_cta_fadein);

  /*if($('#content_inner').width() + $('.cta').width() > $('#container').width())
  {
    alert('bad');
  }
  */
}


var contact_form_open = 0;
var contact_form_anim = 0;

function contact_form()
{
  $('#contact').blur();

  if(contact_form_anim)
  {
    return false;
  }
  
  var left_pos = $('#container').width() / 2;
  var right_pos = $('#container').width() - left_pos - 30; 
  
  contact_form_anim = 3;

  if(contact_form_open)
  {
    contact_form_open = 0;
    
    $('#contact_form').animate({left: $('#container').width()}, timing, function() {
      //complete
      contact_form_anim--;
      $('#contact_form').hide();
    });
  
    $('#contact_form_bg').animate({left: $('#container').width()}, timing, function() {
      //complete
      contact_form_anim--;
      $('#contact_form_bg').hide();
    });

    $('#contact').animate({right: 10}, timing, function() {
      //complete
      contact_form_anim--;
      $('#contact_form_mask').fadeOut();  
      $('#livecam').empty();
    });

    return false;
  }
  
  contact_form_open = 1;

  //alert(left_pos);
  
  //$('#container').width()
  $('#contact').animate({right: right_pos + 40}, timing, function() {
    //complete
    //alert(1);
    //$('#contact_form').fadeIn();
    //$('#contact_form_bg').fadeIn();
    contact_form_anim--;

    $('#contact_form_mask').fadeIn();  

    if(enable_livecam)
    {
      $('#livecam').append('<img src="images/ajax-loader2.gif" class="loader">');
      $('#livecam').after('<div id="preload" style="display:none;"><img src="gfx/livecam.gif"></div>');
      window.setTimeout("$('#livecam img').hide().removeClass('loader').addClass('cam').attr('src','gfx/livecam.gif').css('opacity',daylight).show();$('#preload').remove();",lag);
    }
  });
    
    $('#contact_form').css('right','');
    $('#contact_form').css('left',$('#container').width());
    $('#contact_form').css('width',$('#container').width() - (left_pos));
    $('#contact_form').show();

    $('#contact_form_bg').css('right','');
    $('#contact_form_bg').css('left',$('#container').width());
    $('#contact_form_bg').css('width',$('#container').width() - (left_pos));
    $('#contact_form_bg').show();

  $('#contact_form').animate({left: left_pos}, timing, function() {
    //complete
    //alert(1);
    //$('#contact_form').fadeIn();
    //$('#contact_form_bg').fadeIn();
    contact_form_anim--;
  });

  $('#contact_form_bg').animate({left: left_pos}, timing, function() {
    //complete
    //alert(1);
    //$('#contact_form').fadeIn();
    //$('#contact_form_bg').fadeIn();
    contact_form_anim--;
  });

  //$('#container').append('<div id="contact_form">');
}

function cf_show(link)  //warning: this fn exists also in latifoilo_debug.php
{
  //$(link).blur().after($(link).text()+':').remove();
  $(link).after($(link).text()+':').remove();
  $('#cf').slideDown('normal',function(){
    //alert(2);
    //$('#cf').attr('onsubmit','alert(1);return false;');
    //$('#cf input[type=submit]').attr('onclick','alert(1);return false;');
  });
}

