// Common JavaScript code across your application goes here.

$(document).ready(function(){
  if($('#fading_logo').length) {
    el = $('#fading_logo');
    el.oneTime(3000, function(){
      $('#fading_logo').fadeOut(1000,function(){
        $('#always_logo').fadeIn(500);
      });
    });
  }

  if($('#thumbs .thumb img').length) {
    $('#thumbs .thumb img').mouseover(function(){
      src = $(this).attr('src');
      src = src.replace('thumb','medium');
      $('#photo img').attr('src',src);
    });
    $('#thumbs .thumb img').mouseout(function(){
      src = $(this).attr('src');
      src = src.replace(/_[0-6]/,'_1');
      src = src.replace('thumb','medium');
      $('#photo img').attr('src',src);
    });
  }

  
  if($('#project_slideshow').length) {
    image_srcs = []
    $('#project_images_for_slideshow a').each(function() {
      image_srcs.push({
        href: $(this).attr('href'),
        src: $(this).find('img').attr('src')
      });
    });

    $('#project_slideshow').crossSlide({
      sleep: 3,
      fade: 2
    }, image_srcs);
  }

  if($('#project_list').length) {
    pl = $('#project_list');
    pl.find('ul.hide').css('display','none');
    pl.find('h2 a').click(function(){
      el = $(this)
      list = el.parent().next('ul')
      sign = el.find('.sign');

      if(list.css('display')=='none')
        sign.html('-');
      else
        sign.html('>');
      list.slideToggle("slow");
      return false;
    });
  }
  
  if($('#green_features').length) {
    el = $('#green_features');
    text = el.find('p');
    sign = el.find('.sign');

    $("h1.green").click(function () {
      if(text.css('display')=='none') {
        sign.html('-');
        text.slideDown();
      } else {
        sign.html('>');
        text.slideUp();
      }
      return false;
    });

  }

  if($('#textile_preview').length) {
    local_mutex = false;
    source = $('#textile_source');
    preview = $('#textile_preview');
    previous_content = source.val();
    source.keyup(function() {
      content = source.val();
      if(previous_content != content) {
        update_textile_preview(source, preview);
        previous_content = content;
      }
    });
    update_textile_preview(source, preview);
  }


});

function update_textile_preview(source, preview) {
  $.post('/textile_preview',{
    textile: source.val()
  }, function(data){
    preview.html(data);
  });
}
