var supersizedImages = [];

function resizeElements() {

 correction = window.location.hostname.toLowerCase() == 'indspark.swiftmedia.dk' || window.location.hostname.toLowerCase() == 'indspark.dk' ? 20 : 20;
 $('.skin-logo').css("right", ($(window).width()-$('.skin-navigation').width()-240)/2);
 $('.skin-languages').css("right", (($(window).width()-$('.skin-navigation').width()-240)/2)+250);
 $('.skin-navigation').css("right", (($(window).width()-$('.skin-navigation').width()-240)/2)+240);
 $('.skin-navigation-sub').css("right", (($(window).width()-$('.skin-navigation').width()-240)/2)+240);
 $('.skin-content').css("left", (($(window).width()-$('.skin-navigation').width()-240)/2)+(($('.skin-navigation').width()-$('.skin-content').width())/2)+correction);
 $('.skin-content').css("top", ((($(window).height()-142-440)/2)+142 < 150 ? 150 : (($(window).height()-142-440)/2)+142));

}

$(function() {

  if(location.pathname != '/' && location.pathname.lastIndexOf('/#', 0) != 0 && location.pathname.lastIndexOf('/#!', 0) != 0) {
   location.href = '/#!'+location.pathname+(window.location.search.substring(1) != '' ? '?'+window.location.search.substring(1) : '');
  }

  if(window.location.hash.match(/#!\/.+\/.+/)) {
   path = window.location.hash.replace(/#!\//, '');
   loadNavigation(0, null, path, true);
  }

   $.fn.supersized.options = {  
    startwidth: 900,  
    startheight: 506,
    vertical_center: 1,
    slideshow: 1,
    navigation: 0,
    thumbnail_navigation: 0,
    transition: 0,
    pause_hover: 0,
    slide_counter: 0,
    slide_captions: 0,
    slide_interval: 15000,
    slides : supersizedImages
  };
  if(supersizedImages.length > 1)
   $('#supersized').supersized(); 

  $.address.init(function() {
   $('.skin-navigation a.nonsub').address();           
  }).change(function(event) {
   mainPath = event.path.split("/")[1];
   $('.skin-navigation a').each(function() {
    if($(this).attr('href').toLowerCase() == event.path.toLowerCase() || $(this).attr('href').toLowerCase() == event.path.toLowerCase()+'/') {
     $(this).addClass('selected').focus();
    } else {
     $(this).removeClass('selected');
    }
   });
   $('.skin-navigation-sub a').each(function() {
    if($(this).attr('href').toLowerCase() == event.path.toLowerCase()) {
     $(this).addClass('selected').focus();
    } else {
     $(this).removeClass('selected');
    }
   });
   var handler = function(data) {

    jQuery.each(jQuery.browser, function(i) {
     if($.browser.msie){
      $('.skin-content').html(data.content);
      $('a.ajax').address();
      $('a.lightbox').lightBox({imageLoading: '/images/lightbox/loading.gif', imageBtnClose: '/images/lightbox/close.gif', imageBtnPrev: '/images/lightbox/prev.gif', imageBtnNext: '/images/lightbox/next.gif', imageBlank: '/images/lightbox/blank.gif', txtImage: 'Billede', txtOf: 'af i alt '});
     }else{
      $('.skin-content').fadeOut(400, function() {
       $('.skin-content').html(data.content);
       $('a.ajax').address();
       $('a.lightbox').lightBox({imageLoading: '/images/lightbox/loading.gif', imageBtnClose: '/images/lightbox/close.gif', imageBtnPrev: '/images/lightbox/prev.gif', imageBtnNext: '/images/lightbox/next.gif', imageBlank: '/images/lightbox/blank.gif', txtImage: 'Billede', txtOf: 'af i alt ', fixedNavigation: true});
       $('.skin-content').fadeIn();
      });
     }

     $.address.title(data.title);
     $('#flag_dk').attr('href', data.flag_dk);
     $('#flag_fo').attr('href', data.flag_fo);

    });
   
   };

   jQuery.each(jQuery.browser, function(i) {
    if($.browser.msie){
     $('.skin-content').html('<div class="loading">Vent venligst..</div>');
     $('.skin-content').show();
     myregexp = new RegExp('[^?]+');
     myPage = event.value.replace(myregexp, '').replace('\?page=', '');
     myData = '';
     if(myPage != '') myData = {page: myPage};
     ajaxUrl = '/ajax/content.json?path='+event.path;
     $.ajax({type: 'POST', url: ajaxUrl, data: myData, dataType: 'json',
             error: function(XMLHttpRequest, textStatus, errorThrown) {
              //handler(XMLHttpRequest.responseText);
             },
             success: function(data, textStatus, XMLHttpRequest) {
              handler(data);
             }
     });
    }else{
     $('.skin-content').fadeOut(200, function() {
      $('.skin-content').html('<div class="loading">Vent venligst..</div>');
      $('.skin-content').fadeIn(200, function() {
       myregexp = new RegExp('[^?]+');
       myPage = event.value.replace(myregexp, '').replace('\?page=', '');
       myData = '';
       if(myPage != '') myData = {page: myPage};
       ajaxUrl = '/ajax/content.json?path='+event.path;
       $.ajax({type: 'POST', url: ajaxUrl, data: myData, dataType: 'json',
               error: function(XMLHttpRequest, textStatus, errorThrown) {
                //handler(XMLHttpRequest.responseText);
               },
               success: function(data, textStatus, XMLHttpRequest) {
                handler(data);
               }
       });
      }); 
     });
    }
   });

  });

  $(window).bind("resize", function(){
  
   resizeElements();

  });

});

var navigationSubActive = false;
function loadNavigation(pageId, linkElement, pagePath, checkOnLoad) {

 checkOnLoad = typeof(checkOnLoad) != 'undefined' ? checkOnLoad : false; 

 $('.skin-navigation a').removeClass('selected');
 $(linkElement).addClass('selected');
 if(pageId != 0)
  ajaxUrl = '/ajax/navigation.json?pid='+pageId;
 else
  ajaxUrl = '/ajax/navigation.json?path='+pagePath;
 $.ajax({type: 'POST', url: ajaxUrl, data: {}, dataType: 'json',
   error: function(XMLHttpRequest, textStatus, errorThrown) {
  },
  success: function(data, textStatus, XMLHttpRequest) {
   if(navigationSubActive) {
    $('.skin-navigation-sub').fadeOut(500, function() {
     $('.skin-navigation-sub').html(data.content);
     $('.skin-navigation-sub a').address();
     $('.skin-navigation-sub').fadeIn();
    });
   }else{
    $('.skin-navigation-sub').css('display', 'block');
    $('.skin-navigation-sub').hide();
    $('.skin-navigation-sub').html(data.content);
    $('.skin-navigation-sub a').address();
    $('.skin-navigation').animate({
      top: '-=20'
     }, 500, function() {
      $('.skin-navigation-sub').fadeIn();
    });
   }
   if(checkOnLoad) {
    $('.skin-navigation-sub a').each(function() {
     path = window.location.hash.replace(/#!/, '');
     if($(this).attr('href').toLowerCase() == path.toLowerCase()) {
      $(this).addClass('selected').focus();
     } else {
      $(this).removeClass('selected');
     }
    });
   }
   navigationSubActive = true;
  }

 });

}

function unloadNavigation() {

 if(navigationSubActive) {
  $('.skin-navigation-sub').fadeOut(500, function() {
   $('.skin-navigation').animate({top: '+=20'}, 500);
  });
 }
 navigationSubActive = false;

}

$(document).ready(function() {
  
 resizeElements();

});
