$(document).ready(function() {
  
  // initialize shadowbox
  Shadowbox.init();

  $(".close-drawer").click(function(e) {
    Microsigns.closeDrawer();
    e.preventDefault();
  });
  
  $("#drawer_opener").click(function() {
    Microsigns.openDrawer(1);
  });

  // initialize the page
  
  //Should have a check to see if a url with a tab has been used so we open that tab
  //alert(document.location);
  function findTabLocation(){
      var tabLocation = null;
      var content = null;
      tabLocation = document.location.href.split("#")[1];
      if(tabLocation){
          content = tabLocation.split("_");
          if(content[1] == '1' || content[1] == '2' || content[1] == '3' || content[1] == '4') {
              //console.log(tabLocation);
              $("#" + tabLocation).addClass("active")
              contentStr = "#content_" + content[1];
              $(contentStr).show();
              Microsigns.openSection(content[1]);
          }else{
              $("#tab_1").addClass("active"); //yeah, yeah, repitition, repetition
              $("#content_1").show();   
              Microsigns.openSection(1);
          }
      }else{ //no tab selected, go to the first
          $("#tab_1").addClass("active");
          $("#content_1").show();   
          Microsigns.openSection(1);
      }
  };
    
  // initialize tabs
  $(".tab").click(function(e) {
    var id = $(this).attr('id').split("_")[1];
    $(".content-pane").hide();
    Microsigns.openSection(id);
    $("#content_" + id).show();
    goTo("#tab_" + id);
    $(".tab").removeClass("active");
    $(this).addClass("active");
    e.preventDefault();
    return false;
  });
  
  // Initialize the news section
  $(".news-year-selector").click(function() {
    var year = $(this).attr('id').split("_")[1];
    $(".post").hide();
    $(".post.year-" + year).show();
    $(".news-year-selector").removeClass("active");
    $(this).addClass("active");
    return false;
  });
  
  //Initialize the read_more links
  $(".read_more").click(function(){
     var id = $(this).attr('class').split(" ")[1];
     var link =$(this);
     Microsigns.toggleMore(id, link);
     return false;
  });
  
  
  // Initialize the entire footer
  function initFooter() {
      $('#footer_wrapper_top').hide(); //hides the close button initially
      initLinks();
  };
  //
  
  // initialize footer links
  function initLinks() {
    $(".footer-link").click(function() {
      var id = $(this).attr('id').split("_")[1];
      var cls = $(this).attr('class');
      if (Microsigns.activeFooterContent == id) {
          goToBottom();
        $("#footer_content_"+ Microsigns.activeFooterContent).hide();
          Microsigns.activeFooterContent = null;
      }
      else {
        if (Microsigns.activeFooterContent != null) { //we have an active content
            $("#footer_content_"+ Microsigns.activeFooterContent).hide();
            if (id == 'close') { //if they hit the close button
            } else {
              $('#footer_wrapper_top').show();
              $("#footer_content_"+ id).show();
              goToBottom();
              Microsigns.activeFooterContent = id;
            }
        } else {
          $('#footer_wrapper_top').show();
          $("#footer_content_"+ id).show();
          goToBottom();
          Microsigns.activeFooterContent = id;
        };
      };
      return false;
    });
  };
  
  /**
  * Moves the page to the bottom anchor... used in the footer.
  */
  function goToBottom() {
    var url = location.href.split("#")[0];
    location.href = url + "#bottom";
  }
  
  function goTo(place) {
    var url = location.href.split("#")[0];
    location.href = url + place;
  }
  
  
  initFooter();
  
  /**
  * Gets all of the section links and their corresponding sections, for making an accordion
  */
  
  Microsigns.getSectionLinks(false);
  findTabLocation();
});


