// jquery extensions
(function($) {
	$.fn.orderedListCode = function(idFilter) {
		var code = "";
		var id = "";
		idFilter = (idFilter != undefined && idFilter != null) ? idFilter : "";
		$this = $(this);
		$this.find("li").each(function(i) {
			id = $(this).attr("id").replace(idFilter, "");
			code += id + ":" + i + "|";
		});
		return code.substring(0, code.length-1);
	};
	
	/**
	 * returns a code representation of a tree from the element.
	 * The code is in this format {parentId}:{id}:{order}|{parentId}:{id}:{order}
	 */
	$.fn.treeCode = function() {
		var level = 0;
		var code = "";
		$(this).find("li").each(function(i) {
			var parentId = $(this).parent("ul").parent("li").attr("id");
			//var order = (($(this).prevAll("li").size()+1))/2;
			var order = $(this).prevAll("li").size();
			if (parentId == undefined) {
				parentId = 0;
			}
			code += parentId + ":" + $(this).attr("id") + ":" + order + "|";
		});
		return code.substring(0, code.length-1);
	};
})(jQuery);
var globalFns = {};
globalFns.checkLoggedIn = function() {
	$.post("/is_logged_in.json", {}, function(data){
		if (!data.isLoggedIn) {
			location.reload();
		}
	},
	"json");
};
var logger = {
	isDebbugerEnabled: true
};
logger.debug = function() {
	if (window.console != undefined && logger.isDebbugerEnabled) {
//		window.console.log.apply(this, arguments);
		try{
			console.log.apply(this, arguments);
		} catch(e) { 
			console.log(arguments); 
		}
	}
};
$(document).ready(function() {
	// admin
	$('ul.toggler a[href*="hide"]').click(function() {
		$(this).hide();
		$('ul.toggler a[href*="show"]').fadeIn('fast');
		$('tr.disabled').hide();
	 });
	$('ul.toggler a[href*="show"]').click(function() {
		$(this).hide();
		$('ul.toggler a[href*="hide"]').fadeIn('fast');
		$('tr.disabled').show();
	 });
	$(".modal").not(".proof_points").draggable();
	//$(".toc").draggable();
	// tabs
	$("#manual").tabs();
	// library lists
	$("#accordion").accordion({
//        active:true,
        collapsible:true, 
        autoHeight:false
      }).disableSelection();
    // error bubbles
    /*$('span.error').click(function() {
      $(this).hide("fast");
      return false;
    });*/
    // close parent window
    $('a.close').live("click", function() {
    	$(this).parent().hide("highlight",{},250);
    //	$("a.presentation:hidden").show(); // ie6 kludge
    	return false;
    });
    // close parent window
    /*$("#load_dialog a[href*='presentation_builder']").click(function() {
    	$(this).parents("div.modal").hide("highlight",{},250);
    	return false;
    });*/
    // close if outside of modal is clicked
    $(document).click(function(e) {
    	if (!$(e.target).parents().filter('.modal').length) {
    		$("div.modal:visible").hide("highlight",{},250);
    	//	$("a.presentation:hidden").show(); // ie6 kludge
		}
    });
    // show copaxone TOC
    $('a.copaxone').click(function() {
      //$('#copaxone').toggle('fast');
      $('#copaxone').toggle("highlight",{},250);
      return false;
    });
    // Always focus on the first form element
    $(':input:first').focus();
    // keep new password input clear
    $("#newPassword").val("");
});
//pagination
var entriesPerPage = 10;
function pageselectCallback(pageIndex, jq) {
  var startIndex = parseInt(pageIndex)*entriesPerPage;
  var endIndex = startIndex + entriesPerPage;
  $('.library tbody').children("tr:visible").hide();
  $('.library tbody').children("tr").slice(startIndex, endIndex).show();
  return false;
}
function initPagination() {
   var num_entries = $('.library tbody').children("tr").length / entriesPerPage;
   // Create pagination element
   $('.pagination').pagination(num_entries, {
       num_edge_entries: 2,
       num_display_entries: 5,
       callback: pageselectCallback,
       items_per_page:1
   });
}
$(document).ready(function(){
	// pagination
    initPagination();
    $('a.view_all').click(function() {
    	$('.library tbody').children("tr:visible").show();
    	$('.library tbody').children("tr").show();
    	$('.pagination').toggle();
    	$('.paginate').toggle();
    	$('a.view_all').toggle();    	
    	return false;
    });
    $('a.paginate').click(function() {
    	initPagination();
    	$('.pagination').toggle(); 
    	$('.paginate').toggle();
    	$('a.view_all').toggle();  
    	return false;    	
    });
});
$(document).ready(function(){
  // Open external links in a window and cancel the click action
  $("a[href^='http']").click(function(){window.open(this.href); return false;});
  // open popup info window
  $("td.filename a[href$='vs Gilenya.ppt']").after("<br/><a class='info' href='#gilenya'>See Document Description</a>");
  $('a.info[href="#gilenya"]').live('click',function() {
	  $.ajax({
		  url: "/html/info-gilenya.html",
		  cache: false,
		  success: function(data){
			$("#content h1").after(data);
		    $("div.modal.toc").fadeIn('fast');
		  }
		});
	  return false;
  });
});
/* IE only */
function copyToClipboard(str) {
	if( window.clipboardData && clipboardData.setData )	{
		clipboardData.setData("Text", str);
	}
}

