// JavaScript Document
/**
 * 	LICENSE
 * 	This program is free software; you can redistribute it and/or
 * 	modify it under the terms of the GNU General Public License (GPL)
 * 	WITHOUT ANY WARRANTY, AS IS. See the
 *	GNU General Public License for more details.
 *	To read the license please visit http://www.gnu.org/copyleft/gpl.html
 *	----------------------------------------------------------------------
 *	Original Author of file: Alexander Bolokhovetskyy
 *	Purpose of file:
 *	----------------------------------------------------------------------
 * 	Tested with IE 6, Opera 9, Firefox 2
 */

var uagent = navigator.userAgent.toLowerCase();
var is_ie = (uagent.indexOf('msie') != -1)
            && (uagent.indexOf('opera') == -1)
            && (uagent.indexOf('safari') == -1);
var hostname;
var btnDisabled = "";
var topBtnsImages = new Array(25);

/**
 * Rollover effect - change button style
 *
 * @id  string - id of button (event caller)
 *
 * @return  integer - 0 if succeeds
 *
 * @access  public
 */
function TopButtonMouseover(btnId) {
  if(!(btn = document.getElementById(btnId)))
    return 1;

  if(btn.className == "top_btn")
    btn.className = "top_btn_hover";

  return 0;
} // end of the 'TopButtonMouseover()' function


/**
 * Rollover effect - change button style
 *
 * @id  string - id of button (event caller)
 *
 * @return  integer - 0 if succeeds
 *
 * @access  public
 */
function TopButtonMouseout(btnId) {
  if(!(btn = document.getElementById(btnId)))
    return 1;

  if(btn.className == "top_btn_hover")
    btn.className = "top_btn";

  return 0;
} // end of the 'TopButtonMouseout()' function

function GetServerVar(varName) {
  varName = "" + varName;
  return document.forms.vars_from_server[varName].value;
}

/**
 * Event: Called when loaded 'vars_from_server' form
 * Initializing variables, arrived from server
 *
 * @return  integer - 0 if succeeds
 *
 * @access  public
 */
function OnServerVarsLoaded() {
  hostname = GetServerVar("site_root");
  btnDisabled = GetServerVar("disabled_button");
  return 0;
} // end of the 'OnServerVarsLoaded()' function


/**
 * Attach 'onmouseover' and 'onmouseout' events to buttons
 *
 * @return  integer 0 if succeeds
 *
 * @access  private
 */
function SetBtnRolloverEffect(btnId) {
  var btn = document.getElementById(btnId);
  if(is_ie) {
    btn.attachEvent("onmouseover", function() { TopButtonMouseover(btnId) } );
    btn.attachEvent("onmouseout", function() { TopButtonMouseout(btnId) } );
  }else{
    btn.addEventListener("mouseover", function() { TopButtonMouseover(btnId) }, false);
    btn.addEventListener("mouseout", function() { TopButtonMouseout(btnId) }, false);
  }
  return btn;
}  // end of the 'SetBtnRolloverEffect()' function


/**
 * Go to the page, referenced by link
 *
 * @link  string - referenced page
 * @id  string - id of button (event caller)
 *
 * @return  integer - 0 if succeeds
 *
 * @access  public
 */
function TopButtonClick(btnId, link) {
  var btn = document.getElementById(btnId)
  //click on disabled button - no reaction
  if(btn.className == "top_btn_disabled") return 0;

  document.location = "http://www.portfolio.com/" + link;

  return 0;
} // end of the 'TopButtonClick()' function


/**
 * Set the document linked to button
 *
 * @access  private
 */
function SetBtnLink(btnId, link) {
  var btn = document.getElementById(btnId);

  anchor = btn.getElementsByTagName("a")[0];
  if(!anchor) return 1;
  //if button is disabled, remove it's link
  if(btn.className == "top_btn_disabled") {
    parentDivArray = btn.getElementsByTagName("div");
    for(i=0; parentDivArray[i]; i++)
      if(parentDivArray[i].className == "top_btn_body") {
        parentDiv = parentDivArray[i];
        break;
      }
    //remove link
    parentDiv.innerHTML = anchor.innerHTML;
    return 0;
  }else
    anchor.href = link;

  /*Attach onclick() event. Such double-handling
  of mouse clicks via <a href='..'> and onclick() is used to avoid
  syntax constructions like "<a href='..'><div>Button name</div></a>"
  Why used anchors? To allow user right-click on navi. button and copy
  link location, open it in another tab etc.
  */
  if(is_ie) {
    btn.attachEvent("onclick", function() { TopButtonClick(btnId, link) } );
  }else{
    btn.addEventListener("click", function() { TopButtonClick(btnId, link) }, false);
  }

  return btn;
}  // end of the 'SetBtnLink()' function

/**
 * Set button style and behaviour to disabled
 *
 * @access  public
 */
function DisableButton(btnId) {
  var btn = document.getElementById(btnId);
  if(btn.className == "top_btn") btn.className = "top_btn_disabled";
  return btn;
}  // end of the 'DisableButton()' function


/**
 * Preload images for button
 *
 * @access  private
 */
function PreloadBtnImages(startIndex, btnState) {
  var imagesCat = "files/";
  topBtnsImages[startIndex] = new Image();
  topBtnsImages[startIndex].src = imagesCat + "btn-" + btnState + "-bg.gif";
  topBtnsImages[startIndex+1] = new Image();
  topBtnsImages[startIndex+1].src = imagesCat + "btn-" + btnState + "-right.gif";
  topBtnsImages[startIndex+2] = new Image();
  topBtnsImages[startIndex+2].src = imagesCat + "btn-" + btnState + "-left.gif";
  
  return startIndex;
} // end of the 'PreloadBtnImages()' function


/**
 * Preload images for top panel buttons
 *
 * @access  public
 */
function PreloadTopBtnPanelImages() {
  var index = 0;
  index = PreloadBtnImages(index, "act");
  index = PreloadBtnImages(index, "hover");
  index = PreloadBtnImages(index, "disabled");
} // end of the 'PreloadTopBtnPanelImages()' function


/**
 * Event: Called when loaded top navigation panel
 * Initialize top navigation panel
 *
 * @access  public
 */
function OnTopBtnPanelLoaded() {
  SetBtnRolloverEffect("top_btn1");
  SetBtnRolloverEffect("top_btn2");
  SetBtnRolloverEffect("top_btn3");
  SetBtnRolloverEffect("top_btn4");
  SetBtnRolloverEffect("top_btn5");

  DisableButton(btnDisabled);

  SetBtnLink("top_btn1", GetServerVar("top_btn1_lnk"));
  SetBtnLink("top_btn2", GetServerVar("top_btn2_lnk"));
  SetBtnLink("top_btn3", GetServerVar("top_btn3_lnk"));
  SetBtnLink("top_btn4", GetServerVar("top_btn4_lnk"));
  SetBtnLink("top_btn5", GetServerVar("top_btn5_lnk"));
  
} // end of the 'OnTopBtnPanelLoaded()' function


function OnSiteLogoLoaded() {
  var logo = document.getElementById("hdr_logo_container");
/*
  linklist = document.getElementsByTagName("a");
  for( i=0; linklist[i]; i++ )
    alert("");
*/
  if(btnDisabled != "top_btn1") logo.innerHTML = '<a href="' + hostname + '">'+logo.innerHTML+'</a>';
  
  return logo;
} // end of the 'OnSiteLogoLoaded()' function


function OnContentFieldLoaded() {

} // end of the 'OnContentFieldLoaded()' function


