// ALL Scripts for PHP Modules
// (c) Bout Time Software
// 11.03.17 - added prodid to scroll for handling to support numerous galleries on one page.
// 11.03.20 - added code for handling add serving
// 11.03.24 - ie not working when in load on every page with the add slideshow, so using return to just show each image. nothing else.
// 11.09.05 - added expand functions for new sitemap
// 11.10.10 - updated scroll for tutorial support and enhanced scroll accuracy.
// 11.10.13 - ehnanced scroll to block change of direction until current scroll is complete
// 11.12.01 - upgraded to array for scroll to allow multiple per page. calling module to define prior to function call scrollAmount[id] scrollqty[id]

//*************************** GALLERY SCRIPT for news articles ******************
/////////////////////////////////////////////////////////////////////////////////
///////////////////////// Image thumnail manager

ScrollAmount = 500;  //506				// this is reset based on what needs scrolling called before scroll *legacy*
scrollgroup  = 0;					// 0 means a single page advancement, any other # is the number of page requested.

// below to be defined in calling module or now moved to array
//scrollvalue  = 0;					// this is the amount currently scrolled, set by scroll
//scrollqty    = 10;					// this is the amount to scroll per call of scroll (can be reassigned for faster or slower)
//scrollActive = 0;					// if currently in a scroll sequence do not allow any future sequence until completed?
//scrollMode = 0;

var scrollActive = new Array();
var scrollvalue  = new Array();
var scrollMode   = new Array();
var scrollAmount = new Array();
var scrollqty    = new Array();

var TutDIZ = new Array();				// for tutorial descriptions, make multi dimensional . mulit now defined as required for each element.

