/*
* Common Gemini javascripts
* menus.js
* styleswitch.js
*/

/*
* menus.js
* hide sibling instrument menu items
* and strip instrument heads like 'north', 'south', 'resources
*/

$(document).ready(function() {

    //Remove <a> from menu items that we want to be headers
    $('.menu li#znorth').html('NORTH');
    $('.menu li#zsouth').html('SOUTH');
    $('.menu li#zresources').html('RESOURCES');

    
    //Print 'Show All' text after instruments link 
    //only want code to run if viewing instrument page
  if ($(".menu > li#zinstruments li.active").is(".active")) {

    //hide all siblings (other instruments)
    $(".menu > li > ul > li > ul > li.active").siblings().hide();
    //restore children of current instrument, as they were hidden with above line
    $(".menu > li > ul > li > ul > li >ul > li").show();
    //append 'Show All' text to instruments heading
    $('#zinstruments > a').after("&nbsp;<a id\=\"showInstruments\" href\=\"#\">(Show All)</a>");
  }
    
    //Un-hide other instruments
    $('#showInstruments').click( function() {
        //get text within #showInstruments span
        var showInstruments =  $('a#showInstruments').html();

        //hide or show instruments depending on span value
        switch(showInstruments) {

            case '(Show All)':
                showAllInstruments();
                break;

            case '(Hide)':
                hideAllInstruments();
                break;
        }

    });
	
	function showAllInstruments() {
        $(".menu > li > ul > li > ul > li.active").siblings().show('slow');
        $(".menu > li > ul > li > ul > li >ul > li").show();

        $('a#showInstruments').empty();
        $('a#showInstruments').append('(Hide)');
	}
	
	function hideAllInstruments() {
    $(".menu > li > ul > li > ul > li.active").siblings().hide();
    $(".menu > li > ul > li > ul > li >ul > li").show();
        
        $('a#showInstruments').empty();
        $('a#showInstruments').append('(Show All)');
	}
		
});

/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
* Version 1.1
**/

$(document).ready(function() {
  /* fix ie png transparency */
  $('img[@src$=.png]').ifixpng();

  $(".styleswitch").change(function() {
    switchStylestyle(this.options[this.selectedIndex].value);
    return false;
  });

  var c = readCookie('style1.1');
  if (c) {
    switchStylestyle(c);
    if (c == 'page') {
      $('.styleswitch').attr('selectedIndex',0);
    } else {
      $('.styleswitch').attr('selectedIndex',1);
    }
  }

});

function switchStylestyle(styleName) {
  $('link[@rel*=style][@title]').each(function(i) {
    this.disabled = true;
    if (this.getAttribute('title') == styleName) this.disabled = false;
  });
  createCookie('style1.1', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else {
    var expires = "";
  }
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}
// /cookie functions