/*------------------------------------------------------
Site-wide JavaScripts 

version:  1.1
author:   joe ardeeser
email:    info@jordancrown.com
website:  http://www.jordancrown.com
------------------------------------------------------*/



/* Radios 
------------------------------------------------------*/
// Description: Set theObj radio group to the value of theObjValue
function setRadioValue( theObj, theObjValue ) {
    for( var i = 0; i < theObj.length; i++ ) {
        if( theObj[i].value == theObjValue ) {
      theObj[i].checked = true;
  }
    }
}

// Description: Check if any of the radio buttons are selected in a radio group
// 
// Returns: false, if no. The selected element's index
function radioButtonIsSelected( obj ) {
    var isSelected = false;

    for( counter = 0; counter < obj.length; counter++ ) {
  if( obj[counter].checked ) {
      isSelected = counter; 
  }
    }

    if( isSelected === false ) {
  return false;
    } else {
  return isSelected;
    }
}



/* Tabs
------------------------------------------------------*/
// Selects tabNum tab (assumes $tabs is present) 
function selectTab( tabsObject, tabNum ) {
    tabsObject.tabs('select', tabNum);
}



/* Thinkbox Windows
------------------------------------------------------*/
// Closes the open thick box window
function close_tb_window( delay ) {
    if( delay > 0 ) {
  setTimeout( "tb_remove()", delay );
    } else {
  tb_remove();
    }
}



/* Alerts
------------------------------------------------------*/
function closeAlert( handle ) {
  $(handle).parent().fadeOut('slow');
}




/*  Fade Out Div
------------------------------------------------------*/
function fadeOutDiv( div, ms ) {
  // If optional argument is null, create it
  if(!div) { return false; }
  if(!ms)  { var ms = 1000; }
  
  setTimeout(function(){ $(div).fadeOut('slow'); }, ms);
};




/* Widgets
------------------------------------------------------*/
// Show all bills that are hidden
function expandBills() {
    $('.bills_and_topics .hidden2').css('display', 'block');
    $('.bills_and_topics .expand').css('display', 'none' );
}

// Delete notification
function deleteNotification( handle, id ) {

    // AJAX: delete notification record
   
    $(handle).parent().fadeOut('slow');
}

// Delete bill
function deleteBillTracking( handle, id ) {

    // AJAX: delete bill tracking record 
   
    $(handle).parent().fadeOut('slow');
}

// Delete bill
function deleteKeywordTracking( handle, id ) {

    // AJAX: delete keyword tracking record
   
    $(handle).parent().fadeOut('slow');
}



/* Bubbling
------------------------------------------------------*/
function cancelBubbling(e)
{
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}



/* onload
------------------------------------------------------*/
$(document).ready( function() {

  // Add the additional structure to the box divs
  $("div.box").wrap(
    '<div class="box_outer">' + 
    '<div class="bd">' + 
    '<div class="c">' + 
    '<div class="s">' + 
    '</div>' + 
    '</div>' + 
    '</div>' + 
    '</div>' 
  );

  $('div.box_outer').prepend(
    '<div class="hd">' +
    '<div class="c"></div>' +
    '</div>'
  ).append(
    '<div class="ft">' +
    '<div class="c"></div>' +
    '</div>'
  );

  $('div.box_outer').each( function(i, o) {
    var classes = $(o).find('.box').attr('class');
    $(o).addClass(classes);

    // Remove the classes from the inner box
    $(o).find('.box:first').attr('class', '').addClass('getLayout');
  });

  // Prep the buttons
  init_buttons();
});


/* Misc 
------------------------------------------------------*/

// Set the select item in the menu
function highlightMenuOption( option ) {
  $('#item_handle_' + option).addClass('item_selected');
}


/* Buttons 
------------------------------------------------------*/
function init_buttons() {

  // Add the additional structure to the buttons 
  $('.button:not(:has("span"))').each( function( i, o ) {
      $(o).wrapInner( '<span class="wrap"><span></span></span>' );

      // Make the buttons go back to their normal state (IE)
      $(o).click( function() {
        $(this).blur();
      });
  });
}



/* Forms 
------------------------------------------------------*/
// Submit a form using a link
function submitForm( idButton ) {
  $('#' + idButton).click();
}

function showSuccessMsg( formId ) {
  $('#' + formId  + ' .alert_success' ).removeClass('hidden');
}

function showErrorMsg( formId, msg ) {
  $('#' + formId  + ' .alert_error' ).html(msg);
  $('#' + formId  + ' .alert_error' ).removeClass('hidden');
}

function showLoadingImage( formId ) {
  $('#' + formId  + ' .loading_img' ).removeClass('hidden');

  // Hide the success message
  $('#' + formId  + ' .alert_success' ).addClass('hidden');
}

function hideLoadingImage( formId ) {
  $('#' + formId  + ' .loading_img' ).addClass('hidden');
}


/* Global Variables
------------------------------------------------------*/

/* Fix for jQuery and Rails Authenticity Token
------------------------------------------------------*/
/*
$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});
*/
