//initurl prepare for ajax
initurl = window.location.href.substr(20,1);
// local 30
// remote 20
if (initurl!='#' && initurl!=='')
{
hrefid = window.location.href.substr(20); 
// local /pianorama.dk/
// remote /
window.location.href = '/#'+hrefid;
}
nyside = '#';


/* content functions -------------------------------------------------------------------------------------------------------------------*/
// erstat navne med link -----------------
function pianist_link(showny) {
var count = $('#menu a[title="Pianisterne"]').next('ul').find('li').length;
var n = 0;
while (n<count)
{
var pianist = $('#menu a[title="Pianisterne"]').next('ul').find('li:eq('+n+') a').attr('title');
var href = $('#menu a[title="Pianisterne"]').next('ul').find('li:eq('+n+') a').attr('href');

showreplace = showny.replace(new RegExp( pianist, 'g' ),'<a href="'+href+'" class="internt_link">'+pianist+'</a>');
showny = showreplace;
n++;
}
}


//koncertkalender ----------------
function koncert() {

$('.koncert_mere').html('vis mere...');
    
$(".koncert_head").toggle(function() {
    $(this).parent().children(".koncert_beskrivelse").slideDown(400);
    $(this).children(".koncert_mere").html('minimer...');
    //scrollpos = $(window).scrollTop();
  },function(){
    //$('html, body').delay('100').animate({ scrollTop: scrollpos }, 900);
    $(this).parent().children(".koncert_beskrivelse").slideUp(900);
    $(this).children(".koncert_mere").html('vis mere...');
      });

/*$('.koncert a[class!="internt_link"]').click(function(){
    window.location.href = $(this).attr('href');
    });*/
}

//pianist hover -------------------
function pianisterne() {
$(".pianisterne img").hover(function() {
  var navn = $(this).attr("alt");
  $("h2").html(navn);
  $("#menu a[title*='"+navn+"']").parent().addClass("active");
  $(this).css("border-color","#D45500");
},
function() {
  $("h2").html("se alle navne i menuen til venstre");
  $(".in li").removeClass("active");
  $(this).css("border-color","transparent");
}
);

$(".in a").hover(function() {
   var navn = $(this).attr("title");
   $('.pianisterne img[alt="'+navn+'"]').css("border-color","#D45500");
},
function() {
   $('.pianisterne img').css("border-color","transparent");
}
);
}

// galleri ------------------------------------
function galleri () {
$('.galleri_link').hover(function(){
 $(this).fadeTo('fast', 0.6);
},
function() {
 $(this).fadeTo('fast', 1);
});

var title = $('#content h2').html();
$('a.galleri_link').fancybox({
		'transitionIn'	:	'none',
		'transitionOut'	:	'none',
                'padding' : '0',
                'margin' : '50',
                'cyclic': true,
                'autoScale' : true,
                'title' : title,
                'titlePosition' : 'over',
                'changeFade' : 0,
                'changeSpeed' : 0
	});

}