/**
* This object is used to namespace any javascript that is specific to this
* website
*/
var Microsigns = {
  
  
  drawerMapping: {
    'Hardware01': 0,
    'Hardware02': 1,
    'Hardware03': 2,
    'Software01': 3,
    'Software02': 4,
    'Software03': 5,
    'Software04': 6,
    'Software05': 7,
    'Software06': 8
  },
  
  
  sectionObject: {
      sectionLinks : null,
      sections : null,
      clickTracker : null
  },
  
  /**
  * Indicates the id of the footer content that is currently visible 
  */
  activeFooterContent: null,

  /**
  * Closes the drawer pane
  */
  closeDrawer: function() {
    $("#drawer").slideUp('normal', function() {
      $("#drawer .panel").hide();
    });
  },

  /**
  * Opens the drawer pane
  */
  openDrawer: function(panel_id) {
    $("#drawer .panel").hide();
    $("#drawer_" + this.drawerMapping[panel_id]).fadeIn();
    $("#drawer").slideDown();
  },

  toggleMore: function(toggle, link){
    var whichMore = toggle;
    var hidden = $("#"+whichMore).css('display');
    var msg;
    if (hidden !== "block") {
        msg = "read less";
    }else{
        msg = "read more";
    };
    $("#"+whichMore).toggle("fast");
    link.text(msg);
  },
  
  getSectionLinks: function(first) {
    Microsigns.sectionObject.sectionLinks = $('.section_link'), //gets all the section links
    Microsigns.sectionObject.sections = $('.section'), //gets all of the sections
    Microsigns.sectionObject.clickTracker = null //tracks which section is open and closes it on a second click
        
    $.each(Microsigns.sectionObject.sectionLinks, function(i) {
      var sL = $(Microsigns.sectionObject.sectionLinks[i]);
      var section = sL.next(".section"); //get the section that comes after the link
      var sText = $(section[0]);            
      sL.bind("click", function(e){
        //alert(clickTracker);
        if (this == Microsigns.sectionObject.clickTracker){
          //sText.slideUp('fast');
          //clickTracker  = null;
          return false;
        }
        Microsigns.sectionObject.sections.hide(); //hide all the sections
        sText.slideDown('fast'); //show the right section
        Microsigns.sectionObject.clickTracker = this;
        e.preventDefault();
      }, false);
      Microsigns.sectionObject.section == null;
    });
        
    //open the first section by default
    if (first == true){ //deprecated by openSection now
      //alert(sectionLinks);
      $(Microsigns.sectionObject.sectionLinks[0]).next(".section").show();
      Microsigns.sectionObject.clickTracker = Microsigns.sectionObject.sectionLinks[0];
    }
        //
  },
    
  openSection: function(id) { //opens a section by passing an id, which is in turn gathered on tab change which calls this function
    $(".section").hide();
    $('#first_section_' + id).next(".section").show();
    Microsigns.sectionObject.clickTracker = $('#first_section_' + id).get(0); //returns the DOM elment not the JQuery object
    //alert(Microsigns.sectionObject.clickTracker);
  },

  /**
  * Closes the footer
  */
  closeFooter: function() {
    $(".footer-pane.active").slideUp("normal", function() {
      $(".footer-pane.active").removeClass("active");
    });
  },
	
	/**
	* Indicates the id of the footer content that is currently visible 
	*/
	activeFooterContent: null,
	
	/**
	* Closes the drawer pane
	*/
	closeDrawer: function() {
		$("#drawer").slideUp();
		document.getElementById('flash_header').onCloseDrawer();
		return false;
	},
	
	/**
	* Closes the footer
	*/
	closeFooter: function() {
		$(".footer-pane.active").slideUp("normal", function() {
			$(".footer-pane.active").removeClass("active");
		});
	},
	
	/**
	* Shows the news items for the given year
	*/
	showYear: function(year) {
    $(".post").hide();
    $(".post.year-" + year).show();
    $(".news-year-selector").removeClass("active");
    $("#post_" + year).addClass("active");
    return false;
	}
	
	
};