// MDH bar colors
var colors     = new Array("#bee2ea", "#bee2ea", "#bee2ea", "#bee2ea", "#bee2ea","#bee2ea", "#bee2ea","#bee2ea", "#bee2ea","#bee2ea");  //colors of bars
var maxheight  = 120;

jQuery(document).ready(function($){
	// PNG shade on featured article images
	$("#headline .post-image .post").wrap("<div class='shade'></div>");
	// Speech bubble on comments
	$("dt.author-avatar img").before("<span></span>");
	// Zebra Striped Tables
	$(".article tr:nth-child(odd)").addClass("odd");
	// Table sorting
	$(".article table").tablesorter(); 
	// Bogbidder manipulation
	$(".book-snippet h1,img.library-heading-logo").remove();
	$("#bogbidder td,#bogbidder ul,#bogbidder ul li,.bogbidder-signup,.bogbidder-signup a").removeAttr("style")
	// Subscription Dropdown
	$("div#subscription").addClass("show-subscription");
	$("div#subscription-list").addClass("hidden");
	$("div#subscription-list").addClass("dropdown");
    
    $("div#toggle-options").click(function () {
      if ($("div#subscription-list").is(":hidden")) {
        $("div#subscription-list").slideDown("fast");
      } else {
        $("div#subscription-list").slideUp("fast");
      }
    });
	$("div#subscription-list span.close small").click(function () {
      $("div#subscription-list").slideToggle("fast");
    });
    // Banner - Krimimessen
    $("div#banner a.hide").click(function () {
      $("div#banner").slideToggle("slow");
    });
	// Tabs - http://jqueryfordesigners.com/jquery-tabs/
	$(function () {
    var tabContainers = $('div#type-tabs > div');    
    $('div#type-tabs ul.tabNavigation a').click(function () {
        tabContainers.hide().filter(this.hash).show();        
        $('div#type-tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');        
        return false;
    }).filter(':first').click();
	});
	// Topstory Cycle
	$(function() {
    $('#headline').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 7000,
        pager:  '#topstory-navigation',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#topstory-navigation li:eq(' + (idx) + ') a';
        }
    }); 
    $('#topstory-navigation li a').click(function() {
        $('#headline').cycle('pause');
    });   
    $('#topstory-navigation li a').click(function() {
        $('#headline').cycle('resume');
    });
	});
	// inline form labels by Mike Cravey - http://craveytrain.com/accessible-inline-form-labels
	var oTxtFields = $('#search input,#search textarea');
	$.each(oTxtFields, function(){
		var label = $('#search label[for=' + $(this).attr('id') + ']');
		label.addClass('overlayed');
		if (!$(this).val() == '') {
			label.hide();
		}
		$(this)
			.focus(function(e){
				$('#search label[for=' + $(e.target).attr('id') + ']').hide();
			})
			.blur(function(e){
				if ($(e.target).val() == '') {
					$('#search label[for=' + $(e.target).attr('id') + ']').show();
				}
			})
		;
	});

	
	// MDH graphs
	$.ajax({
		type:     "GET",
		url:      "http://horsensbibliotek.dk/projektmdh/Graf2/make_list_10_mest_udlaante_emner.php",
		dataType: "json",
		success: function(json){;
			var dataArray;
			var counter = 0;
			var maxvalue = 0;
            var singlegraphwidth = 600;
            var leftposition = 0;
            var topposition = 0;
            var graphmargin = 20;

			for( var year in json){
            	for( var i=0; i<json[year].length; i++ ){
                		if( json[year][i][1] > maxvalue ) maxvalue = json[year][i][1];
				}
			}


            for(var year in json ) {   //aha! key in "property map" (= assoc array)
            	arrayOfData = new Array();
                var localmax = 0;
                            
                for( var i=0; i<json[year].length; i++ ){
                	if( json[year][i][1] > localmax ) localmax = json[year][i][1];
                    	arrayOfData.push( new Array( json[year][i][1], makelink( json[year][i][0]) + "", colors[i] ))
				}
                $("#graphs").append(
                	"<div class='singlegraphcont' id='"+year+"container' style='top: "+topposition+"px;'>"+ // style='left: "+leftposition+"px;'
                    	"<div id='"+year+"' style='height:"+Math.round(localmax / maxvalue * maxheight)+";'></div>"+
                    "</div>"
				);
				// $(".singlegraphcont:nth-child(2)").addClass("graph2");
                $("#"+year).jqBarGraph( {
                	data:arrayOfData,
                    title: "",
                    width: singlegraphwidth,
                    animate: true,
                    speed: 2,
                    height: maxheight,
                    legendWidth: maxheight,
                    barSpace: 10,
                    showValues: false
				});
				$("div[id^='graphFieldBar']").addClass("graphbar");
                $("#graphs").css({"height": maxheight * 2 + 50});                
                // $(".graphLabel").css({"height": maxheight});
                $("#"+year+"container").append("<div class='yeartitle'>"+year+"</div>");   //title of graph
                counter ++;
                leftposition += singlegraphwidth + graphmargin;
                topposition += maxheight + graphmargin;
                }
			}
		});  
	
});

// MDH Graph links
function makelink( term ){
	return "<a href='https://www.bibfolk.horsens.dk/sites/WWW/pub/search.html?doaction=search&data=scode_ccl=lem%3D"+escape(term)+"%20&request_attr_sortformat=1%20&request_attr_defaultpage=20%20&rec=0'>"+term+"</a>"
	}

// The search form 
function OnSubmitForm()
{
	var list = document.sf.choice;
	var myform = document.sf;
	if(list.options[list.selectedIndex].value == "bb")
  {
    myform.data.value = 'request_attr_base=www scode_freetext='+escape(document.sf.keyword.value);
    myform.action ="https://www.bibfolk.horsens.dk/sites/WWW/pub/search.html";  	
  }
  else
  if(list.options[list.selectedIndex].value == "hj" )
  {
        myform.action ="http://horsensbibliotek.dk/?s="+unescape(document.sf.keyword.value);
  }  
  return true;  
}


