// JavaScript Document
$(document).ready(function(){
  
  // externalize links
  externalizeLinks();  

  /***** topMenu *****/ 
  var topMenu = $("#nav li a");
  
  
  //hover
  $("span.hover").hide();
  
 // topMenu.hover(
/* slide down/up menu
    function() {
      $(this).stop().animate(
			{backgroundPosition:'(100% 0)'}, 
			{duration:500});
    },
    function() {
    	$(this).stop().animate(
			{backgroundPosition:'(100% -53px)'}, 
			{duration:500});			
    }
*/

/* fade in/out menu */
    /*function() {
        $("#"+$(this).attr("id")+" .hover").fadeIn(300);
    },
    function() {
        $("#"+$(this).attr("id")+" .hover").fadeOut(300);
    }
  );*/
  var selectedLink;
  
  // ajax topMenu navigation
  topMenu.click(function(event){
    event.preventDefault();
    event.stopPropagation();    
    
    selectedLink = $(this);
    setHash(selectedLink);
	
	if (window.location.href.indexOf("/bo/") > 0) {
		if ($("#editor1")){
			var txteditor = $("#editor1").html();		
			$("#editor1").html('<div id="edit">' + txteditor + '</div>');
			alert(txteditor);
		}
	}
	
    var content = $("div.content").fadeOut(500, function() {
      $(this).html("<div class=\"loading\">&nbsp;</div>").fadeIn(100, function() {
        $.ajax({
          type:"POST",
          url:"php/apeipp-ajax.php",
          data:"page="+ selectedLink.attr("id"),
          success: function(html){
            selectedLink.animate( {opacity:"0.1"}, 300, "swing", function(){                  
              selectedLink.attr("class","nav-item-selected");
              selectedLink.animate({opacity:"0.99"}, 500, "swing");
            });

            $("div.content").hide(1,function(){
			    $(this).html(html).fadeIn(700);
                externalizeLinks();
			
			if (window.location.href.indexOf("/bo/") > 0) {
				var oldText = $("#edit").html();
				$("#edit").html('<textarea name="editor1" id="editor1">' + oldText + '</textarea>');
				
				CKEDITOR.replace('editor1',{
					language: 'pt',
					uiColor: '#eeeee9',
					toolbar: [
							    ['Save'],
							    ['Cut','Copy','Paste'],
							    ['Undo','Redo','-','Find','Replace'],
							    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
								['Image','Table','HorizontalRule','Smiley','SpecialChar'],
								['ShowBlocks'],
								'/',
							    ['Bold','Italic','Underline','Strike'],
							    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
							    ['Link','Unlink','Anchor'],
							    ['Format','Font','FontSize'],
							    ['TextColor','BGColor']							    
							]
				});
			}
			
            });
          }
        });      
      });
      $("#nav li a").attr("class","nav-item");
      $("#side-nav li a").attr("class","side-nav-item");        
    });
  });

  if (window.location.hash.length>0){
    $(window.location.hash).click();
  }
});

    
/**** SideBar ****/
function setSideBar(sidebarId) {
  var sidebar = $(sidebarId);  

  // ajax sidebar navigation
  sidebar.click(function(event){
  event.preventDefault();
  event.stopPropagation();    
  
  selectedLink = $(this);
  setHash(selectedLink);
  
  var content = $("div.content").fadeOut(500,
    function() {
      $(this).html("<div class=\"loading\">&nbsp;</div>").fadeIn(100,
      function() {
        
          $.ajax({
            type:"POST",
            url:"php/apeipp-ajax.php",
            data:"page="+ selectedLink.attr("id"),
            success: function(html){
              selectedLink.animate( {opacity:"0.1"}, 300, "swing", function(){                  
                selectedLink.attr("class","side-nav-item-selected");
                selectedLink.animate({opacity:"0.99"}, 500, "swing");
              });
              $("div.content").hide(1,function(){
                $(this).html(html).fadeIn(700);
                externalizeLinks();                
              });
            }
          });
        
        });
       $("#side-nav li a").attr("class","side-nav-item");
       $("#nav li a").attr("class","nav-item");
    });
  });
  
  var fb = $("#fb");
  fb.css("opacity","0.6");
  fb.hover(
    function(){
      fb.animate( {opacity:"0.99"}, 300, "swing");
    },
    function(){
      fb.animate( {opacity:"0.6"}, 300, "swing");  
    }
  );
}

function setHash(queryString){
  var link = $(queryString); 
  window.location.hash = link.attr("href").split("=")[1];
  document.title="APEIPP | " +   link.children("span").html().replace("<br>"," ").substr(0,25);  
  $("body").scrollTop(0);

}
/**** Accordion's Inner-Bar ****/
function setAccordion(accordionId){
  var innerBar = $(accordionId);
  
  // inicialização do 1º item selected
  $(".topico")[0].className="topico-selected";
  $(".topico").hide();
  $(accordionId)[0].className="accordion-selected";
  
  
  innerBar.click(function(event){
    event.preventDefault();
    event.stopPropagation();    
    
    var selectedLink = $(this);
    
    var selectedTopic = $("#"+selectedLink.attr("href").split("#")[1]); 

    var isnew = false;
    
    if (selectedTopic.attr("class") !== "topico-selected"){
    
      $(accordionId).removeClass("accordion-selected");
      this.className = "accordion-selected";
      
      /*
      // toggle links
      oldSelected.animate({opacity:"0.1"}, 300, "swing", function(){
        $(this).attr("class","topico").animate({opacity:"0.99"},500);
      });
      selectedLink.animate({opacity:0.1}, 500, "swing", function(){
        selectedLink.attr("class","topico-selected").animate({opacity:0.99},300);  
      });
      */
      
       
      // expand/collapse
      var oldSelected = $(".topico-selected");
      selectedTopic.attr("class","topico-selected");
      oldSelected.slideUp(500,function(){
        oldSelected.attr("class","topico"); 
        selectedTopic.slideDown(500);
      });
      
    }        
    
  });
}

/* Eventos - Tab dos Eventos*/ 
function setTabs(listSelector){
  var tabList = $(listSelector);
  
  // esconde todos menos o primeiro
  for (var i=1; i<tabList.length; i++){
    $(tabList[i].hash).hide();
  }  
  tabList[0].className="tab-selected";
  
  tabList.click(function(event){
    event.preventDefault();
    event.stopPropagation();
    var selected = this;
    if (selected.className !== "tab-selected"){
      for (var i=0; i<tabList.length; i++){
        if (tabList[i].className == "tab-selected"){
          $(tabList[i].hash).slideUp("fast", function(){
            $(selected.hash).slideDown("fast");    
          });
        }
      }
      tabList.removeClass("tab-selected");
      selected.className = "tab-selected";
    }
  });
}

/* Esconde os detalhes de eventos */
function prepareDetails(){
  var evts = $("div.event-detail");
  var evt;
  for (var i=0; i<evts.length;i++){
    evt = $(evts[i]); 
    evt.attr("id","evt_"+i);
    evt.before("<a class=\"detail\" href=\"#evt_"+i+"\">Informação adicional</a>");
  }
  
  $("div.event-detail").hide();
  
  $("a.detail").click(function(event){
    event.preventDefault();
    event.stopPropagation();
    $(this).next().toggle("fast");  
  });
}

function externalizeLinks(){
  $("a[rel='external']").attr('target','_blank');
}
