/**
 * 
 */


//------------------------------------------------------
// Google Analystic Tracking Cookie - Asynchronous

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-10444487-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


//------------------------------------------------------ 

var application = {
  init : function() {  
	  initialize_superfish_menu();
  }
}

window.onload=function() 
{
	tinyMCE.execInstanceCommand("summary", "mceFocus");
}


function bind_login_function(el)
{
	$.ajax({
		  url: "/login/check",
		  success: function(data){
			  if ('' == data)
			  {
				  show_login($(el).attr('href'));
				  return false;			  
			  }
			  else
			  {		
				  window.location.href = $(el).attr('href');
				  return true;
			  }
		  }
		});
	return false;
}


/**
 * Initializing the superfish main menu.  Copied from Bill's code :)
 */
function initialize_superfish_menu()
{
	jQuery(function(){
		jQuery('ul.sf-menu').superfish({
		 	speed:100,
			delay:100
				});
			});	
}
/**
 * We're using jQuery UI's datepicker library.  Wherever you want to add a datepicker,
 * just create a text field, and set it's class to "date_picker"
 */
$(document).ready(function(){
	$( ".date_picker" ).datepicker({ dateFormat: 'yy-mm-dd' });	
	$(window).bind('resize.dialog', function(e) {
		$(".popup").dialog("option", "position", "center");
		});	
	setTimeout(hide_flash_message, 3000);
	
	  $('.check_login').bind('click', function(){
		  return bind_login_function($(this));
	  });	
	
});

/**
 * 
 * @param div_id - id of the div (pass any meaningful string)
 * @param height - height of the dialog box
 * @param width  - width of the dialog box
 * @param data   - the data inside the div (could be any HTML)
 * @param title  - title of the dialog box
 * @param modal  - boolean; if true, the box would be modal otherwise not
 */
function show_popup_jquery(div_id, height, width, data, title, modal, resizable)
{
	$("#" + div_id).dialog ("destroy");
	$("#" + div_id).remove();
	$('body').append("<div id='" + div_id + "' class='popup' title='" + title + "'>");
	
	$('#' + div_id).html(data);
	$('#' + div_id).dialog({
		height: height,
		width: width,
		modal: modal,
		resizable: resizable
	});	
}

function show_popup(div_id, height, width, data, title, modal, resizable)
{
	  var title_div = "<div class='modal-header'>" + 
	     "<a href='#' class='close'>&times;</a>" +  
	      "<h4>" + title + "</h4>" + 
	    "</div>";
		
	  var body_div = "<div class='modal-body'>" + data + "</div>";
	
	  $('body').append("<div id='" + div_id + "' class='modal hide fade'>" + title_div +  body_div + "</div>");
		
	  $('#' + div_id).modal({
		  show: true,
		  backdrop: "static"});
		
	  return;
}


function validation_object (id, error_message, tiny_mce)
{
	obj = new Object();
	this.id = id;
	this.error_message = error_message;
	this.tiny_mce = tiny_mce;
}

function hide_flash_message ()
{
	$("#flash_messages").fadeOut(4000);
}

function toggle_inactive_items(link)
{
	var show_inactive_items = $("#show_inactive_items").val();
	if (0 == show_inactive_items)
	{
		$("#show_inactive_items").val(1);
	}
	if (1 == show_inactive_items)
	{
		$("#show_inactive_items").val(0);
	}
	document.forms["search_form"].submit();
	tinyMCE.execInstanceCommand("mce_editor_0", "mceFocus");

}

function show_transactions ()
{
	tiny_mce_obj = tinymce.activeEditor;
	$.ajax({
		   type: "GET",
		   url: "/admin/transactions/bonds",
		   success: function(data){
			   show_popup_jquery('transactions', 225, 400, data, 'Select Bonds and Status', true, false);
			   $("#select_button").click(function(event){
				   event.preventDefault();
				   var transaction_id = $("#transaction_id").val();
				   var transaction_status = $("#bond_status").val();
				   tiny_mce_obj.selection.setContent("[" + "transactions|" + transaction_id + "|" + transaction_status + "]");
				   $("#transactions").dialog('destroy');
			   });
		   }		  
		});
}



function openReport(report_id) 
{
	$.ajax({
		   type: "GET",
		   url: "/ratings/validate?report_id=" + report_id + "&js=1",
		   success: function(data)
		   {
			   if (data == 'LOGIN')
			   {
				   show_login('/ratings/show_report/' + report_id);
			   }
			   else if (data == 'VIEW_REPORT')
			   {
				   window.location = '/show_report/' + report_id;
			   }
			   else if (data == 'INVALID_REPORT')
			   {
				   alert ("Invalid Report");
			   }
		   }
		});	
}	


function check_user_login(url)
{
	$.ajax({
	   type: "POST",
	   url: "/login/check",
	   success: function(data){
		  if (data == '')
		  {
			  show_login(url);			  
		  }	
		  else
		 {
			  window.location = url;
		 }
	   }		  
	});
}




