var photo_number = new Array(0, 0, 0);
var photo_number_offset = new Array(0, 0, 0);
var cat_offset = new Array();
var already_scroll = false;


function loadHTML(URL, destination){
var request;
var dest;

function processStateChange(){
    if (request.readyState == 4){
        contentDiv = document.getElementById(dest);
        if (request.status == 200){
            response = request.responseText;
            contentDiv.innerHTML = response;
        } else {
            contentDiv.innerHTML = "Error: Status "+request.status;
        }
    }
}
    dest = destination;
    if (window.XMLHttpRequest){
        request = new XMLHttpRequest();
        request.onreadystatechange = processStateChange;
        request.open("GET", URL, true);
        request.send(null);
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.onreadystatechange = processStateChange;
            request.open("GET", URL, true);
            request.send();
        }
    }
}






function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  if (!elem) {return;}
  if (!elem.style){return;}
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function hideLayer(whichLayer) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
  elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
  elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
  elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  vis.display = 'none';
}


function showLayer(whichLayer) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
  elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
  elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
  elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  vis.display = 'block';
}



function checkEnter(e){ //e is event object passed from function invocation
var characterCode;// literal character code will be stored in this variable

if(e && e.which){ //if which property of event object is supported (NN4)
e = e;
characterCode = e.which; //character code is contained in NN4's which property
}
else{
e = event;
characterCode = e.keyCode; //character code is contained in IE's keyCode property
}
if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
return true;
}
else{
return false;
}

}


function getElement( id )
{
  return document.getElementById(id);
}

function removeAllChilds( el ) {
  var cell = getElement(el);
  if (!cell) return;

  if ( cell.hasChildNodes() )
  {
    while ( cell.childNodes.length >= 1 )
    {
      cell.removeChild( cell.firstChild );
    }
  }
}


function update_del_link(cat, p) {
  id = getElement('del_photo_link');
  if (!id) { return; }
  if (id) {
    id.href = "./www.didyk.eu.delete_photo.php?delete=1&cat="+gphotos[cat]['cat_id']+"&gal="+current_gallery+"&photo="+gphotos[cat][p]['photo_id'];
  }
}


function getImage(pExistingImageID, pImageURL){
  var img = document.createElement('img');
  if (pExistingImageID!=0) {
    img.onload = function (evt) {
      el = document.getElementById(pExistingImageID);
      el.src=this.src;
      var width = this.width;
      var height = this.height;
      /*if (380/height*width<=500) {
	      width = 380/height*width;
	      height = 380;
      }*/
      el.width=width;
      el.height=height;
      var st = document.getElementById(pExistingImageID);
      if (st.style) {st = st.style;}
      var margin_left = parseInt((500 - width)/ 2);
      var margin_top = parseInt((380 - height))-2;
      st.margin = margin_top+"px 0px 0px "+margin_left+"px";
      st.visibility = 'visible';
    }
  }
  img.src = pImageURL;
  return img;
}

function null_() {
	
}

function getImage2(pExistingImageID, pImgContainer, pImageURL){
	cont = document.getElementById(pImgContainer);
	if (cont.style) {cont=cont.style}
	cont.background="url('"+pImageURL+"') center no-repeat";
	return;
  var img = document.createElement('img');
  if (pExistingImageID!=0) {
    img.onload = function (evt) {
      el = document.getElementById(pExistingImageID);
      cont = document.getElementById(pImgContainer);
      this.setAttribute('id', pExistingImageID);
      cont.removeChild(el);
     
      var width = this.width;
      var height = this.height;
      var st = this;
      if (st.style) {st = st.style;}
      var margin_left = parseInt((500 - width)/ 2);
      var margin_top = parseInt((380 - height))-2;
      //st.margin = margin_top+"px 0px 0px "+margin_left+"px";
      //st.padding = "0 0";
	   cont.appendChild(this);
	   cont.style.textAlign="center";
    }
  }
  img.src = pImageURL;
  return img;
}


function copyImageObj(img_obj) {
  var img = document.createElement('img');
  img.src = img_obj.src;
  img.width = img_obj.width;
  img.height = img_obj.height;
  return img;
}

function newImageObj() {
  return document.createElement('img');
}


function getLeft(obj) {
  var curleft = 0;
  if(!obj) return;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
    }
  }
  return curleft;
}

function cat_width(cat) {
  i = gphotos[cat]['last'];
  last_p = getElement("img_cat"+cat+"_"+i);
  first_p = getElement("img_cat"+cat+"_0");
  return (getLeft(last_p)-getLeft(first_p)+last_p.width);
}





