Shadowbox.init();

// wait until document is fully scriptable
$(function() {

    flowpanes();
  accordion();
  expandablePanels();
  toggleDropdown();
  popeye();  
  addEffects();  
  setEqualWidth($(".sitemap li.level2"));  
  setEqualHeight($(".sitemap li.level2"));
  $("#main").prepend("<a name='content-anchor' id='content-anchor' class='accessibility' href='#'>Main Content</a>");
  $("#nav .wrapper").prepend("<a name='navigation-anchor' id='navigation-anchor' class='accessibility' href='#'>Navigation</a>");
  $("#search").prepend("<a name='search-anchor' id='search-anchor' class='accessibility' href='#'>Søgning</a>");
  
  $(".frontpage").prepend("<a name='content-anchor' id='content-anchor' class='accessibility' href='#'>Main Content</a>");
  
  $('#main .foldoutbanner span.trigger').click(function() {        
        $(this).prev(".bannerContent").toggle("slow"); 
    });
  //Open foldoutbanner
    $('#main .foldoutbanner span.enlarge').click(function() {        
        $(this).prev("span.trigger").click();
        $(this).hide();        
    });
    //Close foldoutbanner
    $('#main .foldoutbanner span.compact').click(function() {        
        var content = $(this).parentsUntil(".foldoutbanner"); 
        $(content).hide("slow",function(){
            $(this).parents(".foldoutbanner").find("span.enlarge").show(); 
        });
    });
    
    $("#accordion").find("a.active").parent("li").css("background","url('/images/left_hover.gif') repeat-x scroll 0 0 #fff");
      
    var colorClass = $("#breadcrumb").attr("class");    
    $("#flowpanes").find(".manchet").addClass(colorClass);
  //$(".mailForm").find(".FormInputRow").first().css("height","120px");

$(".imagebanner").each(function(){
  
     if($(this).parent().hasClass("ppy-placeholder"))
     {         
     }
     else
     {      
        var href=$(this).find("a").attr("href");
        var target =$(this).find("a").attr("target");
       
        if(href!="" && href!=undefined)
        {
          $(this).css("cursor","pointer");
          $(this).click(function(){
            if(target =="_blank")
            {
              //window.open(href);              
            }
            else
            {
              window.location=href; 
            }
          });
        }
     }
});

});

function addEffects()
{
    $("img.hover").mouseover(function(){
      var trans = $(this).parent().find("span.trans");
      $(trans).addClass("transActive");
      $(trans).animate({
          top: '50'
      }, 1000, function() {
            togglePaneDropdown();
        });
  });
    $("a.trigger").mouseover(function(){
      $(this).parent().find("span.trans").animate({
          top: '30'
          
          });
      
  });  
  
    $("a.trigger").mouseout(function(){
      $(this).parent().find("span.trans").animate({
          top: '140'
          });
      
  });
  
    $("a.triggerAlbum").mouseout(function(){
      $(this).parent().find("span.trans").animate({
          top: '120'
          });
      
  });  
  $("<div class='leftmenubottom'></div>").insertAfter('#local ul.subnav');  
}

function openBox(url)
{   
    $.get(url, function(data) {
       
        Shadowbox.open({
            content:    data,
            player:     "html"
            },
            function(){            
                Cufon.refresh();
            })
        }
    );   
   
    return false;
}

//Rejseplanen
function rejseplan()
{
    var text = $("#rejseplan").html();
      
       Shadowbox.open({
        content:    text,
        player:     "html",    
        height:     180,
        width:      302  
        },function(){Cufon.refresh();}
    );   
    return false;
}




function expandablePanels()
{  
  // quicklink Panel
  $("#quicklink").click(function(){
    
      //$('div#quicklinks').css('width', '976px');
      //$('div#quicklinks').css('height', $('div#quicklinks').height() + 'px');
      $("div#quicklinks").animate({ height: "toggle", opacity: "toggle"}, "slow" );
    
    //$("div#quicklinks").slideDown("slow");
  
  });  
  // quicklink Panel
  $("#quickClose").click(function(){
    $("div#quicklinks").slideUp("slow");
  
  });
  // sitemap Panel
  $("#sitemaplink").click(function(){
    $("div#sitemap").slideToggle("slow");
  
  });  
}

function sortBy(e)
{  
  window.location.href="?sort="+e.value;
}


