function addStyles(rule){
     var sheet = document.styleSheets[0],
          ln = (sheet.cssRules||sheet.rules).length;
     if (sheet.addRule) {
         rule = rule.match(/(.*){(.*)}/);
         sheet.addRule(rule[1],rule[2], ln);
     } else {
         sheet.insertRule(rule, ln);
     };
     return arguments.callee;
}; 

addStyles(".display_when_ready { display:none; }");



$(document).ready (function() {
  
  $('.display_when_ready').show();

  $('.rounded').corner();
  $('#globalnav').corner({
  			  tl: false,
  			  tr: false,
  			  bl: { radius: 10 },
  			  br: { radius: 10 },
  			  autoPad: true,
  			  antiAlias: true
  });
  
  $('.tablesorter').tablesorter();
  
  $(".tabs").tabs({
    collapsible: true
  });
  
  $('.tooltip').tooltip({ 
      track: false, 
      delay: 100, 
      showURL: false, 
      showBody: false, 
      fade: 250 
  });
  
  $(".truncate-125").truncate( 125, {
          chars: /\s/,
          trail: [ "<span class='small'> ( <a href='#' class='truncate_show'>more</a> . . . )</span>" ]
      });
  
  $('#signin_dialog').dialog({
    autoOpen: false,
    resizable: false,
    draggable: true,
    modal: true,
    width: 410
  });
  
/* remove for now
  $('#sign_in_link').click(function () { 
    $('#signin_dialog').dialog('open');
    return false;
  });
*/

  purchase_dialog_options = { 
      autoOpen: false,
      resizable: false,
      draggable: true,
      modal: true,
      position: 'top',
      width: 525
  };
  
  $("#proceeding_purchase_dialog").dialog(purchase_dialog_options);
  $("#single_session_purchase_dialog").dialog(purchase_dialog_options);
  $("#contact_sales_dialog").dialog(purchase_dialog_options);
	
  $("a.new_window").click(function(){
    window.open(this.href);
    return false;
  });

  $(".colorbox_terms").colorbox({
    opacity: 0.5,
    width:"600",  
    height:"500"
  });

  $(".colorbox_demo").colorbox({
    opacity: 0.5,
    width:"1038", 
    height:"700", 
    iframe:true
  });

  //video close stop audio for ie.  refreshes video, only way to do it i can find as you can't access iframe dom cross domain to call 
  //the stop/pause actions.  we just reload about:blank to kill the video vs simply hiding it.
	if($.browser.msie) {
      $('#cboxClose').click(function(){
        document.frames[$('#colorbox iframe').attr('name')].location = 'about:blank'; 
      });
      $('#cboxOverlay').click(function(){
        document.frames[$('#colorbox iframe').attr('name')].location = 'about:blank'; 
      });
  };

});

function show_individual_session_purchase_dialog(session_id) {
  $('#single_session_purchase_session_id').val(session_id);
  $('#single_session_purchase_dialog').dialog('open');
}

function show_contact_sales_dialog(inquiry_type,dialog_div,proceeding_id) {
  $('#contact_sales_dialog_div').html(dialog_div);
  $('#contact_sales_dialog_inquiry_type').val(inquiry_type);
  $('#contact_sales_dialog_proceeding_id').val(proceeding_id);
  $('#contact_sales_dialog').dialog('open');
}

function toggle_proceeding(proceeding_id) {
  $('#proceeding_' + proceeding_id).slideToggle(500);
  $('#proceeding_' + proceeding_id).prev('h3').children('a').toggleClass("session_spinner_closed");
  $('#proceeding_' + proceeding_id).prev('h3').children('a').toggleClass("session_spinner_open");
}

function check_email() {
  $('#no_firm').hide();
  email = $('#user_email').val();
  domain = email.split('@')[1];

  $.getJSON('/institutions?domain='+domain, function(institution) {
      if( institution != null ) {
        $('#firm_info').hide();
        $('#found_message').fadeIn();
        $('#required_form').fadeIn();
        input = '<input type="hidden" id="user_institution_id" name="user[institution_id]" value="'+institution.id+'"/>';
        $('#required_form').prepend(input);
        $('#submit_section').fadeIn();
        $('#footer').css('zoom','1');
        $('#user_submit').removeAttr("disabled");
      } else {
        $('#found_message').hide();
        $('#no_firm').fadeIn();
        $('#required_form').fadeIn();
        $('#firm_info').fadeIn();
        $('#submit_section').fadeIn();
        $('#footer').css('zoom','1');
        $('#user_submit').removeAttr("disabled");
      };
    });

}

function check_promo() {
  promo = $('#user_promocode').val();
  $.getJSON('/promo_codes?promo_code='+promo, function(promoJSON) {
    if( promoJSON != null ){
      $('#promo_check_td').html(promoJSON.description);
    } else {
      $('#promo_check_td').html('Invalid Promo Code.  <a id="check_promo" href="#" onClick="check_promo();">Recheck</a>');
    }
  });
}
