var viewTime = 8000;
  
var headerScrollTime = 6000;
  
var headerScrollerStarted = false;
var screenScrollerStarted = false;
  
var instruction_message = 'Enter your name, email, message &amp; we\'ll get back ASAP';
var error_message = '<strong>Oops!</strong> name, valid email and message are required';  
var confirm_message = '<strong>Thanks!</strong> We\'ll get back to you shortly';

var screenTimer = null;

var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
  
/*
$(window).load(
    if (window.focus) {
        startHeaderScroller();
    }
);
*/

$(document).ready(function() {

  //replace imgs for ie
	if (jQuery.browser.msie && (ie55 || ie6)) {
    $("#project-nav li").each(function() {
      $(this).find('a.img-link img').get(0).src = '/images/projects/'+this.id+'/icon.gif';
    });
  }
  
  $('#live_chat_link').click(function() {
    window.open($(this).get(0).href,'','width=600,height=550,left=0,top=0,resizable=yes,menubar=no,location=yes,status=yes,scrollbars=yes');
    return false;
  });
  
  // header links
  $('div#header ul li a').hover(function () {
    $(this).addClass("hover");
  },function(){
    $(this).removeClass("hover");
  });
  
  // contact form
  $("#contact_form_link").click(function() {
    if ($("#contact_form").get(0).offsetHeight == 425) {
      pageTracker._trackPageview("/user-event/contact-form-close");
      $("#contact_form div.address").hide();                              
      $("#contact_form form").hide();
      $("#contact_form").animate({ "height": 0 }, "slow", null, function() {if (screenTimer && screenScrollerStarted) {screenTimer.reset(viewTime);}}); 
      $(this).removeClass("selected");
    } else {
      pageTracker._trackPageview("/user-event/contact-form-open");
      if (screenTimer && screenScrollerStarted) {screenTimer.stop();}
      $("#contact_form").animate({ "height": 425 }, "slow", null, function() {
        $("#contact_form div.address").show();
        $("#contact_form form").show();
      }); 
      $(this).addClass("selected");
    }
    return false;
  });
  
  if (document.location.hash == '#contact') {
    $("#contact_form_link").click();
  }
  
  $("#cancel_contact_link").click(function() {
    pageTracker._trackPageview("/user-event/contact-form-close");
    $("#contact_form form").hide(); 
    $("#contact_form div.address").hide();
    $("#contact_form").animate({ "height": 0 }, "slow", null, function() {if (screenTimer && screenScrollerStarted) {screenTimer.reset(viewTime);}}); 
      $(this).removeClass("selected");
    return false;
  });
  
  $("#contact_form form input.submit").click(function() {
    pageTracker._trackPageview("/user-event/contact-form-submit");
    var has_errors = false;
    jQuery.each(['name', 'email', 'message'], function() {
      $("#contact_form form label#label_"+this).removeClass('error');
      if (jQuery.trim($("#contact_form form #"+this).val()) == "") {
        has_errors = true;
        $("#contact_form form label#label_"+this).addClass('error');  
      }
    });
    var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
    if (!re.test(jQuery.trim($("#contact_form form input#email").val()))) {
      has_errors = true;
      $("#contact_form form label#label_email").addClass('error');
    }
    if (has_errors) {
      $('#contact_error').get(0).innerHTML = error_message;
      return false;
    }
    
    var dataStringPieces = [];
    jQuery.each(['name', 'email', 'company', 'phone', 'message'], function() {
      dataStringPieces.push(this+'='+$("#contact_form form #"+this).val());
    });
    var dataString = dataStringPieces.join("&");
    //alert (dataString);return false;  
    $.ajax({
      type: "POST",  
      url: "/ajax/contact_form.php",  
      data: dataString,  
      success: function(result) {
        /*
        if (result == 'error') {
          alert('We could not send your information. Please try again later.');
          return false;
        }
        */
        pageTracker._trackPageview("/user-event/contact-form-success");
        $("#contact_form form label").removeClass('error');
        $('#contact_error').get(0).innerHTML = instruction_message;
        $("#contact_form form").hide();
        $("#contact_form div.address").hide();
        jQuery.each(['name', 'email', 'company', 'phone', 'message'], function() {
          $("#contact_form form #"+this).get(0).value = '';
        });
      
        $('#contact_form p#contact_confirm').get(0).innerHTML = confirm_message;
        $('#contact_form p#contact_confirm').show();
        setTimeout(function() {
          $('#contact_form p#contact_confirm').fadeOut("slow", function() {
            $("#contact_form").animate({ "height": 0 }, "slow", null, function() {if (screenTimer && screenScrollerStarted) {screenTimer.reset(viewTime);}});
          });
        }, 4000);
      },
      error: function() {
        alert('We could not send your information. Please try again later.');
      }
    });
    
    return true;
  });
  
  // Open web site links in new tab
  $("p.link a").click(function() {
    pageTracker._trackPageview("/user-event/project-click-"+this.id);
  }).attr({target: "_blank"});

  // Project navigator
  $("#project-nav").hover(function() {
    pageTracker._trackPageview("/user-event/project-nav-mouseover");
    //$(this).addClass("hover");
    $("#project-nav").stop();
    $("#project-nav").animate({ "width": 266 }, "fast", null, function() {$("#project-nav").addClass("hover");});
  },function(){
    pageTracker._trackPageview("/user-event/project-nav-mouseout");
    $("#project-nav").stop();
    $("#project-nav").animate({ "width": 70 }, "slow", null, function() {$("#project-nav").removeClass("hover");});
    if (screenTimer && screenScrollerStarted) {screenTimer.reset(viewTime);}
  });
  
  $("#project-nav li").hover(function() {
    $(this).addClass("hover");
  },function(){
    $(this).removeClass("hover");
  });
  
  var screens = [];
  
  //grab screens into array
  $("#project-nav li").each(function() {
    screens.push(this.id);
  });
  
  var currentScreen = screens[0];
  
  $("#project-nav li").click(function() {
    pageTracker._trackPageview("/user-event/project-nav-click-"+this.id);
    $("#project-nav li").removeClass("selected");
    $(this).addClass("selected");
    $("div#all-projects").scrollTo($('#'+this.id+'-block'), 800);
    currentScreen = this.id;
    return false;
  });
  
  //use a false time - timer will be reset by header scroller below
  screenTimer = $.timer(1000000, function (timer) {
    screenScrollerStarted = true;
    //if called from header scroller, interval may be (purposely) unset for the first iteration
    //user may also get the ability to alter the speed
    timer.reset(viewTime);
    var nextScreen = null;
    $("#main-block:not('.showing-drop-down') #project-nav:not('.hover') li").each(function() {
      if (currentScreen == this.id) {
        nextScreen = $(this).next().get(0);
        if (!nextScreen) {
          nextScreen = $("#project-nav li:first").get(0);
        }
        return false;
      }
    });
    if (nextScreen) {
      currentScreen = nextScreen.id;
      $("#project-nav li").removeClass("selected");
      $('#'+currentScreen).addClass("selected");
      //$("#project-nav").addClass("scrolling");
      $("#project-nav").animate({ "width": 266 }, "slow", null, function() {
          $("#project-nav").addClass("scrolling");
          $("div#all-projects").scrollTo($('#'+currentScreen+'-block'), 800, {onAfter: function() {
            $("#project-nav").animate({ "width": 70 }, "slow", null, function() {$("#project-nav").removeClass("scrolling");});
          }});
        });
    } else {
      timer.stop();
    }
  });
  screenTimer.stop();
  
  startHeaderScroller();
  
});

