﻿var SwIP = new function()
{
  this.wB = null;
  this.wBP = null;
  this.wP = null;
  this.url = null;
  this.settings = null;
  this.gallery = null;
  this.current = null;
  
  this.Show = function(e, url, settings)
  {
    SwIP.url = url;
    SwIP.settings = settings;
    if(!SwIP.wB) SwIP.wB = SwIP.CreateBlockerPopupElement(); 
    if(!SwIP.wBP) SwIP.wBP = SwIP.CreateProgressElement(); 
    if(!SwIP.wP) SwIP.wP = SwIP.CreateImgPopupElement();    
    
    SwIP.ShowBlocker();
    SwIP.ShowProgress();
    SwIP.ShowPopup();
    
    e = e || window.event;    
    e.cancelBubble = true;
    e.returnValue = false;
    if(e.stopPropagation) e.stopPropagation();
    if(e.preventDefault) e.preventDefault();
  }
  
  this.ShowGallery = function(e, gallery, current)
  {
    SwIP.gallery = gallery;
    SwIP.current = current;
    SwIP.Show(e, gallery[current].url, gallery[current].settings);
  }
  
  this.CreateBlockerPopupElement = function()
  {
    var wBlocker = document.createElement("div");
    wBlocker.style.display = "none";
    wBlocker.style.position = "absolute";
    wBlocker.style.zIndex = "100";
    wBlocker.style.opacity = "0.5";
    wBlocker.style.filter = "filter: alpha(opacity=50)";
    wBlocker.style.backgroundColor = "black";
    // wBlocker.style.backgroundImage = "url(img/progressbar.gif)";
    wBlocker.style.backgroundPosition = "center center";
    wBlocker.style.backgroundRepeat = "no-repeat";
    wBlocker.onclick = SwIP.Hide;
    document.body.appendChild(wBlocker);
    
    return wBlocker;
  }

  this.CreateProgressElement = function()
  {
    var wProgress = document.createElement("img");
    wProgress.style.display = "none";
    wProgress.style.position = "absolute";
    wProgress.style.zIndex = "101";
    wProgress.src = "img/progressbar.gif";
    document.body.appendChild(wProgress);
    
    return wProgress;
  }
  
  this.CreateImgPopupElement = function()
  {
    var wPopup = document.createElement("div");
    wPopup.id = "swi_popup";
    wPopup.style.position = "absolute";
    wPopup.style.zIndex = "102";
    if(SwIP.gallery) wPopup.onclick = SwIP.Next;
      else wPopup.onclick = SwIP.Hide;
    document.body.appendChild(wPopup);
    
    return wPopup;
  }
  
  this.ShowBlocker = function()
  {
    SwIP.wB.style.left=document.documentElement.scrollLeft + "px";
    SwIP.wB.style.top=document.documentElement.scrollTop + "px";
    SwIP.wB.style.width=document.documentElement.clientWidth + "px";
    SwIP.wB.style.height=document.documentElement.clientHeight + "px";
    SwIP.wB.style.display="block";
  }

  this.ShowProgress = function()
  {
    SwIP.wBP.style.left=(document.documentElement.scrollLeft + (document.documentElement.clientWidth / 2) - 122) + "px";
    SwIP.wBP.style.top=(document.documentElement.scrollTop + (document.documentElement.clientHeight / 2) - 8) + "px";
    //SwIP.wBP.style.width=document.documentElement.clientWidth + "px";
    //SwIP.wBP.style.height=document.documentElement.clientHeight + "px";
    SwIP.wBP.style.display="block";
  }
  
  this.ShowPopup = function()
  {
    SwIP.wP.style.visibility = "hidden";
    SwIP.wP.style.display="block";
    SwIP.wP.style.left=((document.documentElement.scrollLeft + (document.documentElement.clientWidth/2))-(SwIP.wP.offsetWidth/2)) + "px";
    SwIP.wP.style.top=((document.documentElement.scrollTop + (document.documentElement.clientHeight/2))-(SwIP.wP.offsetHeight/2)) + "px";
    SwIP.wP.style.width = "1px";
    SwIP.wP.style.height = "1px";
    SwIP.wP.style.overflow = "hidden";
    SwIP.wP.style.zIndex=101;
    //if(SwIP.settings && SwIP.settings.border) SwIP.wP.style.border = SwIP.settings.border;
    var innerHTML = "";
    if(SwIP.settings && SwIP.settings.showTitleBar)
    {
      innerHTML += "<div id=\"swi_topbar\"><div" + (SwIP.settings.border ? " style=\"margin-left: " + SwIP.settings.borderSize + "px; margin-right: " + SwIP.settings.borderSize + "px\"" : "") + ">";
      if(SwIP.settings.titlePosition == "top") innerHTML += "<div id=\"swi_title\">" + SwIP.settings.title + "</div>"
      if(SwIP.settings.descriptionPosition == "top") innerHTML += "<div id=\"swi_description\">" + SwIP.settings.description + "</div>";
      innerHTML += "</div></div>";
    }
    
    if(SwIP.settings)
    {
      innerHTML += "<div id=\"swi_divimg\" " + (SwIP.settings.border ? "style=\"border: " + SwIP.settings.border + "\"" : "") + "><img id=\"swi_img\" src=\"" + SwIP.url + "\" onload=\"SwIP.Recalc();\" style=\"cursor: pointer;\" /></div>";
    } else {
      innerHTML += "<div id=\"swi_divimg\"><img id=\"swi_img\" src=\"" + SwIP.url + "\" onload=\"SwIP.Recalc();\" style=\"cursor: pointer;\" /></div>";
    }
    
    if(SwIP.settings && SwIP.settings.showBottomBar)
    {
      innerHTML += "<div id=\"swi_bottombar\"><div" + (SwIP.settings.border ? " style=\"margin-left: " + SwIP.settings.borderSize + "px; margin-right: " + SwIP.settings.borderSize + "px;\"" : "") + ">";
      if((SwIP.settings.titlePosition == "bottom") || (!SwIP.settings.titlePosition)) innerHTML += "<div id=\"swi_title\">" + SwIP.settings.title + "</div>"
      if((SwIP.settings.descriptionPosition == "bottom") || (!SwIP.settings.descriptionPosition)) innerHTML += "<div id=\"swi_description\">" + SwIP.settings.description + "</div>";
      innerHTML += "</div></div>";
    }
    SwIP.wP.innerHTML = innerHTML;
  }
  
  this.Recalc = function(recursed)
  {
    var elm = document.getElementById('swi_img');
    var divimg = document.getElementById('swi_divimg');
    var topbar = document.getElementById('swi_topbar');
    var bottombar = document.getElementById('swi_bottombar');
    var topbarheight = 0;
    var bottombarheight = 0;
    var bordersize = 0;
    if(topbar) topbarheight = topbar.offsetHeight;
    if(bottombar) bottombarheight = bottombar.offsetHeight;
    if(SwIP.settings && SwIP.settings.borderSize) bordersize += SwIP.settings.borderSize;
    var winw = document.documentElement.clientWidth - topbarheight - bottombarheight - (bordersize * 2) - 50;
    var winh = document.documentElement.clientHeight - (bordersize * 2) - 50;
    if(elm.width>winw)
    {
      elm.height = elm.height / (elm.width / (winw - 50));
      elm.width = winw - 50;
    }
    if(elm.height>winh)
    {
      elm.width = elm.width / (elm.height / (winh - 50));
      elm.height = winh - 20;
    }
    SwIP.wP.style.width = (elm.width + (bordersize * 2)) + "px";
    SwIP.wP.style.height = (elm.height + topbarheight + bottombarheight + (bordersize * 2)) + "px";
    SwIP.wP.style.left=((document.documentElement.scrollLeft + (document.documentElement.clientWidth/2))-(SwIP.wP.offsetWidth/2)) + "px";
    SwIP.wP.style.top=((document.documentElement.scrollTop + (document.documentElement.clientHeight/2))-(SwIP.wP.offsetHeight/2)) + "px";
    if(bottombar && !recursed) SwIP.Recalc(true);
    SwIP.wP.style.visibility = 'visible';
    this.wBP.style.display="none";
  }
  
  this.Hide = function()
  {
    SwIP.wP.style.display="none";
    SwIP.wBP.style.display="none";
    SwIP.wB.style.display="none";
  }
  
  this.Next = function(e)
  {
    SwIP.Hide();
    if(SwIP.current < SwIP.gallery.length - 1)
    {
      SwIP.current++;
      SwIP.Show(e, SwIP.gallery[SwIP.current].url, SwIP.gallery[SwIP.current].settings);
    }
  }
}

