﻿var iFrames = new Array();
var activeApplication = -1;

window.onload = function()
{
 for(var i = 0; i < 3; i++)
 {
  iFrames[i] = document.createElement("iframe");
  iFrames[i].className = "applicationIFrame";
  document.body.appendChild(iFrames[i]);
 }

 var isSecure = document.location.protocol == "https:";

 var search = location.search.replace(/^\?/gi, "");
 searches = search.split("&");
 var search = [];
 for(var i = 0; i < searches.length; i++)
 {
  var searchItem = searches[i].split("=");
  search.push(searchItem[0]);
  search.push(searchItem[1]);
 };


 var page = null;
 var sertificateWrong = null;
 var idParameter = null;
 var confirmationCode = null;

 var pageMapping = [["/", (document.location.protocol == "https:" ? 1 : 2)], ["/authorization.html", 1], ["/index.html", 2], ["/blog.html", 3], ["/gallery.html", 4], ["/contact.html", 5]];

 var path = location.pathname;

 for(var i = 0; i < pageMapping.length; i++)
  if(path == pageMapping[i][0])
  {
   page = pageMapping[i][1];
   break;
  };

 for(var i = 0; i < search.length; i += 2)
  switch(search[i].replace(/^.*&/gi, ""))
  {
   case "page":
    page = search[i+1];
    break;

   case "sertificateWrong":
    sertificateWrong = parseInt(search[i+1]);
    break;

   case "id":
    idParameter = search[i+1];
    break;

   case "confirmation_code":
    confirmationCode = search[i+1];
    break;
  };

 var application = page == null ? 0 : 2;

 var parameters = [idParameter, confirmationCode];

 SetState([application, isSecure, page, sertificateWrong, parameters]);
}

function SetState(state)
{
 for(var i = 0; i < iFrames.length; i++)
  iFrames[i].style.display = "none";

 if(activeApplication != -1)
 {
  iFrames[activeApplication].contentWindow.document.write("hello");
  iFrames[activeApplication].contentWindow.document.location = "about:blank";
 }

 //var webServiceAbsolutePathUrl = document.location.href.replace(/Client\/Index\.html.*$/gi, "");
 //if(document.location.protocol == "file:")
 // webServiceAbsolutePathUrl = "http://cserver.cyborg.su/c_dev03/";

 var location = document.location;

 var webServiceAbsolutePathUrl = location.protocol + "//" + location.hostname + "/";

 var applicationsUrls = ["personal/index.html", "public/ajax/Index.html", "public/flash/index.html"];

 document.getElementById("searchContentContainer").style.display = state[0] == 0 ? "none" : "";

 iFrames[state[0]].style.display = "";

 self.activeIFrame = iFrames[state[0]];
 var activeWindow = self.activeIFrame.contentWindow;

 var onApllicationLoaded = function(event)
 {
  
  switch(event[0])
  {
   //Exit
   case 0:
    //alert("выход из приложения = " + activeApplication);
    break;
    //ChangeApplication
   case 1:
    //alert("Переключаемся в приложение = " + event[1] + ", сессия = " + event[2]);
    var sessionData = null;
    if(event[2] && (event[2][0] || event[2][1]))
     sessionData = [event[2][0], event[2][1], event[2][2] ? event[2][2] : undefined, event[2][3] != undefined ? event[2][3] : undefined, event[2][4] != undefined ? event[2][4] : undefined, event[2][5] != undefined ? event[2][5] : undefined];

    //alert("sessionData = " + sessionData);
    SetState([event[1], sessionData]);
    break;

   //ChangeProtocol
   case 2:
    //alert("Переключаемся в приложение = " + event[1] + ", сессия = " + event[2]);
    switch(event[1])
    {
     //simpleMode
     case 0:
      var location = self.document.location
      var search = location.search.replace(/^\?/gi, "");
      search = search.replace(/sertificateWrong=\d*/gi, "");
      if(event[2] && event[2][1])
       search = "?" + (search == "" ? "" : (search + "&")) + event[2][1];

      var href = "http://" + location.hostname.replace(/^prv\./gi, "") + location.pathname + search + location.hash;

      self.document.location = href;
      break;

     //secureMode
     case 1:
      self.document.location = self.document.location.href.replace(/(^http:\/\/prv.)|(^http:\/\/www.)|(^http:\/\/)/, "https://prv.");
      break;
    }
    break;
  }
 }

 var onLoad = function()
 {
  if(!activeWindow.$Application)
   return;

  activeWindow.$Application.applicationPage = state[2];
  activeWindow.$Application.sertificateWrong = state[3];

  activeWindow.$Application.parameters = state[4];

  activeWindow.$Application.onEvent = onApllicationLoaded;

  activeWindow.$Application.prevSession = state[1];
 };

 iFrames[state[0]].onreadystatechange = function()
 {
  if(iFrames[state[0]] && iFrames[state[0]].readyState == "complete")
   onLoad();
 }

 iFrames[state[0]].onload = function()
 {
  onLoad();
 }

 activeApplication = state[0];

 var loc;
 loc = webServiceAbsolutePathUrl + applicationsUrls[state[0]];

 activeWindow.document.location = loc;//applicationsUrls[state[0]];
}

