urlroot = window.location.host.indexOf('idium.n') !== -1 ? window.location.pathname.substring(0,window.location.pathname.indexOf('/',2)) + "/" : "/";

$(document).ready(function() {

  if($("div.users").size() === 1) {
    if(ips.system.userId === "") {
      $("div.users").load(urlroot + "?module=Users;action=UserGroup.publicGetUsersInGroup;template=ajaxUsers;id=" + ips.system.usergroupId);
    } else {
      $("#content").load(urlroot + "?module=Users;action=User.publicOpen;template=ajaxUsers;id=" + ips.system.userId);
    }
  }

  // SEARCH
  $("a.paginator").live("click", function(e){
    e.preventDefault();
    $(this).closest(".results").load($(this).attr("href"));
  });

  if($(".results").size() === 1) {
    $(".publicSearch").load(urlroot + "?module=Search;action=publicSearch;qtype=Articles.Article;_ips_encoding=utf-8;pagesize=10;template=intraSearchResults;qroot=39;query=" + ips.system.query + ";index=1");
  }

  if($(".results").size() > 1) {
    $(".resultArticles").load(ips.system.urlroot + "?module=Search;action=publicSearch;qtype=Articles.Article;_ips_encoding=utf-8;pagesize=5;template=intraSearchResults;qroot=70;query=" + ips.system.query + ";index=1");
    $(".resultFiles").load(ips.system.urlroot + "?module=Search;action=publicSearch;qtype=Files.File;_ips_encoding=utf-8;pagesize=5;template=intraSearchResults;qroot=171;query=" + ips.system.query + ";index=1");
    $(".resultUsers").load(ips.system.urlroot + "?module=Search;action=publicSearch;qtype=Users.User;_ips_encoding=utf-8;pagesize=5;template=intraSearchResults;qroot=16;query=" + ips.system.query + ";index=1");
  }

  // LOAD FAVOURITES
  if($("#favourites").size() === 1) {
    $("#favourites").load("./?template=ajaxFavourites"); 
  }

  // ADD FAVOURITE
  $("a.favourite").live("click", function(e){
    e.preventDefault();
    $.get($(this).attr("href") + ";template=xml", function(){
       $("#favourites").load("./?template=ajaxFavourites");
    });
  });

  var initSetup = true;
  function closeViewerWindow() {
    $("#overlay,#zmagReader").remove();
    initSetup = true;
  }

  $('a.lightboxImage').lightBox();
  //$('.christmas a').lightBox();
  $("a.newArticle").bind("click", function(e){
    var theURL = $(this).attr("href") +";get=content";
    e.preventDefault();
    loadLightbox(theURL);
    return false;       
  });
  function submitHandler($elem) {
    $elem.find("form").submit(function(e){
      e.preventDefault();
      var $this = $(this);
      $.ajax({
         type : "post",
         url : $this.attr("action") + ";get=content",
         data : $this.serialize(),
         error : function(){},
         success : function(data){
           $elem.html(data);
           submitHandler($elem);
         }
      });
    });
  }
  function loadLightbox(theURL) {
    if (initSetup) {
      initSetup = false;
      $("<div id='overlay' class='clearfix'></div><div id='zmagReader'><div class='wrapper'><a href='#' class='close'>(Lukk vindu)</a><div id='myViewerContent' style='width:100%; height:100%;'></div><a href='#' class='close'>(Lukk vindu)</a></div></div>").insertBefore("#wrapper").hide().slideDown(function(){
        $("#myViewerContent").load(theURL,function(){
          submitHandler($(this));
        });
      });
      $("#overlay").animate({opacity:0.5});
      $(document).keyup(function(event){
        if (event.keyCode == 27) {
          $(document).unbind("keyup");
          closeViewerWindow();
        }
      });
      $(".close").click(function(){
        closeViewerWindow();
      });
      $("#overlay,#zmagReader").click(function(e){
        if (e.target == $("#zmagReader").get()[0]) {
          closeViewerWindow();
        } 
      }); 
    } else {           
      $("#overlay,#zmagReader").slideDown(function(){}).click(function(){
        if (e.target == $("#zmagReader").get()[0]) {
          closeViewerWindow();
        } 
      });   
    }
  }
});

var player;

function focusPlayer(obj) {
  var id = obj['id'].substring(4);
  
  var windowHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
  $("#lightbox").css("height", windowHeight + 60 + "px");
  $("#lightbox").css("width", document.body.offsetWidth + "px");
  $("#lightbox").fadeIn("10000");
}
function unfocusPlayer(obj) {
  var id = obj['id'].substring(4);
  $("#lightbox").fadeOut("fast");
}
function playerState(obj) {
  var state = obj.newstate;
  if(state == "PAUSED" || state == "COMPLETED") {
    unfocusPlayer(obj);
  } else if(state == "PLAYING") {
    focusPlayer(obj); 
  }
}



function playerReady(obj) {
  var id = obj['id'];
  var version = obj['version'];
  var client = obj['client'];
  player = document.getElementById(id);

  player.addModelListener("STATE", "playerState");
  player.addViewListener('STOP', 'unfocusPlayer');
}