var SwPL = new function()
{
  this.wB = null;
  this.wP = null;
  this.plid = null;
  this.noclose = false;
  this.type = 'popup';
  
  this.Show = function(e, plid, type, noclose)
  {
    if(noclose) this.noclose = noclose;
    if(type) this.type = type;
    SwPL.plid = plid;
    if(!SwPL.wB) SwPL.wB = SwPL.CreateBlockerElement(); 
    if(!SwPL.wP) SwPL.wP = SwPL.CreatePopupElement();    
    
    SwPL.ShowBlocker();

    e = e || window.event;    
    e.cancelBubble = true;
    e.returnValue = false;
    if(SwPL.type == "popup") SwPL.ShowPopup();
    if(SwPL.type == "async") SwPL.LoadPopup();
    
    return false;
  }
  
  this.CreateBlockerElement = function()
  {
    var wBlocker = document.createElement("div");
    wBlocker.style.display = "none";
    wBlocker.style.position = "absolute";
    wBlocker.style.zIndex = "90";
    wBlocker.style.opacity = "0.5";
    wBlocker.style.filter = "filter: alpha(opacity=50)";
    wBlocker.style.backgroundColor = "black";
    wBlocker.style.backgroundPosition = "center center";
    wBlocker.style.backgroundRepeat = "no-repeat";
    wBlocker.onclick = SwPL.Hide;
    document.body.appendChild(wBlocker);
    
    return wBlocker;
  }
  
  this.CreatePopupElement = function()
  {
    var wPopup = document.createElement("div");
    wPopup.style.position = "absolute";
    wPopup.style.zIndex = "101";
    if(!SwPL.noclose) wPopup.onclick = SwPL.Hide;
    if(SwPL.type == 'formpopup') document.getElementById('aspnetForm').appendChild(wPopup);
    else document.body.appendChild(wPopup);
    
    return wPopup;
  }
  
  this.ShowBlocker = function()
  {
    SwPL.wB.style.left=document.documentElement.scrollLeft + "px";
    SwPL.wB.style.top=document.documentElement.scrollTop + "px";
    SwPL.wB.style.width=document.documentElement.clientWidth + "px";
    SwPL.wB.style.height=document.documentElement.clientHeight + "px";
    SwPL.wB.style.display="block";
  }
  
  this.ShowPopup = function()
  {
    SwPL.wP.innerHTML = document.getElementById(SwPL.plid).innerHTML; 
    SwPL.wP.style.display="block";
    SwPL.wP.style.left=((document.documentElement.scrollLeft + (document.documentElement.clientWidth/2))-(SwPL.wP.offsetWidth/2)) + "px";
    SwPL.wP.style.top=((document.documentElement.scrollTop + (document.documentElement.clientHeight/2))-(SwPL.wP.offsetHeight/2)) + "px";
    SwPL.wP.style.overflow = "hidden";
    SwPL.wP.style.zIndex=91;
  }
  
  this.LoadPopup = function()
  {
    SwPL.wP.innerHTML = "<div style=\"background-color: white;\"><img src=\"/img/progressbar.gif\" /></div>";
    
    new Ajax.Request("/shared/sharedhtml.aspx?text=" + encodeURIComponent(SwPL.plid), 
    {
        onSuccess : function(resp)
          {          
            SwPL.UpdateAsyncPopup(resp);              
          },
        onFailure : function(resp) 
          {
            SwPL.Hide();
          }
    });
    
    SwPL.wP.style.display="block";
    SwPL.wP.style.left=((document.documentElement.scrollLeft + (document.documentElement.clientWidth/2))-(SwPL.wP.offsetWidth/2)) + "px";
    SwPL.wP.style.top=((document.documentElement.scrollTop + (document.documentElement.clientHeight/2))-(SwPL.wP.offsetHeight/2)) + "px";
    SwPL.wP.style.overflow = "hidden";
    SwPL.wP.style.zIndex=91;
  }
  
  this.UpdateAsyncPopup = function(resp)
  {
    SwPL.wP.innerHTML = resp.responseText;
    SwPL.wP.style.left=((document.documentElement.scrollLeft + (document.documentElement.clientWidth/2))-(SwPL.wP.offsetWidth/2)) + "px";
    SwPL.wP.style.top=((document.documentElement.scrollTop + (document.documentElement.clientHeight/2))-(SwPL.wP.offsetHeight/2)) + "px";
  }
  
  this.Hide = function()
  {
    SwPL.wP.style.display="none";
    SwPL.wB.style.display="none";
  }    
}