function startHeaderScroller() {
  if (headerScrollerStarted) {return;}
  headerScrollerStarted = true;
  
  var currentWant = $("#youwant span:first");
  var currentOffer = $("#weoffer span:last");

  $.timer(headerScrollTime, function (timer) {
    timer.stop();
    $("#youwant span").each(function() {
      if (currentWant.get(0) == this) {
        currentWant = $(this).next();
        //only run the animation once
        if (currentWant.get(0)) {
          $("#youwant").animate({"left": -(currentWant.get(0).offsetLeft)+"px"}, 1000);
        }
        /*
        //run the animation in a loop
        if (!currentWant.get(0)) {
          currentWant = $("#youwant span:first");
        }
        $("#youwant").animate({"left": -(currentWant.get(0).offsetLeft)+"px"}, 1000);
        */
        return false;
      }
    });
    $("#weoffer span").each(function() {
      if (currentOffer.get(0) == this) {
        currentOffer = $(this).prev();
        //only run the animation once
        if (currentOffer.get(0)) {
            $("#weoffer").animate({"left": -(currentOffer.get(0).offsetLeft)}, 1000, null, function() {timer.reset(headerScrollTime)});
        } else {
            //we're finished; start the screen scroller
            screenTimer.reset();
        }
        /*
        //run the animation in a loop
        if (!currentOffer.get(0)) {
          currentOffer = $("#weoffer span:last");
        }
        $("#weoffer").animate({"left": -(currentOffer.get(0).offsetLeft)}, 1000, null, function() {timer.reset(4000)});
        */
        return false;
      }
    });
    /*
    $("#weoffer span").each(function() {
      if (currentOffer == this) {
        currentOffer = $(this).next().get(0);
        if (!currentOffer) {
          currentOffer = $("#weoffer span:first").get(0);
        }
        $("#youwant").scrollTo(currentOffer, 400, {axis: 'x'});
        return false;
      }
    });
    */
  });
}