//Subpage accordion left navigation
function accordion()
{
    var active; 
    $("#accordion a").each(function(){
        
        if($(this).hasClass('active'))
        {
            active = $(this).closest('.pane').prev('h3').index('h3');
            $(this).closest('.pane').prev('h3').addClass('current');
            $(this).closest('.pane').css('display','block').addClass('open');
        }
    });
    
    if(active!=undefined)
    {        
        $("#accordion").tabs("#accordion div.pane", {
        tabs: 'h3', 
        effect: 'slide',
        initialIndex: null
        });          
    }
    else
    {
        
        $("#accordion").tabs("#accordion div.pane", {
        tabs: 'h3', 
        effect: 'slide',
        initialIndex: 0
        });   
    }
     //alert(index);

      
     $("#accordion h3").last().css("border-bottom","none");  
        
     
}

//subpage tabs
function flowpanes()
{
  var tallestcolumn = 0;
  $("#flowpanes .items div").each(
       function()
       {
          currentHeight = $(this).height();
          
          if(currentHeight > tallestcolumn)
          {
              tallestcolumn  = currentHeight;
          }
       }
    );
    $("#flowpanes").height(tallestcolumn+16);
  
    // select #flowplanes and make it scrollable. use circular and navigator plugins
  $("#flowpanes").scrollable().navigator({
    // select #flowtabs to be used as navigator
    navi: "#flowtabs",
    // select A tags inside the navigator to work as items (not direct children)
    naviItem: 'a',
    // assign "current" class name for the active A tag inside navigator
    activeClass: 'current',
    // make browser's back button work
    history: true
  });
}


function frontpageTabs()
{
    /* Initial random tab */
    randomizeTabs();   
    
    
}
function togglePaneDropdown()
{
    $("span.transActive .dropdownlink").mouseover(function(){    
        var id = $(this).attr("href");        
        $(id).show();
        return false;
    });   

}
function toggleDropdown()
{
    $(".ddWrap").mouseover(function(){    
        var dd = $(this).find(".dropdown");                
        $(dd).show();       
                
        return false;
    });
    $(".ddWrap").mouseout(function(){    
        var dd = $(this).find(".dropdown");
               
        $(dd).hide();
        return false;
    });

}

function randomizeTabs()
{
    // Count the number of items
    var countTabs = $('.tabs a').size();     

    // Find a random number between 0 and the number of items 
    var randomizeIt = Math.floor(Math.random()*(countTabs));      

    // Click the chosen item    
    $("ul.tabs").tabs("div.panes > div",{ initialIndex:randomizeIt });
}

function hideSchool(element)
{
    var value = $(element).attr("value");
    //Trigger
    if($(element).is(':checked'))
    {
       $("li.schoolFolder[id*='"+value+"']").show();           
    }
    else
    {    
       $("li.schoolFolder[id*='"+value+"']").hide();
    }      
}
function sortByEdu(element)
{
     var value = $(element).attr("value");
     
     $("li.counsellorItem").show();
     
     if(value!='all')
     {     
        $("li.counsellorItem").each(function(){      
               
         if ($(this).attr("rel").search(new RegExp(value, "i")) < 0) {        
                    $(this).hide();
                } else {
                    $(this).show();
                }
        })
    }
    else
    {
        
        $("li.counsellorItem").show();
    }
    $("li.counsellorItem").removeClass("workerItemRight"); 
    $("li.counsellorItem::visible::odd").addClass("counsellorItemRight");   
     
}

function simplefilterList(element)
{    
    var value = $(element).attr("value");
  
  
    //Trigger
    if($(element).is(':checked'))
    {
       $("li[rel*='"+value+"']").addClass('showElement'+value); 
      
       if(value==12){$("li[rel*='Næstved']").addClass('showElementNæstved');}
       if(value==13){$("li[rel*='Vordingborg']").addClass('showElementVordingborg');}
       if(value==14){$("li[rel*='Ringsted']").addClass('showElementRingsted');}
       
    }
    else
    {    
       $("li[rel*='"+value+"']").removeClass('showElement'+value);
      
       if(value==12){$("li[rel*='Næstved']").removeClass('showElementNæstved');}
       if(value==13){$("li[rel*='Vordingborg']").removeClass('showElementVordingborg');}
       if(value==14){$("li[rel*='Ringsted']").removeClass('showElementRingsted');}
      
    }      
    
    /* Employee list */
  $("li.workerItem").removeClass("workerItemRight");
    $("li.employeeFolder").show();
    $("li.employeeFolder").each(function(){
        
        if($(this).find("li:visible").length==0)
        {
            $(this).hide();
        }
        else
        {
            $(this).show();
        }
       $(this).find("li.workerItem:visible:nth-child(3n)").each(function () {
                $(this).addClass("workerItemRight");
        });
        
    }); 
      
    
    
}   