function scroll(prodid,scrollright)
{
  objDIV = document.getElementById('thumbs'+prodid);
  if (!scrollvalue[prodid]) scrollvalue[prodid] = 0;		// force a value if none found to ensure first event is accurate.

  if ((objDIV.scrollLeft <= 0 && !scrollright) || (scrollActive[prodid] == 1 && scrollMode[prodid] != scrollright)) return;		// this is to prevent scrolling past 0 and if in forward mode to block attempt to backward until forward mode finished (and vise versa)

  if (scrollAmount[prodid]-scrollvalue[prodid] <= Math.ceil(scrollqty[prodid]*8)) scrollqty[prodid] = Math.ceil(scrollqty[prodid]/2);
    else if (scrollAmount[prodid]-scrollvalue[prodid] <= Math.ceil(scrollqty[prodid]*4)) scrollqty[prodid] = Math.ceil(scrollqty[prodid]/4);
      else if (scrollAmount[prodid]-scrollvalue[prodid] <= scrollqty[prodid])  scrollqty[prodid] = scrollAmount[prodid]-scrollvalue[prodid];

  if (scrollAmount[prodid]-scrollvalue[prodid] == 0)  scrollqty[prodid] = 0;

  scrollvalue[prodid] += scrollqty[prodid];

  if (scrollright == 1)
    objDIV.scrollLeft += scrollqty[prodid];
  else 
    objDIV.scrollLeft -= scrollqty[prodid];

  if (scrollvalue[prodid] < scrollAmount[prodid])
  {
    scrollActive[prodid] = 1;
    scrollMode[prodid] = scrollright;
    process = window.setTimeout("scroll('"+prodid+"','"+scrollright+"')",1);
  }
  else
  {
    scrollActive[prodid]=0;
    scrollvalue[prodid] = 0;
    scrollqty[prodid] = 10;							// now also reset scroll qty to default incase quickjump was active.


    var UpdateCnt = document.getElementById('POS'+prodid);		// get UpdateCnt value.
    if (UpdateCnt != null)						// ifTutorial this input field is set. so now update it and auto update diz.
    {
      if (scrollgroup > 0)						// if scrollgroup set then auto set value to current scrollgroup
        UpdateCnt.value = scrollgroup; 
      else
        if (scrollright==1)
        {
          if (Number(UpdateCnt.value) < Number(document.getElementById('MAXPOS'+prodid).value))
            UpdateCnt.value = Number(UpdateCnt.value)+1;
        }
        else
          UpdateCnt.value = Number(UpdateCnt.value)-1;

      var objDIZ = document.getElementById('ptitle'+prodid);		// get description object.
      if (objDIZ != null)						// if it is set then update it
      {
       if (TutDIZ[prodid][UpdateCnt.value] != null)			// if tutorial array description position is set then use it else set no diz available		//Number(UpdateCnt.value)
        document.getElementById('ptitle'+prodid).innerHTML=TutDIZ[prodid][Number(UpdateCnt.value)];
       else
        document.getElementById('ptitle'+prodid).innerHTML="<BR><div align=center><I>A description for this picture is currently unavailable.</I></DIV>";
      }
    }
    scrollgroup = 0;
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// Used to provide fast navigation to a specific picture in a scroll list (ie tutorials)
//
//var hscroll = (document.all ? document.scrollLeft : window.pageXOffset);
//var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
//then when you swap in the new form just scroll to those positions:
//window.scrollTo(hscroll, vscroll);
//   document.Form1.scrollx.value = (document.all)?document.body.scrollLeft:window.pageXOffset; 

function QuickJump(prodid,pos)
{
  if (!prodid || pos <= 0) return;				// nothing entered.

  objDIV = document.getElementById('thumbs'+prodid);		// identify object to scroll

  var ScrollReq = scrollAmount[prodid]*(pos-1);			// total scroll required to reach selection
  var scrolldir = 0;						// identify if left or right scroll is required based on current scroll state of objDIV

  if (objDIV.scrollLeft > ScrollReq)
    scrollAmount[prodid] = objDIV.scrollLeft - ScrollReq; 	//+((objDIV.scrollLeft%ScrollAmount)*ScrollAmount);
  else
  {
    scrollAmount[prodid] = ScrollReq - objDIV.scrollLeft;
    scrolldir = 1;
  }

  scrollgroup = pos;						// set the page that was requested
  scrollqty[prodid] = 100;					// speed scroll to 100. it is reset after scroll() has completed by scroll().

  scroll(prodid,scrolldir);
}


//////////////////////////////////////////////////////////
///////////////// image selection fade out and in
/////////////////////////////////////////////////////////

  var browser=navigator.appName;
  var b_version=navigator.appVersion;
  var bversion=parseFloat(b_version);
  if (browser=="Microsoft Internet Explorer")
  {
    if (bversion <= 4)
     isitie = 1;
    else
     isitie = 2;
  }
  else
    isitie = 0;

  var newsrc = "";
  var fn1 = 1;
  var fn2 = 100;
  var imgposition = "";


/////////////////////////////////////////////////////////
///////////////////// Select next image
/////////////////////////////////////////////////////////
function switchpic(imgpos, newimage)
{
  imgposition = imgpos;					// incase running multiple galleries on one page.

  if (newsrc == newimage)
   return;

  if (fn1 < 1 || fn2 < 100)		//if user clicks on another thumb while previous one is fading up, it causes a stutter and image doesn't fully fadein. so run checkimgstatus to sort it out
   checkimgstatus();

  newsrc = newimage;
  fadeit();
}

function fadeit()
{
  delay = 50;
  fn1 = fn1 - 0.11;
  fn2 = fn2 - 10;

  document.images[imgposition].style.opacity =fn1;
  if (isitie)
    document.images[imgposition].style.filter = 'alpha(opacity='+fn2+')';

// 100803 was just fn2 now trying force above 0 to ensure it doesn't revert to negative which yields no image
  if (fn2 >0)
    setTimeout("fadeit()",delay);
  else
  {
    document.images[imgposition].src = newsrc;

    var pic=new Image();

    if (isitie == 1)
    {
      pic.onload = function() {setTimeout("fadeitup()",delay);};
      pic.onerror = function() {setTimeout("fadeitup()",delay);};
      pic.src= newsrc;
    }
    else
    {
     pic.src= newsrc;
     if(pic.complete) setTimeout("fadeitup()",delay);
      else setTimeout("checkimgstatus()",delay);
    }
  }
}
function fadeitup()
{
  delay = 50;
  fn1 = fn1 + 0.11;
  fn2 = fn2 + 10;

  document.images[imgposition].style.opacity =fn1;
  if (isitie)
    document.images[imgposition].style.filter = 'alpha(opacity='+fn2+')';

  if (fn2 < 100)
    setTimeout("fadeitup()",delay);
}

function checkimgstatus()
{
  delay = 100;
  var pic=new Image(600,450);
  pic.src= newsrc;
  if(pic.complete) fadeitup();
    else setTimeout("checkimgstatus()",delay);
}

//////////////////////////////////////////////////////
/////////////////// for adjusting Image descriptions
//////////////////////////////////////////////////////

function picdiz(infob, details)
{
 document.getElementById(infob).innerHTML=details;
}
//*************************************************************/


//*********************************** DROP DOWN SELECTION MENUS FOR ONLINE FORMS *******************************/
/////////////////////////////////////////
//  FUNCTIONS to handle search form drop downs
/////////////////////////////////////////

//document.onclick = closecurrent; 
function inuse(currentsel)  { currentsel.style.backgroundColor="#aaaaaa"; }
function outuse(currentsel) { currentsel.style.backgroundColor="#888888"; }

var curlist = 0;		// keep track of which list is currently active

function showlist(id)
{
  var same = 0;
  if (curlist) { same = curlist;closecurrent();}	// if a list is active then it wasn't shutdown, ie no selection made, so now close it
  curlist = document.getElementById(id);		// assign current selection to current list variable
  if (curlist != same)					// is the current selection the same as the previous selection, if yes then this is to close the current list (ie double click)
    curlist.style.visibility='visible';
  else
   curlist = 0;
}

function makeselection(id,value,multiitem)
{
  if (multiitem)
  {
    if (document.getElementById(id).value == "Click to Select Suburbs, Default is All") document.getElementById(id).value="";

    if (document.getElementById(id).value.indexOf(value) == -1)		// check if postcode exists in value already. if -1 result is no
      document.getElementById(id).value += " "+value;
    else
      alert ("The selected suburbs postcode ( "+value+" ) is already selected. Some suburbs have the same postcode.");
  }
  else
    document.getElementById(id).value = value;
  if (curlist)						// selection has been made so close list.
    closecurrent();
}

function closecurrent()
{
  if (curlist)						// double check that curlist is valid, if yes then close it
    curlist.style.visibility='hidden';
  curlist = 0;
}
//*************************************************************/


//******************************************************* FLASH OBJECT ******************/
////////////////////////// 

colrange = ["#ffffff","#ffffcc","#ffff99","#ffff00","#ffcc00","#ff9900","#ff6600","#ff3300","#ff0000","#dd0000","#bb0000","#990000","#770000","#550000","#330000","#000000","#330000","#550000","#770000","#990000","#bb0000","#dd0000","#ff0000","#ff3300","#ff6600","#ff9900","#ffcc00","#ffff00","#ffff00","#ffffcc","#ffffff"];
colbw = ["#ffffff","#000000","#ffffff","#000000","#ffffff"];

ct = 0;
showt=0; showp=0;

function flashany(what)
{
  document.getElementById(what).style.color=colrange[ct];

  if (ct >= (colrange.length-1))
  {
   t=setTimeout("flashit()",3000);
    ct = 0;
  }
  else
  {
    ct++;
    t=setTimeout("flashit()",75);
  }
}

///////////////////////////////////////
////// Advertising display code. using image expanding idea.
///////////////////////////////////////


addloc   = [""];						// define in module for location
addlink  = ["display-product.php?pc=code"];			// define in module for link location
addinfo  = ["Bout Time Software"];				// define in module for description. for html use '

var addsc	= addloc.length-1;				// set to length of description array -1 as counter used starts at 0 but length starts counting at 1
var t		= 0;						// timer
var addpos	= 0;						// current position
var adddelay	= 2000;						// slideshow delay

function slidelink()  { if (addlink[addpos]) window.location=addlink[addpos];else window.location="#";}

iwid 		= 1;						// increment width
addmaxwidth	= 245;						// max width to define size of container.

var lastadd 	= 0;						// keep track of last value for quick and dirty comparison
var newaddsrc   = 0;						// what image is coming up next.

/////////////////////////////////////////
// function to show top slideshow
/////////////////////////////////////////

function addshow(status)
{
  addsc	= addloc.length-1;					// reset this now to ensure that it is accurate s above addsc is upon script load where arrays are empty

  if (addpos >= addsc)
    addpos = 1;
  else
    addpos++;

  newaddsrc = "images/adds/"+addloc[addpos]+"/advert.jpg";		// get new source location using fdata which is path structure
  document.images['prelo'].src = newaddsrc;

  iwid = 1;
  t=setTimeout("growimg()",1000);					// run delay first to give image time to load?
}

function addloadcheck()							// not active.
{
// trying to run it back on itself if image not loaded..?
  var addpic=new Image();
  addpic.src= document.images['prelo'].src;
  if(!addpic.complete) {alert("not loaded");t=setTimeout("growimg()",500);}

// testing new idea.
}

/////////////////////////////////////////
// function to expand image
/////////////////////////////////////////
function growimg()
{
  document.images['addimg'].src = newaddsrc;

  if (addinfo[addpos])
    document.getElementById('addsummary').innerHTML=addinfo[addpos];
  else
    document.getElementById('addsummary').innerHTML="";


  if (isitie == 1)
  {
    iwid = addmaxwidth;
    waitshow();
    return;
  }

  document.images['addimg'].width = iwid;

// moving onload to new function not active.

  if (iwid <= addmaxwidth-1)						// was <=
  {
    iwid = iwid+20;							// was 30
    if (iwid > addmaxwidth) iwid=addmaxwidth;				// had +1 to both
    t = setTimeout("growimg()",5);
  }
  else
  {
    waitshow();
  }
}

/////////////////////////////////////////
// function to restart slideshow after pause
/////////////////////////////////////////
function waitshow()
{
  if (iwid < addmaxwidth)					// was <= . trying this instead so if image not complete. continue.
    growimg();
  else
    t = setTimeout("addshow()",adddelay);
}
//////////////////////////
//////check status of image load
/////////////////////////
function addcheck()
{
  var cstatus = 0;

  var addpic=new Image(250,150);
  addpic.src = newaddsrc;

  if (isitie == 1)
  {
    if (addpic.onload)					// contains value if still loading.
      cstatus = 0;
    else
      cstatus = 1;
    if (addpic.onerror) cstatus = 1; else cstatus=1;		// if error skip.
  }
  else
  {
    if(addpic.complete)
      cstatus = 1;						//setTimeout("waitshow()",500);
  }

  if (cstatus)
//return;
  t=setTimeout("growimg()",adddelay);					// delay was adddelay now trying 100//    growimg();
  else
    setTimeout("addcheck()",250);
}


//////////////////////////////////////////////////////////
/////////////********************* PRODUCT MANAGER DISPLAY INFORMATION BOXES ***************************//
//////////////////////////////////////////////////////////
// used to manage which box is currently displayed.
//////////////////////////

 tabrange = ["Features","Specifications","Details","Options","Downloads"];

function DisplayInfo(what)
{
  for (i=0; i <= tabrange.length-1; i++)
  {
    document.getElementById(tabrange[i]).style.display = "none";
//  document.getElementById(tabrange[i]).style.visibility = 'hidden';			

    tabicon = "Tab"+i;
    document.getElementById(tabicon).className='InfoTab';
  }

  tabicon = "Tab"+what;
  document.getElementById(tabicon).className='InfoTabOn';
  document.getElementById(tabrange[what]).style.display = "";

//  document.getElementById(tabrange[what]).style.visibility = 'visible';			// visibility makes element keep it's current position in the html.
}

function TabLight(what)
{
  if (document.getElementById(tabrange[what]).style.display == "") return;
  tabicon = "Tab"+what;
  document.getElementById(tabicon).className='InfoTabLight';
//    document.getElementById(tabicon).style.backgroundColor="#5555FF";
}

function TabDark(what)
{
  if (document.getElementById(tabrange[what]).style.display == "") return;

  tabicon = "Tab"+what;
  document.getElementById(tabicon).className='InfoTab';
//  document.getElementById(tabicon).style.backgroundColor="#aaaaaa";				// background colour change fixes color and change style doesn't restore.
}

////////////////////////////////////////////////
/////////// File Download
////////////////////////////////////////////////
function filedl(from,where,what)
{
  var url = "";
  switch (from)
  {
    case 1:
      url="images/news/"+where+"/"+what;
    break;

    case 2:
      url="images/product/"+where+"/"+what;
    break;

    case 3:
      url="images/gallery/"+what;
    break;

  }

  if (url)
  {
    window.open("download.php?me="+from+"&rid="+where+"&n="+what);
//    window.open(url,'Download');  
  }
}

////////////////// NEW download option for download.php accessed from product-insert.php and module-gallery.php

function getfile(url)
{
  if (browser=="Microsoft Internet Explorer")
  {
    var referLink = document.createElement('a');
    referLink.href = url;
    document.body.appendChild(referLink);
    referLink.click();
  }
  else
    location.href = url;
}


/* offline download check. not using now, using above instead, much cleaner.
//use browser sniffing to determine if IE or Opera (ugly, but required)
var isOpera, isIE = false;
if(typeof(window.opera) != 'undefined'){isOpera = true;}
if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true;}

//define for all browsers
function getfile(url){
  location.href = url;
}

//re-define for IE
if(isIE){
  function getfile(url){
    var referLink = document.createElement('a');
    referLink.href = url;
    document.body.appendChild(referLink);

alert(referLink.href);
    referLink.click();
  }
}
*/

///////////////////////////////////////////////////
//////////////// Download Product page as PDF.
////////////////////////////////////////////////////
function pagedl(from,where)
{
  var url = "product-pdf.php?rid="+from;
  if (url && where==1)
    window.open(url,'Download');  
}


///////////////////////////////////////////////////
//////////////// For Catalogue hide/show of category lists
//////////////////////////////////////////////////

function expandobject(what)
{
  if (document.getElementById(what).style.display == "")
    document.getElementById(what).style.display = "none";
  else
    document.getElementById(what).style.display = "";
}



////////////////////////////////////////////////
//////// FOR FAQ SECTION HANDLING OF CAT and ANSWERS now also used for Portfolio page
///////////////////////////////////////////////

 FAQCat = ["A","B"];				// these are reset and assigned via php
 FAQAns = ["A","B"];				// these are reset and assigned via php

function DisplayFAQCat(what)			// what is position in array
{
  for (i=0; i <= FAQCat.length-1; i++)
  {
    if (document.getElementById(FAQCat[i]) != null) document.getElementById(FAQCat[i]).style.display = "none";
  }
  document.getElementById(FAQCat[what]).style.display = "";
}

function DisplayFAQAns(what)			// what is actual array value not position
{
  for (i=0; i <= FAQAns.length-1; i++)
  {
    if (document.getElementById(FAQAns[i]) !=null) document.getElementById(FAQAns[i]).style.display = "none";
  }
  if (!what)
    document.getElementById('AnsIntro').style.display = "";
  else
    document.getElementById('A'+what).style.display = "";
}


////////////////////////////////////////////////////
////////// Expand option for Sitemap
////////////////////////////////////////////////////

function smexpand(section)
{
  if (document.getElementById(section).style.display=="")
   {
      document.getElementById(section).style.display = "none";
   }
  else
   document.getElementById(section).style.display = "";
}

var expandmax = 0;

function expandall(how)
{
  for (i=1;i<expandmax;i++)
  {
    var section = "PS"+i;
    if (how) document.getElementById(section).style.display = "";
    else     document.getElementById(section).style.display = "none";
  }
}



//  document.write("<A TITLE='Click to Download' HREF='images/product/"+where+"/"+what+"'>"+what+"</A>");

// -- END --
