$(function() {
  
    var app_debug = 0;
  
    $('a.show_div').click(function() {
        //var trgt = $(this).attr('href').slice(1);
        //console.log($(this).attr('href'), trgt);
        $($(this).attr('href')).toggle();
        return false;
    });


    $('.hide_parent').each(
        function(){
            $(this).parent().hide();   
        }
    );

    // Create Footnote links for print
    footnoteLinks('maincol','printlinksdiv');
    // Put current URL in footer for print-backref
    //console.log(document, document.documentURI, document.location);
    $(document.createElement("div")).addClass("printOnly").addClass("footer").append(document.createTextNode(location.href)).appendTo('#footer_container');

});


//
// Various usefull functions
//

function addbasket(node_id, obj_id) {
	var form = document.forms["addbasket"];
	form.elements["ContentNodeID"].value = node_id;
	form.elements["ContentObjectID"].value = obj_id;
	form.submit();
	return true;
}

//
// Clear Form elements
function clearForm(form){
	for(i=0; i<form.elements.length; i++)
	{
		with(form.elements[i]){
			if(type=='text' || type=='textarea' || type=='password'){
				value='';
			}
			checked = false;
		}
	}
}

function CreateBookmarkLink(title, url) {
    if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}


/* Alistapart - custom style */
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
   createCookie("nupistyle", title, 365);
}

function getActiveStyleSheet() {
var i, a;
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  if(a.getAttribute("rel").indexOf("style") != -1
  && a.getAttribute("title")
  && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

 
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 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;
}


/* Footnote Links for Print  - based on:
    footnoteLinks() by Aaron Gustafson
    Version:        1.3
    Homepage:       http://www.easy-designs.net/code/footnoteLinks/
*/
function footnoteLinks(containerID,targetID) {
    var a_enum = 0;
    var a_ary = [];
    
    $('#'+targetID).append('<h2>Links</h2>').append('<ol id="footnotelinks"></ol>');
  
  $('#'+containerID).find('a:visible').each(function(){
    //console.log('link: ', $(this).attr('href'));
    // Skip ignore-links and anchors with no href
    if(!$(this).hasClass('ignore') && $(this).attr('href')) {
        var i = jQuery.inArray($(this).attr('href'), a_ary);
        // NOTE: fix relative URLs
        var full_href = ($(this).attr('href').charAt(0)=='/') ? 'http://'+document.domain+$(this).attr('href') : $(this).attr('href');
        //console.log('Relative URL: ', $(this).attr('href'), document.domain, window.location.hostname, location.pathname);
        if(i!=-1) {
            //console.log('exists: ', $(this).attr('href'));
            $(this).after('<sup class="printOnly">'+parseInt(i+1)+'</sup>');
        } else {
            $(document.createElement("li")).append(document.createTextNode(full_href)).appendTo('#footnotelinks');
            a_enum++;
            //$(this).after('<sup>'+a_enum+'</sup>');
            var sup = $(document.createElement("sup")).addClass("printOnly").append(document.createTextNode(a_enum));
            $(this).after(sup);
            a_ary.push($(this).attr('href'));
        }
    }
  });
  return true;
}