function simplefilterCalendar(element, id)
{
    var value = $(element).attr("value");
    var checked = $(element).is(":checked");
    
    
  var type1='0'
  var type11='0'
   var type2= '0';
  var type22= '0';
   var type3= '0';
  var type33= '0';
   
   if($('#calendarfilter').find('#Checkbox1').is(":checked"))
    {
        type1 = '12';
      type11 = 'Næstved';
    }
    if($('#calendarfilter').find('#Checkbox2').is(":checked"))
    {
        type2 = '13';
       type22 = 'Vordingborg';
    }
    if($('#calendarfilter').find('#Checkbox3').is(":checked"))
    {
        type3 = '14';
      type33 = 'Ringsted';
    }
    replacecalendar(type1,type2,type3,type11,type22,type33,id); 
    
    
    
    
    
}

function replacecalendar(type1, type2, type3,type11, type22, type33, id)
{
    $("#navigateMe").find('div').detach();
    
    $.get('AjaxGetCalendarEvents.aspx', { type1: type1, type2 : type2, type3:type3, type11: type11, type22 : type22, type33:type33, id:id }, function(data) {
            $("#navigateMe").append($(data));
            
            
            $(".eventfolder").show();
            $(".eventfolder").each(function(){
    
                if($(this).next().hasClass("eventfolder")){ $(this).hide(); }
                else
                {       
                    var month = $(this).attr('id');
        
                    if($("#navigateMe div.events-"+month+":visible").length==0)
                    {   
                        $(this).hide();
                    }
                    else
                    {
                        $(this).show();
                    }
                }
        
            });
         pagination();
      });  
}

function pagination()
{    
    var max = 9;
    
    if($('#navigateMe').find("div:visible").length > max )
    {    
        $('#page_container').pajinate({
        items_per_page : max,        
        nav_label_prev : '<',
        nav_label_next : '>'        
      });  
      $('.page_navigation').show();
  }
  else
  {
      $('.page_navigation').hide();
      
  }
}


function popeye()
{
    var options1 = {
            navigation: 'hover',//hover, permanent or false
            direction:  'left'
        }
        var options2 = {
            caption:    'permanent', //hover, permanent or false
            navigation: 'permanent',//hover, permanent or false
            direction:  'left'
        }
        var options3 = {  
            caption:    'permanent',
            navigation: 'permanent',         
            direction:  'left'
        }
$("#sub .popeye3").each(function(){
    $(this).popeye(options3);
});

$("#main .popeye3").each(function(){
    var direction = $(this).attr("rel");
    
    var options = {  
            caption:    'permanent',
            navigation: 'permanent',         
            direction:  direction
        }

    $(this).popeye(options);
});



       
}

function search(word, url)
{

$('input#search-site').keypress(function(e) {
    if (e.keyCode == '13') 
    {
        e.preventDefault();
        var val = $('input#search-site').val();
        //window.location = "/soegeresultat.aspx?search="+val;
       window.location = url+"?search="+val;
    }
});


}
function setEqualWidth(columns)
{
    var wrapwidth = $(".sitemapwrap").width()-16;
    var colWidth = wrapwidth / $(columns).length;
   
    columns.width(Math.floor(colWidth));
    
    
}
function setEqualHeight(columns)
{  
    /*
    var tallestcolumn = 0;
    columns.each(
       function()
       {
          currentHeight = $(this).height();
          
          if(currentHeight > tallestcolumn)
          {
              tallestcolumn  = currentHeight;
          }
       }
    );
    columns.height(tallestcolumn);
    */
    
}
function simplefilterRefs(element) {
   var value = $(element).attr("value");

    //Trigger
        
    if ($(element).is(':checked')) {
                $("ul.newslist li[rel*='" + value + "']").removeClass('hideElement' + value);
    }
    else {
                $("ul.newslist li[rel*='" + value + "']").addClass('hideElement' + value);
    }
} 
  function simplefilterBrochures(element)
{    
    var value = $(element).attr("value");
    
    $("tr[rel*='"+value+"']").removeClass('showAll');
    //Trigger
    if($(element).is(':checked'))
    {
       $("tr[rel*='"+value+"']").addClass('showElement'+value);           
    }
    else
    {    
       $("tr[rel*='"+value+"']").removeClass('showElement'+value);
    }      
    
        
}
      
/*  
*
* Studieguide
*
*/