// scs----------------------------
function scs () {
    $(document).ready(function(){
     /* slideshow settings -------- */
    var slide_size = parseFloat($(".scs-slide").css("width"))+parseFloat($(".scs-slide").css("padding-left"))+parseFloat($(".scs-slide").css("padding-right"));
    var slide_count = $("#scs-slider > div").length;
    var slider_size = slide_size*slide_count+1000;
    $("#scs-slider").css({width:+slider_size+'px'}); //sets slider width
    var pos1 = 0;
    var endpos = - slide_size*slide_count+slide_size;
    var freq = 9000;
    var speed = 500;
    var time_stop = 30000;  //ventetid efter tryk pзо&#65533; knap

    // slideshow -----------------

    slideshow = setTimeout (slider, 100);
    function slider() {
    var next = pos1;
    while (next>=endpos) {
        $("#scs-slider").animate({left:+next+'px'},speed).delay(freq);
        next=next-slide_size;
        }
        clearTimeout ( slideshow );
        slideshow = setTimeout (slider, 100);
        } //end slider
        
        // right ---------------------------
        $("#scs-next-button").click(function(){
            $("#scs-slider").stop(true,true);
            clearTimeout ( slideshow );
            var pos =  parseFloat($("#scs-slider").css("left"));
            var y = pos;
            var z = endpos;
            while (y>=endpos)
            {
                if (pos==z && pos>endpos)
                {
                    var newpos = pos - slide_size;
                    $("#scs-slider").animate({left:+newpos+'px'}, 'normal');
                    }
                    y=y-slide_size;
                    z=z+slide_size;
                    }
                    
                    slideshow = setTimeout (slider, time_stop);
                    return false; //prevents linking
                });
        
        
        // left ----------------------------
        $("#scs-prev-button").click(function(){
            $("#scs-slider").stop(true,true);
            clearTimeout ( slideshow );
            var pos =  parseFloat($("#scs-slider").css("left"));
            var y = pos;
            var z = pos1;
            while (y<=pos1)
            {
                if (pos==z && pos<pos1)
                {
                    var newpos = pos + slide_size;
                    $("#scs-slider").animate({left:+newpos+'px'}, 'normal');
                    }
                    y=y+slide_size;
                    z=z-slide_size;
                    }
                    
                    
                    slideshow = setTimeout (slider, time_stop);
                    return false;
                    });    

    });
}
/* END content functions -------------------------------------------------------------------------------------------------------------------*/


/* ---------------------------------------------------------------------------------------------------------------------------------------  */
$(document).ready(function(){
   ajax_loader(); 
   ajax_back_button();
   forside_link();

   //check hash on site load---
   var hash = window.location.hash.substring(1);
   if (hash)
   {
      $('#content').hide();
      loadcscript(hash);
      var parentclass = $('a[href="'+hash+'"]').closest('ul').attr('class');
            if (parentclass == 'in')
            {
            $('#menu li').removeClass("active");
            $('a[href="'+hash+'"]').closest('ul').parent().addClass("active");
            $('a[href="'+hash+'"]').parent().addClass("active");
            }
            else
            {
            $('#menu li').removeClass("active");
            $('a[href="'+hash+'"]').parent().addClass("active");
            }
   } //end if

$('a[href="http://pianorama.dk/"]').attr("href","/"); 


  //content scripts
internt_link();
scs();
pianisterne();
galleri();
koncert();


setTimeout("player()", 2500);
});


// load content function and apply javascript-------------------
function loadcscript (nyside){
    
       $.ajax({
  type: 'GET', 
  url: nyside,
  cache: true,
  context: document.body,
  success: function(html){
  var showny = $(html).find('#content').html();
   
        $("#content").html(showny).show();
 
         $('body').css('cursor', 'default');
         $('#menu').addClass("menu_on").removeClass("menu_off");

scs();
internt_link();
galleri();
pianisterne();
koncert(); 
     } // end callback
  });

$('html, body').delay('100').animate({ scrollTop: 0 }, 'normal');
 
}


// prevent link while loading
   $('.menu_off a').live('click', function(e){
   e.preventDefault();
    });

// load menu -------------------------
function ajax_loader () {
$('.menu_on a').live('click', function (e) {
  e.preventDefault();

  $('#menu').removeClass("menu_on").addClass("menu_off");
  nyside = $(this).attr('href');
  $('#content').fadeOut(120);
  $('body').css('cursor', 'wait');
  window.location.hash = nyside;
 
 loadcscript(nyside);
  
/* menu */
var parentclass = $(this).closest('ul').attr('class');
if (parentclass == 'in')
{
$('#menu .in li').removeClass("active");
}
else
{
$('#menu li').removeClass("active");
}
$(this).parent().addClass("active");
});
}

// load hashchange-------------------------
function ajax_back_button(){
$(window).bind('hashchange', function(event) {
  var side = window.location.hash.substring(1);
  if (side!=nyside && side!=='') //prevent double load
  {
  $('#menu').removeClass("menu_on").addClass("menu_off");
  $('#content').fadeOut(50);
  $('body').css('cursor', 'wait');
  
  
  loadcscript(side);

/* menu */
var parentclass = $('a[href="'+side+'"]').closest('ul').attr('class');
if (parentclass == 'in')
{
$('#menu li').removeClass("active");
$('a[href="'+side+'"]').closest('ul').parent().addClass("active");
$('a[href="'+side+'"]').parent().addClass("active");
}
else
{
$('#menu li').removeClass("active");
$('a[href="'+side+'"]').parent().addClass("active");
}
} // end if
else if (side==='')
{
window.location.href = '/';
}
});
}