function check_nav(cat) {
  if (cat_width(cat)<=500) {
    hideLayer("control_sm_l"+cat);
    hideLayer("control_sm_r"+cat);
  } else {
  if (gphotos[cat]['pos'] == 0) {
    hideLayer("control_sm_l"+cat);
	showLayer("control_sm_l_clear"+cat);
  } else {
	 hideLayer("control_sm_l_clear"+cat);
    showLayer("control_sm_l"+cat);
  }
  if (gphotos[cat]['pos']+cat_width(cat) <= 500) {
    hideLayer("control_sm_r"+cat);
  } else {
    showLayer("control_sm_r"+cat);
  }}
  if (current_photo == 0 && current_cat == 0) {
    hideLayer("control_col_l");
	showLayer("control_col_l_clear");
  } else {
	  hideLayer("control_col_l_clear");
    showLayer("control_col_l");
  }
  if (current_photo == gphotos[current_cat]['last'] && cat == gphotos.length-1) {
    hideLayer("control_col_r");
  } else {
    showLayer("control_col_r");
  }
}



function cat_scroll(cat, step) {
  id = getElement("cat_wrapper"+cat);
  var noPx = document.childNodes ? 'px' : 0;
  if( id.style ) { id = id.style; }
  if (!id.left) {
    id.left = 0 + noPx;
  }
  id.left = ( parseInt(id.left) + step ) + noPx;
  if ((parseInt(id.left)>gphotos[cat]['pos'] && step<0) || (parseInt(id.left)<gphotos[cat]['pos'] && step>0)) {
    setTimeout(function(){cat_scroll(cat, step)},50);
  } else {
    id.left = gphotos[cat]['pos'] + noPx;
    already_scroll = false;
  }
}





function cat_next(cat) {
  if (!gphotos[cat]) {return;}
  if (already_scroll) {return;}
  gphotos[cat]['pos'] -= 50;
  if (gphotos[cat]['pos']+cat_width(cat)<500)
  {
    gphotos[cat]['pos'] = 500-cat_width(cat);
  }
  check_nav(cat);
  if (!already_scroll) {already_scroll = true;cat_scroll(cat, -30);}
}

function cat_prev(cat) {
  if (!gphotos[cat]) {return;}
  if (already_scroll) {return;}
  gphotos[cat]['pos'] += 50;
  if (gphotos[cat]['pos']>0) {gphotos[cat]['pos'] = 0;}
  check_nav(cat);
  if (!already_scroll) {already_scroll = true;cat_scroll(cat, +30);}
}


function move_cat(cat) {
  p = getElement("img_cat"+cat+"_"+current_photo);
  pf = getElement("img_cat"+cat+"_0");
  if (getLeft(p)-getLeft(pf)+gphotos[cat]['pos']<0) {
    gphotos[cat]['pos'] -= (getLeft(p)-getLeft(pf)+gphotos[cat]['pos']);
    already_scroll = true;cat_scroll(cat, +30); return;
  }
  if (getLeft(p)-getLeft(pf)+p.width+gphotos[cat]['pos']>500) {
    gphotos[cat]['pos'] -= (getLeft(p)-getLeft(pf)+p.width+gphotos[cat]['pos'])-500;
    already_scroll = true;cat_scroll(cat, -30);
  }
}


function setPhoto(p, c) {
  if (!gphotos) {return;}
  while (!gphotos[c]) {c--;}
  while (!gphotos[c][p]) {p--;}
  current_photo = p;
  current_cat = c;
  move_cat(current_cat);
  check_nav(current_cat);
  update_del_link(c,p);
  getImage2('nn_html_photo', 'nn_html_photo_div', "/images/"+gphotos[current_cat][current_photo]['photo_url']);
  getElement('photo_dsc').innerHTML = gphotos[current_cat][current_photo]['photo_name'];
}


function next_photo() {
  current_photo++;
  if (!gphotos[current_cat][current_photo]) {
    if (gphotos[current_cat+1]) {
      current_cat++;
      current_photo = 0;
    } else {
      current_photo--;
      return;
    }
  }
  move_cat(current_cat);
  check_nav(current_cat);
  update_del_link(current_cat, current_photo);
  getImage2('nn_html_photo', 'nn_html_photo_div', "/images/"+gphotos[current_cat][current_photo]['photo_url']);
  getElement('photo_dsc').innerHTML = gphotos[current_cat][current_photo]['photo_name'];
}

function prev_photo() {
  current_photo--;
  if (!gphotos[current_cat][current_photo]) {
    if (gphotos[current_cat-1]) {
      current_cat--;
      current_photo = gphotos[current_cat].length-1;
    } else {
      current_photo++;
      return;
    }
  }
  move_cat(current_cat);
  check_nav(current_cat);
  update_del_link(current_cat, current_photo);
  getImage2('nn_html_photo', 'nn_html_photo_div', "/images/"+gphotos[current_cat][current_photo]['photo_url']);
  getElement('photo_dsc').innerHTML = gphotos[current_cat][current_photo]['photo_name'];
}




function loadcat(gal, cat, photo) {
  loadHTML('/www.didyk.eu.categories.php?gal='+gal+'&cat='+cat+'&offset='+cat_offset[cat]+'&selected='+photo, 'cat'+cat);
}