// Progress preloading
var progressImage = new Image(); 
progressImage.src = "img/progressbar.gif";

// Copy protection
function InitCopyProtection()
{
  if(!document.getElementsByTagName('body')[0])
  {
    window.setTimeout(InitCopyProtection, 100);
    return;
  }
  document.onkeydown = function(ev) {
   var e = ev || event;
   if(e.ctrlKey && (e.keyCode == '67'))
   {
      document.getElementsByTagName('body')[0].innerHTML = '<h1>This page is copy protected!!!</h1>';
      e.returnValue=false;
      window.clipboardData.setData('text', 'No copy!');
      return false;
   }
  }
  var body = document.getElementsByTagName('body')[0];
  body.onselectstart = function(ev) { 
    var e = ev || event;
    e.returnValue = false;
    return false;
  };
  body.onmousedown = function(ev) {
    var e = ev || event;
    e.returnValue = false;
    return false;
  };
  body.onmouseup = function(ev) {
    var e = ev || event;
    e.returnValue = false;
    return false;
  };
  body.oncontextmenu = function(ev) {
    var e = ev || event;
    var elm = e.target || e.srcElement;
    if(elm.tagName != 'IMG')
    {
      e.returnValue = false;
      return false;
    }
  };
}

//InitCopyProtection();
//document.body.onselectstart = function { alert('Co to delas?'); }

