// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//global for rotator position
var c = 0;

function init_fancybox()
{
  if ($("#flickr_badge_uber_wrapper")){
    var total = $("#flickr_badge_uber_wrapper .flickr_badge_image").children("a").size();

    for (var i = 1; i <= total; i++){
      $("#flickr_badge_image" + i + " a").attr("href", $("#flickr_badge_image" + i + " img").attr("src").split("_s").slice(0,1)+".jpg");
    }
    $("#flickr_badge_uber_wrapper .flickr_badge_image a").fancybox();
  }
  jQuery(".fancybox").fancybox({
    'frameWidth':800,
    'hideOnContentClick': false
  });

}

function rotate_banner() {
  try {
    $("#rotator .b" + c).fadeOut(500, function() {
      c++;
      $("#rotator .b" + c).fadeIn(1000);
    });

    if (c == 1) c = 0;

    tout = setTimeout('rotate_banner()', 5000);
  } catch(e) {}
}

function load_pagination_links() {
  init_fancybox();

  jQuery("#news_list_pages a").click(function(){
    jQuery("#sub_news_list").load(this.href, function() {
      load_pagination_links();
    });
    return false;
  });

  jQuery("#research_list .pagination a").click(function(){
    jQuery("#research_list").load(this.href, function() {
      load_pagination_links();
    });
    return false;
  });

  jQuery("#article_list_pages a").click(function(){
    jQuery("#sub_oped_list").load(this.href, function() {
      load_pagination_links();
    });
    return false;
  });

  jQuery("#past_events_all .pagination a").click(function(){
    jQuery("#events_wrapper").load(this.href, function() {
      load_pagination_links();
    });
    return false;
  });

  jQuery("#past_videos .pagination a").click(function(){
    jQuery("#past_videos").load(this.href, function() {
      load_pagination_links();
    });
    return false;
  });

  /*jQuery("#archive_link").click(function(){
    jQuery("#events_wrapper").load(this.href, function() {
      load_pagination_links();
    });
    return false;alert($("#flickr_badge_image" + i + " img").attr("src").split("_s").slice(0,1));
    for (var i = 1; i <= total; i++){
      //$("#flickr_badge_image" + i + " a").attr("href", "g");
    }
  });*/

}

function speaker_series_ajax(url){
  jQuery("#events_wrapper").load(url, init_fancybox);
  return false;
}

function speaker_series_ajax_podcast(url){
  jQuery("#podcasts").load(url, init_fancybox);
  return false;
}

function research_sort_ajax(url){
  jQuery("#research_list").load(url, load_pagination_links);
  return false;
}

jQuery(document).ready(function() {

  //rotate_banner();

  jQuery(".fancybox_in").click(function(){
    alert("f");
    jQuery("#fancy_ajax").load(this.href);
    return false;
  });

  init_fancybox();
  load_pagination_links();

  jQuery('input.clearme').focus(function(i) {
    if (this.value == this.title) {
      this.value = "";
      jQuery(this).removeClass('clearme');
    }
  });

  jQuery('input.clearme').blur(function(i) {
    if (this.value == "") {
      this.value = this.title;
      jQuery(this).addClass('clearme')
    }
  });

  jQuery('#signup_form').submit(function() {
    jQuery.post('/emails/create', jQuery(this).serialize(), function(data) {

      if (data.status == "success") {
        jQuery('#signup_form_wrap').html(data.message);
        jQuery('#signup_form_wrap').addClass('json_message');
      }
      else jQuery('#signup_form_fail').html(data.message);
    }, "json");
    return false;
  });

  $('.flash').hide().slideDown().animate({opacity: 1.0}, 6000).slideUp();

  if (jQuery('#events_side')) initMenu();

  jQuery('#speaker_series_sort_events').change(function(){
    if(this.value != '-1') speaker_series_ajax("/events/speaker_series/"+this.value);
    else document.location = '/events';
  });

  jQuery('#speaker_series_sort_channels').change(function(){
    if(this.value != '-1') speaker_series_ajax_podcast("/channels/speaker_series/"+this.value);
    else document.location = '/channel';
  });

  jQuery('#paper_category').change(function(){
    if(this.value != '-1') research_sort_ajax("/papers/sort_by/"+this.value);
    else document.location = '/research';
  });

});

function initMenu() {

  $('#events_side .speaker_series p').hide();
  //$('#menu div:first').show();
  $('#events_side .speaker_series h4').click(
    function() {
      var checkForA = this.childNodes[0].parentNode.getElementsByTagName('a')[0];
      /*if(checkForA)
      {
        //document.location = checkForA + "#" + this.childNodes[0].parentNode.getElementsByTagName('a')[0].name;
        //return false;
        var stage = $("#stage");
        stage.location.href = checkForA;
        //alert(stage.src);
      }*/


      var checkElement = $(this).next();

      if((checkElement.is('p')) && (checkElement.is(':visible'))) {
        checkElement.slideUp('normal');
        return false;
        }
      if((checkElement.is('p')) && (!checkElement.is(':visible'))) {
        $('#events_side .speaker_series p:visible').slideUp('normal');
        checkElement.slideDown('normal');
        speaker_series_ajax(checkForA.href);
        return false;
        }
      }
    );

  /*if (window.location.hash)
  {
    var id = window.location.hash + "1";
    window.location.hash = "";
    $(id).slideDown('normal');
  }*/
}

