$(document).ready(function() {
  
  Cufon.replace('h1', { fontFamily: 'Gotham' });
  Cufon.replace('#services_bar ul li p, .slider_text p, .slider_text a', { fontFamily: 'DIN'});

  $("#close_warning").click(function() {
    $("#ie_warning").fadeOut();
  });
  $("#logo_light_home").delay(800).fadeIn(100).fadeOut(100).fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(200).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(500).delay(400).fadeIn(600);
  
  $("#logo").hover(
    function(){
      $("#logo_light, #logo_light_home").stop(true, true).fadeIn(100).fadeOut(100).fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(200).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(500).delay(300).fadeIn(600);},
    function(){
      $("#logo_light, #logo_light_home").fadeIn(100);
  });
  
  $("#social_nav").hover(
    function(){
      $(this).stop(true).animate({"right": "0"}, 300);},
    function(){
      $(this).animate({"right": "-94px"}, 200, "linear");
  });

  $("#home_slider").delay(600).animate({opacity: "1"}, 1000);
  
    $("#client_container").smoothDivScroll({ 
      autoScroll: "always", 
      autoScrollDirection: "endlessloopright", 
      autoScrollStep: 1, 
      autoScrollInterval: 15,
      visibleHotSpots: "always"
    });

  $("#client_container").bind("mouseover", function() {
	$(this).smoothDivScroll("stopAutoScroll");
  }).bind("mouseout", function() {
	$(this).smoothDivScroll("startAutoScroll");
  });

  
  if ($('.service_text').length) {
    $('.service_text').cycle({ 
        fx: 'custom', 
        sync: 0,
        cssBefore: {  
          left: -30,  
          opacity: 0,
          display: "block"
        },
        cssAfter: {  
          display: "none"
        },
        animOut: {  
          opacity: 0,
          left: 30
        },
        animIn: { 
          opacity: 1, 
          left: 0  
        },
        speedIn: 500,
        speedOut: 400,
        delay: 1000,
        timeout: 8000,
        next: "#home_slider_next",
        prev: "#home_slider_prev",
        pagerAnchorBuilder: function(idx, slide) { 
          return '#services_open li:eq(' + idx + ')'; 
        }
      });
  };

  if ($('.service_images').length) {
    $('.service_images').cycle({ 
        fx: 'custom', 
        sync: 0,
        cssBefore: {  
          left: -0,  
          opacity: 0,
          display: "block"
        },
        cssAfter: {  
          display: "none"
        },
        animOut: {  
          opacity: 0,
          left: 0
        },
        animIn: { 
          opacity: 1, 
          left: 0  
        },
        speedIn: 800,
        speedOut: 800,
        delay: 1000,
        timeout: 8000,
        next: "#home_slider_next",
        prev: "#home_slider_prev",
        pager: '#services_open',
        pagerAnchorBuilder: function(idx, slide) { 
          return '#services_open li:eq(' + idx + ')'; 
        },
        updateActivePagerLink: function(pager, activeIndex) {
          $(pager).find('li:eq(' + activeIndex + ')').addClass('active').siblings().removeClass('active'); 
        }
      });
  };
  
  if ($('#home_project_slider').length) {
    $('#home_project_slider').cycle({ 
        fx: 'fade',
        speed: 400,
        timeout: 0,
        pager: '#home_project_pager'
      });
  };
  
  if ($('#announcements_box')) {
    $('#announcements_box') 
      .cycle({ 
        fx: 'fade',
        speed: 400,
        timeout: 5000
      });
  };

  $("#client_prev").hover(
    function(){
      $(this).stop(true).animate({"left": "4px"}, 150);},
    function(){
      $(this).animate({"left": "10px"}, 150);
  });
  
  $("#client_next").hover(
    function(){
      $(this).stop(true).animate({"right": "4px"}, 150);},
    function(){
      $(this).animate({"right": "10px"}, 150);
  });
  
  $(".view_more_home").hover(
    function(){
      $(this).stop(true).animate({"padding-right": "25px", "margin-right": "-5px"}, 150);},
    function(){
      $(this).animate({"padding-right": "20px", "margin-right": "0px"}, 150);
  });
  
  $(".more_link").hover(
    function(){
      $(this).stop(true).animate({"padding-right": "25px"}, 150);},
    function(){
      $(this).animate({"padding-right": "20px"}, 150);
  });
  
  $("#home_slider_prev, #home_slider_next").hover(
    function(){
      $(this).stop(true).animate({"opacity": "1"}, 150);},
    function(){
      $(this).animate({"opacity": ".5"}, 150);
  });

  $(".service").hide(); //Hide all content
  $("#service_tabs a:first").addClass("active").show(); //Activate first tab
  $(".service:first").show(); //Show first tab content

  //On Click Event
  $("#service_tabs a").click(function() {
    $("#service_tabs a").removeClass("active");
    $(this).addClass("active");
    $(".service").hide(); //Hide all tab content

    var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
    $(activeTab).fadeIn(); //Fade in the active ID content
    return false;
  });
  
  $(".product").hover(
      function(){
        $(this).find("img").stop(true, true).animate({"opacity": "1"}, 300);},
      function(){
        $(this).find("img").animate({"opacity": ".5"}, 300);
    });

  
  // make links with class of "lightbox" use fancybox
  if ($('a.lightbox').length) {
    $('a.lightbox').fancybox({
      overlayColor    : '#000',
      overlayOpacity  : '.65',
      titlePosition   : 'inside',
      autoScale       : 'false',
      scrolling       : 'no'
    });
  };
  
  // fade flash messages out after 5 seconds
  if ($(".flash-message").length) {
    setTimeout(function() { $('.flash-message').fadeOut(); }, 5000);
  };
  
  // make all links with class of 'external' open in a new window
  $("a.external").click(function() {
    window.open(this.href);
    return false;
  });
  
  // position the project titles
  $('.project').each(function(index, div) {
    var title = $(div).find('.project-title');
    var h_move = (296 / 2) - (title.width() / 2);
    var v_move = (210 / 2) - (title.height() / 2);
    title.css({top: v_move + 'px', left: h_move + 'px'});
  });
  
  // show/hide project titles
  $('.project').hover(
    function() {
      $(this).find('.project-title').stop(true, true).fadeIn(300);
    },
    function() {
      $(this).find('.project-title').stop(true, true).fadeOut(200);
    }
  );
  
  // open project links in lightbox
  if ($('.project a').length) {
    $('.project a').fancybox({
      titleShow: false,
      width: 900,
      overlayColor  : '#000',
      overlayOpacity  : .65,
      autoScale : false,
      scrolling : 'no',
      onComplete: function() {
        // trigger image cycle
        $('.project-images').cycle({
          timeout: 0,
          pager: '.project-images-pager',
          speed: 400
        });
        $('.project-images').show();
        
        // find the tallest image and set the height of the container
        if ($('.project-images img').length) {
          var height = 0;
          $('.project-images img').each(function() {
            if ($(this).height() > height) {
              height = $(this).height();
            };
          });
          height += 70; // add a little to account for padding, pager div, etc.
          $('#fancybox-outer').css('min-height', height + 'px');
          
          // we should also move the fancybox up some
          if (height > $(window).height()) {
            $('#fancybox-wrap').css('top', 0 + 'px');
          };
        }
      }
    });
  };
  
  // show/hide functions for contact form
  if ($('.contact_type').length) {
    // show the div if there is already a value
    if ($("input[name=contact_type]").val() != undefined) {
      var div_type = $(this).val();
      $("#"+div_type+"-options").show();
    };
    
    // show/hide the correct div
    $("input[name=contact_type]").change(function() {
        var div_type = $(this).val();
        $(".choice_checkboxes").hide();
        $(".choice_checkboxes input:checkbox").attr('checked', false);
        $("#"+div_type+"-options").show();
    });
  };

  $('.launch-button, .contact-button, .contact-button-services').hover(
    function() {
      $(this).find('a').stop(true, true).animate({opacity: "1"}, 300);
    },
    function() {
      $(this).find('a').stop(true, true).animate({opacity: "0"}, 300);
    }
  );

  $('#btn_submit').hover(
    function() {
      $(this).find('#send_btn').stop(true, true).fadeIn(300).css({display: "block"});
    },
    function() {
      $(this).find('#send_btn').stop(true, true).fadeOut(200);
    }
  );
  
  // make text areas into wysiwyg editor
  if ($('textarea.tinymce').length) {
    $('textarea.tinymce').tinymce({
      // Location of TinyMCE script
      script_url : '/javascripts/tiny_mce/tiny_mce.js',

      // General options
      theme : "advanced",
      plugins : "emotions,preview,paste,media",

      // Theme options
      theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,image,media,|,removeformat,cut,copy,paste,pastetext,|,formatselect,fontselect,fontsizeselect,|,help,code",
      theme_advanced_buttons2 : "",
      theme_advanced_buttons3 : "",
      theme_advanced_toolbar_location : "top",
      theme_advanced_toolbar_align : "left",
      theme_advanced_blockformats : "p,div,h3,h4,blockquote",
      extended_valid_elements : "a[name|href|target|title|onclick],img[class|src| border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
      theme_advanced_statusbar_location : "bottom",
      theme_advanced_resizing : true
    });
  };
  
  // semi-hack to get sortable table elements to retain width
  if ($('.sortable td').length) {
    $('.sortable td').each(function() {
      var cur_width = $(this).width();
      $(this).css('width', cur_width + 'px');
    });
  };
});

// function for admin to show busy widget when deleting content
function show_busy_row(prefix, id) {
  if($("#" + prefix + "_link_"+id).length) { $("#" + prefix + "_link_"+id).hide(); }
  if($("#" + prefix + "_busy_"+id).length) { $("#" + prefix + "_busy_"+id).show(); }
}

// function for admin to show form div and hide link div
function show_form_div(prefix, id) {
  if($("#" + prefix + "_link").length) { $("#" + prefix + "_link").hide(); }
  if($("#" + prefix + "_form").length) { $("#" + prefix + "_form").show(); }
}

// function for admin to show link div and hide form div
function show_link_div(prefix, id) {
  if($("#" + prefix + "_form").length) { $("#" + prefix + "_form").hide(); }
  if($("#" + prefix + "_link").length) { $("#" + prefix + "_link").show(); }
}

function setupScroll() {
  $("#client_container").smoothDivScroll({ 
    autoScroll: "onstart", 
    autoScrollDirection: "endlessloopright", 
    autoScrollStep: 1, 
    autoScrollInterval: 15,
    visibleHotSpots: "always"
  });
}
