﻿// image2output javascript

window.onload = function(){
  highlightInputTextOnClick();
}

function highlightInputTextOnClick(){
  var elems = document.getElementsByTagName("input");
  for(i = 0; i < elems.length; i++){
    if(elems[i].type == "text"){
      elems[i].onfocus = function(){
        this.select();
      }
    }
  }
}

// home page panel fade
var imgs = new Array("fade_hypershot", "fade_inkjet", "fade_capture", "fade_colour", "fade_prepress", "fade_pressroom");
var text = new Array("text_hypershot", "text_inkjet", "text_capture", "text_colour", "text_prepress", "text_pressroom");
var alttext = new Array("alt_hypershot", "alt_inkjet", "alt_capture", "alt_colour", "alt_prepress", "alt_pressroom");
var current = 0;
var next = 1;
var interval = 6000;
var isTextVisible;

function initImgsToFade(){
  for(i = 1; i < imgs.length; i++){
    $(imgs[i]).style.display = "none";
    $(text[i]).style.display = "none";
    $(alttext[i]).style.display = "none";
  }
}

function startFade(){
  initImgsToFade();
  setTimeout("calculateNextFade()", interval);
}

function calculateNextFade(){
  if(next >= imgs.length){
    next = 0;
  }
  doFade(imgs[current], imgs[next]);
  if(isTextVisible){
    doFade(text[current], text[next]);
  }
  else{
    doFade(alttext[current], alttext[next]);
  }
  current = next;
  next++;
  setTimeout("calculateNextFade()", interval);
}

function doFade(objOut, objIn){
  Effect.Fade($(objOut), {from:1.0, to:0.0, duration:2.0});
  Effect.Appear($(objIn), {from:0.0, to:1.0, duration:2.0});
}

// window resize listener
function windowListener(){

  window.onresize = function(){
    windowListener();
  }
  if(getWindowWidth() <= 970){
    hideFadingText();
    isTextVisible = false;
    $('fade-alt-text').style.display = "block";
    $('fade').style.minHeight = "350px";
  }
  else{
    isTextVisible = true;
    $('fade-alt-text').style.display = "none";
    $('fade').style.minHeight = "320px";
  }
}

function hideFadingText(){
  for(i = 0; i < text.length; i++){
    $(text[i]).style.display = "none";
  }
}

function getWindowWidth(){
	if (self.innerWidth)
	{
		return frameWidth = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		return frameWidth = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return frameWidth = document.body.clientWidth;
	}
	else return;
}

function jsUpdateSearchText() {
  var sel = document.getElementById("searchBySelect");
  
  if (sel.options[sel.options.selectedIndex].value == "keyword"){
      document.getElementById("strsearch").value = "keyword";
  }
  else {
      document.getElementById("strsearch").value = "product code";
  }
}

function updateSearchBar(){
  var sel = document.getElementById("i20Search_searchBySelect");
  var value = sel.options[sel.options.selectedIndex].value;

  if (value == "code"){
    document.getElementById("i20Search_manufacturerList").style.display = "none";
    document.getElementById("i20Search_strsearch").style.display = "inline";
    document.getElementById("i20Search_strsearch").value = "product code";
  }
  else if (value == "manu") {
    document.getElementById("i20Search_manufacturerList").style.display = "inline";
    document.getElementById("i20Search_strsearch").style.display = "none";
  }
  else {
    document.getElementById("i20Search_manufacturerList").style.display = "none";
    document.getElementById("i20Search_strsearch").style.display = "inline";
    document.getElementById("i20Search_strsearch").value = "keyword";
  }
}

function setTimeoutWarning(){
  var basketTimeoutWarning = setTimeout("alert('You have been inactive for 25 minutes. After 30 minutes your session will expire and you will lose the contents of your shopping basket. Please navigate to another page on the website to continue.')", 15000000);
}

function clearTimeoutWarning(){
  if(typeof(basketTimeoutWarning) == "undefined"){
    clearTimeout(basketTimeoutWarning);
  }
}

function toggleVisibility(id){
  if(document.getElementById(id).style.display == "none" || document.getElementById(id).style.display == ""){
    document.getElementById(id).style.display = "block";
  }
  else{
    document.getElementById(id).style.display = "none;";
  }
  return false;
}

function toggleFilterVisibility(id){
  toggleVisibility(id);
  if (document.getElementById('toggleMoreOptions').innerHTML == "+ more options") {
    document.getElementById('toggleMoreOptions').innerHTML = "- less options";
  }
  else {
    document.getElementById('toggleMoreOptions').innerHTML = "+ more options";
  }
  return false;
}

function randomNumber(lowerBound, upperBound){
 var range = upperBound - lowerBound + 1;
 return Math.floor(Math.random() * range) + lowerBound;
}

function randomiseLandingPanels(){
 for(i = 0; i < xsell_panels.length; i++){
    document.getElementById(xsell_panels[i]).style.display = "none";
 }
 document.getElementById("xsell_" + randomNumber(1, xsell_panels.length)).style.display = "block"; 
}

var c

function showCurrencySelector(){
   document.getElementById('siteheader_currencyList').style.display = "block";
   if (typeof(c) != "undefined"){
      clearTimeout(c);
   }
   c = setTimeout('hideCurrencySelector()', 4000);
}
function hideCurrencySelector(){
    document.getElementById('siteheader_currencyList').style.display = "none";
}