var menus = new Array();
var openMenus = new Array();
var menuTimer = 0;
var slideTimer = 0;
var thumbTimer = 0;
var lastMenu = '';
var slideOffset = 0;
var thumbOffsetL = 0;
var thumbOffsetT = 0;
var currentPic = 1;
var maxPic = 0;
var usrDir = '';
var moving1 = 0;
var moving2 = 0;
var openPanel = 0;
var lastPanel = 0;
var obj = 0;
var orderItems = new Array();
var cGal = 1;
comp = (document.getElementById) ? true:false;

function setDebug(){
  document.write('<div id="dbWin" name="dbWin" style="position:absolute; top: 0px; left: 0px; width: 100px; color: grey;">Debug Window</div>')
}
function db(str){
  document.getElementById('dbWin').innerHTML += '<br />'+str;
}

function DocLoaded(imgN){
  document.getElementById('loadingBox').style.display = 'none';
  slideAll(2);

  //prevent double click selecting contents of div's
  sobj = document.getElementById('prevPic');
  sobj.onselectstart = function() { return false; }
  sobj.onmousedown = function() { return false; }

  sobj = document.getElementById('nextPic');
  sobj.onselectstart = function() { return false; }
  sobj.onmousedown = function() { return false; }
  
  if (cGal){
    ajFun ('basketCount','cartCount');
  }
}

function addComment(){
  //Comment on picture
}

function buildPOST(formName) {
    frm = document.forms[formName];
    var qs = ''
    for (e=0;e<frm.elements.length;e++) {
        if (frm.elements[e].name!='') {
            var name = frm.elements[e].name;
            qs+=(qs=='')?'':'&'
            qs+= name+'='+escape(frm.elements[e].value);
        }
    }
    qs+="\n";
    return qs;
}

