document.write("<style>.mainCurtain, .mainPopup { display: none; }</style>");

$(function(){

  lang = window.location.pathname.split('/');
  baseUrl = 'http://www.implement.dk/'+ lang[1] +'/popup/';
    
  $('.main a[href^=/'+lang[1]+'/popup/]').click(function() {
    showPopup();
    return fetchAjax(this.href);
  });
  
  $('.main a[href^=/da/inspiration/cases.html]').click(function() {
    showPopup();
    return fetchAjax('kundecases.html');
  });  
  
  if(window.location.hash.length && window.location.hash.match('u=')) {
    showPopup();
  } else if(typeof popup != 'undefined') {
    var location = $('#location').val(window.location).val();
    window.location.hash = 'u='+ makeHash(location);
    showPopup();
  }
  
  $.historyInit(pageload);
});

function fetchAjax(link) {
	$.historyLoad("u="+ getHash(link));
	return false;
}

function getHash(link) {
  hash = link.replace(/^.*#/, '');
	hash = hash.replace(baseUrl,'');
  hash = hash.replace('.html','');
  hash = hash.replace('?','&');
	return hash;
}

function makeHash(link) {
	hash = link.replace(/#.*/, '');
	hash = hash.replace(baseUrl,'');
	hash = hash.replace('.html','');
  return hash;
}

function pageload(hash) {
	if(hash && hash.match('u=')) {
		lang = window.location.pathname.split('/');
		
	  hash = hash.split('u=')[1];
    $('.popupContentAjax').remove();
    $.ajax({
      type: "GET",
      url: "/services/ajax/ajax_popup.php",
      cache: false,
      data: "lang="+lang[1]+"&urlpart="+ hash,
      datatype: "html",
      success: function(html){
        $('.popupInner').html(html);
        popupLinks();
      }
    });
    return false;
	} else if(hash == 'closed' || hash == '') {
	  closePopup();
	}
}

function showPopup() {
  popupLinks();
  scrollHeight = document.documentElement.scrollTop + document.body.scrollTop;
  popupOffset = $(window).height()/2 + scrollHeight - 275;
  popupOffset = (popupOffset > scrollHeight) ? popupOffset : scrollHeight + 10;  
  $('.mainCurtain').height($(document).height()).css("opacity",".85").show().next().css("top", popupOffset).show();
  $('html').addClass((popupOffset != (scrollHeight + 10)) ? "curtain" : "curtain2");
  document.documentElement.scrollTop = scrollHeight;
}

function closePopup() {
  if($('.popupContent').length > 0) {
    scrollHeight = (typeof scrollHeight != 'undefined') ? scrollHeight : 0;
    $('.mainCurtain').hide().next().hide();
    $('html').removeClass("curtain");
    $('.popupHeader ul, .popupContentAjax, .popupSidebar').remove();
    document.documentElement.scrollTop = scrollHeight;
    window.location.hash = "closed";
  }
  return false;
}

function popupLinks() {
  // CLOSE POPUP
  $('.mainCurtain, .popupHeader .close').click(function() {
    return closePopup();
  });

  // TABS
  $('.popupHeader ul a').click(function() {
    $(this).parent().attr("class",function() {
      className = this.className;
      $(this).parent().attr("class",className);
    });
    $(this).blur();
    return fetchAjax(this.href);
  });

  // SIDEBAR MENU
  $('.popupSidebar a').click(function() {
    if(this.href.match("/popup/")) {
      $(this).addClass("active").parent().siblings("> a.active").children("a").removeClass("active");
      return fetchAjax(this.href);
    }
  });
  
  // SIDEBAR SELECTS
  $('.popupSidebar select').change(function() {
      fetchAjax($(this).blur().val());
  });
  
  
  $('.popupContentAjax a').click(function() {
    if(this.href.match("/popup/")) {
      if($(this).hasClass("next")) {
        $('.popupSidebar .active').removeClass("active").parent().next().children("a").addClass("active");
      }
      return fetchAjax(this.href);
    }
  });
}