// load -- internt link ---------------------------------
function internt_link() {
$('.internt_link').click(function (e) {
  e.preventDefault();
  $('#menu').removeClass("menu_on").addClass("menu_off");
  nyside = $(this).attr('href');
  $('#content').fadeOut(50);
  $('body').css('cursor', 'wait');

  window.location.hash = nyside;
  
  loadcscript(nyside);

/* menu */
var parentclass = $('#menu a[href="'+nyside+'"]').closest('ul').attr('class');
if (parentclass == 'in')
  {
  $('#menu li').removeClass("active");
  $('#menu a[href="'+nyside+'"]').parent().addClass("active").closest('ul').parent().addClass("active");
     }
else
{
$('#menu li').removeClass("active");
}
$('#menu a[href="'+nyside+'"]').parent().addClass("active");
});
}

// forside link - 
function forside_link() {
$('.forside_link').click(function (e) {
  e.preventDefault();
  $('#menu').removeClass("menu_on").addClass("menu_off");
  nyside = $(this).attr('href');
  $('#content').fadeOut(50);
  $('body').css('cursor', 'wait');

  window.location.hash = nyside;
  
  loadcscript(nyside);
$('#menu li').removeClass("active");
});
}

// jplayer  ------------------------------
function player_jplayer() {
var playlist_num = 0;
var play_this = $('#play_bibliotek li:eq('+playlist_num+')').attr("id");

$("#jquery_jplayer_1").jPlayer({
        ready: function () {
           $('#play_bibliotek li[id="'+play_this+'"]').addClass("active_bibliotek");
           $(this).jPlayer("setMedia", {
                    mp3: "assets/uploads/musik/"+play_this
            }).jPlayer("play");
        },
        ended: function () {
$('#play_bibliotek li').removeClass("active_bibliotek");
 $(this).jPlayer("setMedia", {
                mp3: "assets/uploads/musik/"+getnextplay()
            }).jPlayer("play");
        },
        swfPath: "assets/scripts/jQuery.jPlayer.2.0.0",
        supplied: "mp3"
    });

$('#play_bibliotek li').click(function(){
  $('#play_bibliotek li').removeClass("active_bibliotek");
  play_this = $(this).attr('id');
  $('#play_bibliotek li[id="'+play_this+'"]').addClass("active_bibliotek");
  $("#jquery_jplayer_1").jPlayer( "clearMedia" ).jPlayer("setMedia", {mp3: "assets/uploads/musik/"+play_this}).jPlayer("play");
});

function getnextplay () {
  playlist_num++;
  var nextplay = $('#play_bibliotek li:eq('+playlist_num+')').attr("id");
  $('#play_bibliotek li[id="'+nextplay+'"]').addClass("active_bibliotek");
  return nextplay;
}
}

// player --------------------------------------
function player() {
 $('#player').animate({bottom:"-300px"},1500);
 $('.vis_bibliotek').toggle(
     function(){
          $('#player').animate({ bottom: "0px" }, {duration: 'slow',easing: 'easeOutBounce'});   
          $(this).removeClass("bib_op").addClass("bib_ned");
      },
     function(){
      $('#player').animate({bottom: "-300px"}, 500);
      $(this).removeClass("bib_ned").addClass("bib_op");
   });
   
    $("#play_bibliotek").hover(function(e){
      $('.follow').css('top', e.clientY).css('left', e.clientX);
    });


       //flash support ---------
      if(swfobject.hasFlashPlayerVersion("1"))
      {
       //du har flash
      }
      else
      {
      $('#player').bind('mousemove', function(e){
        $('#flash').show().css({
           left:  (e.pageX + 20) + "px",
       top:   (e.pageY - 40) + "px"
        }).delay(7000).fadeOut();
         });
      }
     //-----
   
  player_jplayer();
   
 
}