function placeOrder(){
  //this function needs an ajax call with POST.
  var params = buildPOST('order');
  var ajr;
  if (window.XMLHttpRequest){
    ajr = new XMLHttpRequest;
  } else {
    ajr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  ajr.open("POST",'placeOrder.php', true);
  ajr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  ajr.setRequestHeader("Content-length", params.length);
  ajr.setRequestHeader("Connection", "close");
  
  ajr.onreadystatechange=function(){
    if (ajr.readyState == 4){
      if(ajr.responseText=='DONE'){
        alert ('Your order has been sent. Thank you!');
        clearOrder();
        showPanel();
      } else {
        alert (ajr.responseText);
      }
    }
  }
  ajr.send(params);

}
function add2Order(){
  //Add photo to order session
  var photoCode =  document.getElementById('imgName').innerHTML;
  var itemCode = document.getElementById('orderItem').value;
  var qty = document.getElementById('quantity').value;
  
  if (!(isset(itemCode))){
    alert ('Please select a product & size from the list');
    return;
  }
  
  var params = '&photoCode='+photoCode+'&itemCode='+itemCode+'&qty='+qty;
  ajFun('add', 'cartCount', params);
  
  document.getElementById('quantity').value = 1;
}

function viewOrder(){
  // reset order panel
  document.getElementById('viewOrder').innerHTML = '<img src="images/loading.gif">';
  // show order panel
  if (openPanel.id != 'viewOrder'){
    showPanel('viewOrder'); 
  }
  
  // ajax request for order
  ajFun('view','viewOrder');
}

function removeThis(key){
  var params = '&id='+key;
  ajFun ('removeItem', 'cartCount', params);
  setTimeout('viewOrder()', 500);
}

function clearOrder(){
  // ajax request to clear order;
  ajFun ('clear', 'cartCount');
}

function ajFun (func, output, params){
  params = typeof(params) != 'undefined' ? params : '';
  var url = 'ajFunctions.php?func='+func+params;
  var ajr;
  if (window.XMLHttpRequest){
    ajr = new XMLHttpRequest;
  } else {
    ajr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  ajr.open("GET",url, true);
  ajr.onreadystatechange=function(){
    if (ajr.readyState == 4){
      //use information
      document.getElementById(output).innerHTML = ajr.responseText;
    }
  }
  ajr.send();
}

function setItem(itemType){
  document.getElementById('orderList').innerHTML = orderItems[itemType];
}

function chgItm(){
  //calculate long side
  var lSd = 0;
  var sSd = 0;
  var msg = '';
  switch (parseInt(document.getElementById('orderItem').value)){
    //photo
    case 1: case 7: case 9: case 12: case 14: case 16: case 18:
    lSd = 139; sSd = 93;
    msg = 'The proportions of this print size match the image from the camera, none of the image will be lost';
    break;
    case 2: case 5: case 10:
    lSd = 93; sSd = 139;
    msg = 'SQ';
    break;
    case 3:
    lSd = 130; sSd = 100;
    msg = '5x7 is a very common frame size, a small amount of clipping of the image will be require to fit this size frame.';
    break;
    case 4: case 11:
    lSd = 124; sSd = 105;
    msg = 'This is a common frame size, however some clipping of the image will occur.';
    break;
    case 6: 
    lSd = 116; sSd = 112;
    msg = '8x10 is probably the most common frame size availible, some clipping of the image will be require to fit this size frame.';
    break;
    case 13: case 17:
    lSd = 116; sSd = 112;
    msg = 'This is a common frame size, however some clipping of the image will occur.';
    break;
    case 8: case 15:
    lSd = 112; sSd = 117;
    msg = 'This is a common frame size, however some clipping of the image will occur.';
    break;
    default:
    lSd = 138; sSd = 138;
  }
//get aspect
  var sImg = document.getElementById('sizeImg');
  var cLn = document.getElementById('clipLine');
  if (sImg.width == sImg.height){
    //square image
    tp = (138 - sSd) / 2;
    cLn.style.height = sSd + 'px';
    lt = 20;
    cLn.style.width = '138px';
    cLn.style.top = tp + 'px';
    cLn.style.left = lt + 'px';
    if (msg == 'SQ'){
      msg = 'Some images look better in a square frame. This print size will fit a square image and fit common frame sizes.';
    }
  } else if (sImg.width > sImg.height){
    // landscape
    tp = 0;
    cLn.style.height = '91px';
    lt = ((138 - lSd) / 2) + 20;
    cLn.style.width = lSd + 'px';
    cLn.style.top = tp + 'px';
    cLn.style.left = lt + 'px';
    if (msg == 'SQ'){
      msg = 'This is a square print size and should be selected for square cropped images.';
    }
 } else {
    //portrait
    tp = 68 - (lSd / 2);
    cLn.style.height = lSd + 'px';
    lt = 43;
    cLn.style.width = '91px';
    cLn.style.top = tp + 'px';
    cLn.style.left = lt + 'px';
    if (msg == 'SQ'){
      msg = 'This is a square print size and should be selected for square cropped images.';
    }
  }
  //show cliping
  //show help message
  document.getElementById('itemHelp').innerHTML = '<p>'+msg+'</p>';
}

function chgQty(diff){
  //change quantity
  var qty = parseInt (document.getElementById('quantity').value);
  qty = qty + diff;
  if (qty < 1){qty = 1;}
  document.getElementById('quantity').value = qty;
}

function setP(mp){
  maxPic = mp;
}
function setPfolio(){
  cGal = 0;
}

function setUsrDir(dr){
  usrDir = dr;
}

function showBk(){
  document.getElementById('prevArw').style.visibility = "visible";
}

function hideBk(){
  document.getElementById('prevArw').style.visibility = "hidden";
}

function showFd(){
  document.getElementById('nextArw').style.visibility = "visible";
}

function hideFd(){
  document.getElementById('nextArw').style.visibility = "hidden";
}

function slideAll (imgNo){
  currentPic = imgNo;
  stopSlide();
  img = document.getElementById('img'+imgNo);
  imgT = document.getElementById('imgT'+imgNo);
  if (img.offsetLeft){
    imgL = img.offsetLeft;
    imgW = img.offsetWidth;
    tl = imgT.offsetLeft;
    tt = imgT.offsetTop;
  } else {
    imgL = img.x;
    imgW = img.width;
    tl = imgT.x;
    tt = imgT.y;
  }
  slideOffset = 432 - (imgW/2) - imgL;
  thumbOffsetL = tl - 1;
  thumbOffsetT = tt - 1;
  if (img.alt){
    document.getElementById('imgName').innerHTML = img.alt;
    document.getElementById('sizeImg').src = usrDir + '/size/' + img.alt + '.jpg';
  }
  if (cGal){chgItm();}
  slideTimer = setInterval ('slidePics()', 20);
  thumbTimer = setInterval ('slideTb()', 20);
}

function slidePics(){
  var xnow = document.getElementById('picsInner').style;
  var xInt = parseInt(xnow.left);
  var diff = 0;
  if (xInt > slideOffset){
    diff = xInt - slideOffset;
    if (diff > 200) {xInt -= 90;}
    else {xInt -= (diff/4);}
    xnow.left = xInt+'px';
  }
  else if (slideOffset > xInt){
    diff = slideOffset - xInt;
    if (diff > 200) {xInt += 90;}
    else {xInt += (diff/4);}
    xnow.left = xInt+'px';
  }
  else {
    stopSlide();
  }
}

function slideTb(){
  var xnow = document.getElementById('thumbP').style;
  var xInt = parseInt(xnow.left);
  var yInt = parseInt(xnow.top);
  var diff = 0;
  if (xInt > thumbOffsetL){
    diff = xInt - thumbOffsetL;
    if (diff > 40) {xInt -= 40;}
    else {xInt -= (diff/2);}
    xnow.left = xInt+'px';
  }
  else if (thumbOffsetL > xInt){
    diff = thumbOffsetL - xInt;
    if (diff > 40) {xInt += 40;}
    else {xInt += (diff/2);}
    xnow.left = xInt+'px';
  }
  if (yInt > thumbOffsetT){
    diff = yInt - thumbOffsetT;
    if (diff > 40) {yInt -= 40;}
    else {yInt -= (diff/2);}
    xnow.top = yInt+'px';
  }
  else if (thumbOffsetT > yInt){
    diff = thumbOffsetT - yInt;
    if (diff > 40) {yInt += 40;}
    else {yInt += (diff/2);}
    xnow.top = yInt+'px';
  }
}

function prevPic(){
  //move to prev pic;
  if (currentPic > 2){
    imgNo = currentPic - 1;
    slideAll(imgNo);
  }
}

function nextPic(){
  //move to next pic;
  if (currentPic < (maxPic-1)){
    imgNo = currentPic + 1;
    slideAll(imgNo);
  }
}

function stopSlide(){
	if (slideTimer > 0 ){clearInterval(slideTimer); slideTimer = 0;}
  if (thumbTimer > 0 ){clearInterval(thumbTimer); thumbTimer = 0;}
}


//Create menu object
function menuObj(name, level){
	var c=this; c.name=name; c.level=level; c.itemCount=0;
	c.mnuItems = new Array();
	return this;
}

function mnuItem(name, url, child){
	var c=this; c.name=name; c.url=url; c.child=child;
  c.imgOff = new Image();
  c.imgOff.src = 'images/' + name + 'Off.jpg';
  c.imgOn = new Image();
  c.imgOn.src = 'images/' + name + 'On.jpg';
	return this;
}

menuObj.prototype.addMnuItem = function(name, url, child){  //Add menu item
	var c = this;
	c.mnuItems[c.itemCount] = new mnuItem(name, url, child);
	c.itemCount ++;
}

function mnuOver(child, level){
	stopAll();
	if (level != null)
	{
    for (i=level+1; i < openMenus.length; i++){
			//close open menus at higher levels
			if (comp)
			{
				//document.getElementById(openMenus[i]).style.visibility = 'hidden';
        document.getElementById(child).style.display = 'none';
			} else {
				//document.all[openMenus[i]].style.visibility = 'hidden';
				document.all[child].style.display = 'none';
			}
		}
	}
	if (child != null){
		openMenus[level + 1] = child;
		if (comp){
			document.getElementById(child).style.display = 'block';
		} else {
			document.all[child].style.display = 'block';
		}
	}
}

function mnuOff(){
	if (menuTimer > 0){clearTimeout(menuTimer); menuTimer = 0;}
	menuTimer = setTimeout('clearAll()', 400);
}

function stopAll(){
	if (menuTimer > 0 ){clearTimeout(menuTimer); menuTimer = 0;}
}

function clearAll(){
	for (i=1;i < openMenus.length ;i++ ){
		if (comp)
		{
			document.getElementById(openMenus[i]).style.display = 'none';
		} else {
			document.all[openMenus[i]].style.display = 'none';
		}
	}
	openMenus = new Array();
}

function showPanel(pan){
  //check panels are not moving
  if (moving1 || moving2){return;}

  //hide current panel if any
  if (openPanel){
    lastPanel = openPanel;
    moving2 = setInterval('move2()', 20);
  }
  if (pan == openPanel.id){openPanel = 0; return;}

  //set this panel to current one
  if (pan){
    // move panel to view
    openPanel = document.getElementById(pan);
    moving1 = setInterval('move1()', 20);
    //slideTimer = setInterval ('slidePics()', 20);
  } else {
    openPanel = 0;
  }

}

function move1(){
  //move current
  var cTop = parseInt(openPanel.style.top);
  if (cTop < 20){
    cTop += 50;
    openPanel.style.top = cTop+"px";
  } else {
    clearInterval(moving1);
    moving1 = 0;
  }
}

function move2(){
  //move last
  var pTop = parseInt(lastPanel.style.top);
  if (pTop < 600){
    pTop += 50;
    lastPanel.style.top = pTop+"px";
  } else {
    pTop = -620;
    lastPanel.style.top = pTop+"px";
    clearInterval(moving2);
    moving2 = 0;
  }
}

function btnOver(btn){
  //hightlight button
  if (btn){
    var btnName = btn.id;
    btn.src = "images/btnOver/"+btnName+".jpg";
  }

}

function btnOff(btn){
  //restor button
  if (btn){
    var btnName = btn.id;
    btn.src = "images/btn/"+btnName+".jpg";
  }
}

function isset(me)
{
 if (me == null || me == '')
  return 0;
 else
  return 1;
}