function addScrolling()
{  
  $.scrollTo( 0 );
}
function filterme(element,number)
{    
     var id = $(element).closest("div.pane").attr("rel");    
     var value = $(element).attr("value");
     var rel = $(element).attr("rel");
     
            
               
     if($(element).is(':checked'))
     {                
         var add = "<span id='f-"+value+"'>"+value+" </span>"
         $("#filters").find("li[rel='"+id+"']").append(add);
       
        toggleResult();
     }
     else
     {
         var remove = $("#filters").find("span[id='f-"+value+"']").detach();          
         toggleResult();
     }
}
function toggleResult()
{
    $(".result").each(function () {
    //add extra && for each filter pane
        if (hasFilter(3, $(this)) && hasFilter(1, $(this)) && hasFilter(4, $(this))) {
            $(this).show()
        }
        else {
            $(this).hide() 
        }
    });
    //Toggle resultpanes
    hideEmptyPanes();
}
  
  
function hasFilter(filter, result)
{ 
  
      var showMe = false;
          
          //Iterate over checkboxes
          if($("input.filterBox:checked[filter='"+filter+"']").length)
          {            
             $("input.filterBox:checked[filter='"+filter+"']").each(function(){
                var rel = $(this).attr("rel");           
           
                //Contains filter               
                 if($(result).attr("filter"+filter).indexOf(rel) != -1)
                 {                  
                    showMe = true;                                                
                 }
             
             }); //end checkboxes
        }
        else
        {
           showMe = true;
        }
          

      return showMe;
}
  

function toggleResultsOLD(show,filter,value)
{    
    var results = $(".result:[filter"+filter+"*='"+value+"']");
    var otherresults = $(".result:[filter"+filter+"!='"+value+"']");
    
    
    //Reset class
    $(results).removeClass("showFilter"+filter);
    $(results).removeClass("hideFilter"+filter);  
    
    $(results).each(function(){
        if(show)
        {
            $(this).addClass("showFilter"+filter);
        }
        else
        {
            $(this).addClass("hideFilter"+filter);
        }
    });  
    
    if(show)
    {          
        $(otherresults).addClass("hideFilter"+filter);      
    }
    
    if($("#interests input[filter='"+filter+"']:checked").length==0)
    {        
        $(".result").removeClass("hideFilter"+filter);
    }
    //If no filters selected, show all results
    if($("#interests input:checked").length==0)
    {        
        $(".result").removeClass("hideFilter1").removeClass("hideFilter2").removeClass("hideFilter3").removeClass("hideFilter4");
    }    
    
    //Toggle resultpanes
    hideEmptyPanes();
     
}
//Hide resultpanes with no results
function hideEmptyPanes()
{
    $(".resultpane").show();

    $(".resultpane").each(function(){
      var matches = $(this).find(".result:visible").length;     
        $(this).find("span.match").empty();
        $(this).find("span.match").text(matches);
        if($(matches).length==0)
        {
            $(this).hide();
        }
    });
}

function showResult(link,id)
{
    $("#bottomResult").show();
    $("#bottomResult").removeClass();
    $("#bottomResult").addClass("bottomResult").addClass("clearfix");
    var color = $(link).attr("rel");
   
    
    $("#bottomResult").empty().html('<img src="/images/spinner.gif" alt="loading..." style="" />');
    $('#bottomResult').load('AjaxStudieguideResult.aspx?id='+id,{color: color}, function() {     
            $("#bottomResult").addClass(color);
             $.scrollTo($('#selectedResult'), 800);
             Cufon.replace("#selectedResult h3");
             Cufon.refresh();
             flowpanes();
             //Set result divs height
             if($("#selectedResult").height() > $("#similarResult").height())
             {
                $("#similarResult").height($("#selectedResult").height())
             }  
    });
    return false;
}
function closeResult()
{
    
    $.scrollTo($('#results'), 800, function(){ $('#bottomResult').empty();});
    return false;
}
        
        
function initializeGuide()
{
        
        Cufon.replace("#selectedResult h3, h3.resultTop, div.sectionTitle");
        $("#bottomResult").hide();
        $("span.tip[title]").tooltip({offset:[0, 70]});
        
        
        //Load results
        $("#resultSection").empty().html('<img src="/images/spinner.gif" alt="loading..." style="" />');
        $('#resultSection').load('AjaxStudieguide.aspx', function() {     
            $(".result[title]").tooltip({offset:[2, 0]});
            hideEmptyPanes();
        });
    
       //Initialize tabs
       $("#interests").tabs("#interests div.pane", {
            tabs: 'h3',
            effect: 'slide',
            initialIndex: 0,
            onClick: function(event, tabIndex) { Cufon.refresh() }
        });
        
        
        //Half width on ul if there's more than one
        $("#interests .pane").each(function(){        
            if($(this).find("ul").length > 1)
            {            
                var width = $(this).width();
                $(this).find("ul").width(width/2);
                
            }
        });
        
        addScrolling();
} 

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

