/*
 * common.js v1.3
 */


function initPage(){
  goToHash();
  getExMenuLines();
  scrollArrows();
  initBrowser();
////  fixLinks();
}

function resizePage(){
  resizeArrows();
  resizeBrowser();
}

function scrollPage(){
  scrollArrows();
}







function getRealLeft(el) {
  var p = 0;
  while (el != null) {
    p += el.offsetLeft;
    el = el.offsetParent;
  }
  return p;
}






function iePNGfix(ob){
	var ieBr = navigator.appVersion.split("MSIE")
	var version = parseFloat(ieBr[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)){
		// do the fix ...
		if (ob.style){
			// object
		} else {
			ob = document.getElementById(ob);
		}
		if(ob.fix == 1) {return;}
		ob.fix = 1;
		var src = ob.background;
		if (ob.src) {src = ob.src;}
		ob.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ src +"', sizingMethod='scale');";
		if (ob.src) {
		  ob.src = ".rs/e.gif";
		} else {
		  ob.background = "url(.rs/e.gif)";
		}
	}
}



function resizeArrows(){
  var ap = document.getElementById("arrowPrev");
  var an = document.getElementById("arrowNext");
  if (ap){
    ap.style.left = document.body.scrollLeft + 10;
  }
  if (an){
    an.style.left = document.body.scrollLeft + document.body.clientWidth - 60;
    if (document.body.scrollLeft + document.body.clientWidth >= document.body.scrollWidth){
      an.style.visibility = "hidden";
    } else {
      an.style.visibility = "visible";
    }
  }
  if (ap){
    if (document.body.scrollLeft == 0){
      ap.style.visibility = "hidden";
    } else {
      ap.style.visibility = "visible";
    }
  }
}
function scrollArrows(){
  resizeArrows();
}









var step = 5;
var tmo = false;
var target = 0;
var stepScroll = 5;

function arrowNextAction(){
  target = document.body.scrollLeft;
  var i = 1;
  var pg = 0;
  var b = 0;
  while( pg != null ){
    pg = document.getElementById("page"+(i++));
    b = getRealLeft(pg);
    if ( b > target ){
      target = b;
      break;
    }
  }
  if(b == 0) {
    target = document.body.scrollLeft + document.body.clientWidth;
  }
  moveLeft();
}
function moveLeft(){
  //window.status = ":: "+step + " :: " + target;
  clearTimeout(tmo);
  clearTimeout(tmos);
  var diff = target - document.body.scrollLeft;
  var d2 = diff / 2;
  if (diff > 0){
    if (step > diff) {step = diff;}
    var oScroll = document.body.scrollLeft;
    // window.scroll(document.body.scrollLeft + step,0);
    window.scrollBy(step,0);
    if (document.body.scrollLeft == oScroll){
      return;
    }
    scrollArrows();
    if (diff > d2) {
      step += 5;
    } else {
      step -= 5;
      if (step < 1) step = 1;
    }
    tmo = setTimeout('moveLeft();', 10);
  }
}

var tmos = false;
function arrowScrollCancel(){
  clearTimeout(tmos);
}
function arrowNextScrollAction(){
  clearTimeout(tmos);
  window.scrollBy(stepScroll,0);
  scrollArrows();
  tmos = setTimeout('arrowNextScrollAction();', 10);
}
function arrowPrevScrollAction(){
  clearTimeout(tmos);
  window.scrollBy(-stepScroll,0);
  scrollArrows();
  tmos = setTimeout('arrowPrevScrollAction();', 10);
}









function arrowPrevAction(){
  target = document.body.scrollLeft;
  var i = 1;
  var pg = 0;
  var b = 0;
  var c = 0;
  while( pg != null ){
    pg = document.getElementById("page"+(i++));
    b = getRealLeft(pg);
    if ( b >= target ){
      target = c;
      break;
    }
    c = b;
  }
  if(b == 0) {
    target = document.body.scrollLeft - document.body.clientWidth;
  }
  moveRight();
}
function moveRight(){
//  window.status = ":: "+step + " :: " + target;
  clearTimeout(tmo);
  clearTimeout(tmos);
  var diff = document.body.scrollLeft - target;
  var d2 = diff / 2;
  if (diff > 0){
    if (step > diff) {step = diff;}
    var oScroll = document.body.scrollLeft;
    // window.scroll(document.body.scrollLeft - step,0);
    window.scrollBy(-step,0);
    if (document.body.scrollLeft == oScroll){
      return;
    }
    scrollArrows();
    if (diff > d2) {
      step += 5;
    } else {
      step -= 5;
      if (step < 1) step = 1;
    }
    tmo = setTimeout('moveRight();', 10);
  }
}


function pScrollArrows(){
  scrollArrows();
  clearTimeout(tmo);
}
var keyblock = false;
function keyDown(event){
  if (keyblock) return;
  if (event.keyCode == 32){
    arrowNextAction();
  }
  if (event.keyCode == 37 || event.keyCode == 39){
    tmo = setTimeout('pScrollArrows();', 10);
  }
}










/*
 *
 *
 *
 */

function initBrowser(){
  var dv = document.getElementById("picBrowser");
  var mt = document.getElementById("pageTable");

  if (dv){
//    dv.style.width = document.body.scrollWidth;
    if (mt) {dv.style.width = mt.clientWidth;}

    var out = '';
    var allImages = 60;
    var images = 34;
    var i = 1;

    for(var k=0; k<allImages; k++){
      out += '<img vspace="12" src="images/p/img_' + i + '.jpg" onclick="openPic(\'images/img_' + i + '.jpg\', this)" style="cursor:pointer" width="80" height="80">';
      out += '<img vspace="12" src=".rs/shd.jpg" width="20" height="80">';
      i++;
      if (i > images) i = 1;
    }


//    out += '<div id="picBrowserPic" style="position:absolute;z-index:2000;left:480;top:0;width:100;height:100;">.</div>';

    dv.innerHTML += out;
  }
}

function resizeBrowser(){
  var dv = document.getElementById("picBrowser");
  var mt = document.getElementById("pageTable");
  if (dv){
//    dv.style.width = document.body.scrollWidth;
    if (mt) {dv.style.width = mt.clientWidth;}
  }
}



function openPic(img, ob){
  var dv = document.getElementById("picBrowserPic");
  dv.style.visibility = "visible";
  dv.style.left = ob.offsetLeft;
  dv.style.height = 575;
  dv.style.width = 80;
  var out = '<img style="border:solid #A18F7B 3px" onload="openPicAfter(this)" hspace="10" vspace="10" src="'+img+'" alt="Loading image, please wait...">';
  out += '<div id="picBrowserPicBlend" onmouseOut="closePic()" onMouseDown="closePic()" style="position:absolute;left:0;top:0;width:100%;height:100%;background:url(.rs/e.gif)"></div>';
  dv.innerHTML = out;
}
function openPicAfter(img){
  var dv = document.getElementById("picBrowserPic");
  dv.pic = img;
  dv.style.width = img.width + 26;
  if (img.height > 575){
    dv.style.height = img.height + 26;
  }
  var rEdge = document.body.scrollLeft + document.body.clientWidth;
  var imgRight = dv.offsetLeft + dv.offsetWidth;
  if (imgRight > rEdge){
    dv.style.left = rEdge - dv.offsetWidth;
  }
  var dvb = document.getElementById("picBrowserPicBlend");
  dvb.style.width = dv.style.width;
  dvb.style.height = dv.style.height;
}
function closePic(){
  var dv = document.getElementById("picBrowserPic");
  dv.style.visibility = "hidden";
  dv.pic.style.width = "100px";
  dv.pic.style.height = "100px";
  dv.style.width = "100px";
  dv.style.height = "100px";
  var dvb = document.getElementById("picBrowserPicBlend");
  dvb.style.width = "100px";
  dvb.style.height = "100px";
}






function goAnchor(name){
    if (name.indexOf("#") == 0) {
      name = name.substring(1);
    }
    var pg = document.getElementById(name);
    target = getRealLeft(pg);
    if (target < document.body.scrollLeft){
      moveRight();
    } else {
      moveLeft();
    }
}



function fixLinks(){
  var link = "";

  var dochref = document.location.href;

  if (document.location.hash != ""){
    dochref = dochref.substring(0, dochref.length - document.location.hash.length);
  }

  if (document.location.pathname == "/" || document.location.pathname == ""){
    dochref += "index.html";
  }

  for (var i=0; i< document.links.length; i++){ 
    link = document.links[i];

    if(link.hash != ""){
      if(dochref + link.hash == link.href){
        link.href = "javascript:goAnchor('"+ link.hash +"')";
      }
    }

  }

}


function goToHash(){
  if (document.location.hash != ""){
    goAnchor(document.location.hash);
  }
}



var exMenuLinesCount=0;
function getExMenuLineHTML(){
  document.write('<span id="exMenuLine'+(exMenuLinesCount++)+'">loading...</span>');
}
function getExMenuLines(){
	var ajx = ajaxGet( "reg/extmenuline.jsp?r="+Math.random() );
	ajx.onreadystatechange = function() {
		if (ajx.readyState == 4) {
			var res = ajx.responseText;
			// alert(res);
			var dv = 0;
			for(var i=0 ; ; i++){
				dv = document.getElementById("exMenuLine"+i);
				// alert(dv);
				if(dv == null) {
					fixLinks();
					return;
				}
				dv.innerHTML = res;
			}
		};
	}; ajx.send(null);
}


















function ajaxGet(url) {
	var areq = false;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		areq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		areq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	areq.open('GET', url, true);
	areq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	return areq;
}




function ajaxPost(url) {
	var areq = false;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		areq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		areq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	areq.open('POST', url, true);
	areq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	return areq;
}


