/* products1.js */

var version;

// Lets start ...
function on_load(){;
  clearChecked("smt1");

  drawGallery();

  eraseCookie("item");
  eraseCookie("itemDescription");
}

// If smt1 radio clicked ...
function clickedSmt1(){
  var grp = "smt1"
  var elements = document.getElementsByName(grp);
  var val = getCheckedValue(elements);
  var x;

  createCookie(grp, val, 0);
  x = readCookie(grp);
  if (x != val){
    alert("Cookies have been turned off in your browser!\n\nThis page requires cookies to function correctly.\nPlease turn cookies on and refresh the page.");
    return false;
  }

  eraseCookie("item");
  eraseCookie("itemDescription");

//  showHide();
}

function clickedAnItem(){
  var evt = window.event || arguments.callee.caller.arguments[0];
  var target = evt.target || evt.srcElement;
  createCookie("item",target.id,0);

  var x = target.alt.indexOf(" - click", 0);
  var y = target.alt.substring(0, x);
  createCookie("itemDescription", y, 0);

  window.opener.showHide();
}

function clearChecked(aName){
  var radioObj = document.getElementsByName(aName);
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
	}
}

// Does all the hard work!
function showHide(){
  var itemSelected, showHide = "";
  var showInfo, group = "";
  var x,y,z = "";

  x = readCookie("smt1");
  group = x.substring(0, 1);
  itemSelected = readCookie("item");
  itemDescription = readCookie("itemDescription");
  if (itemSelected != null){
    showHide = "visible";
    showInfo = "scd";
  }else{
    itemSelected = "";
    showHideItem = "hidden";
  }
//  visibleHidden("changeitem", showHideItem);

  // Display relevant details in infobox
  if (showInfo != ""){
    // list of html IDs/classes that need to be shown or not
    var info = ['init', 'scd'];

    for (x in info){
      y = "hidden";
      if (info[x] == showInfo)
        y = "visible";

      visibleHidden("info" + info[x], y);
    }
  }

  // If we're showing an item, populate the element with the correct picture
  var buttons = "hidden";
  if (itemSelected){
    z = "";

    var id = "";
    var style = "";
    var ajax = "";

/*
    // slight adjustments to style
    if ("~aluminium~wide-pan ~open fram~doors for~".indexOf(itemSelected.substring(0, 9) + "~") < 0){
      style = "style=\"height:;\"";
    }else if ("~hinges~modern pelmet~".indexOf(itemSelected + "~") > 0){
      style = "style=\"height:200px;\"";
    }
*/

    ajax = "onload=\"ajax_call('" + itemSelected + "', '', 'price_blurb_products1');\" ";

    // get relevant image path
    if (group == "5"){
      x = itemSelected;
      y = ".gif";
      z = itemSelected;
    }else{
      x = itemSelected.substring(0, 1) + itemSelected.substring(2);
      x = x.toLowerCase();
      y = ".jpg";
      z = itemDescription;
    }
    var altTitle = z.substring(0,1).toUpperCase() + z.substring(1);
    z = "<img " + style + ajax + " src=\"/images/products1/" + x + y + "\"";

    buttons = "visible";

    // image
    document.getElementById("infoscdimg").innerHTML =
      "<div>" + z + " alt=\"" + altTitle + "\" title=\"" + altTitle + "\"/></div>";

    // Now the item descriptors
/*
    if (group != '5'){
      y = altTitle.indexOf('(');
      altTitle = altTitle.substring(0, y - 1) + "<span style=\"font-size:.5em;\"> "
               + altTitle.substring(y) + "</span>";
    }
*/
    style = "style=\"padding-bottom:0px;";
    if (altTitle.length > 25){
      style+= "font-size:1.4em;";
    }
    y = "<h2 " + style + "\">" + altTitle + "</h2>";

    var text = y;
    document.getElementById("infodoorcolourheads").innerHTML = text;
  }
  visibleHidden("infoblurbtext", "visible");
  visibleHidden("infodoorbuttons", buttons);
}

// Get the currently selected door and add to the "gallery1" cookie
function addToGallery (){
  var x;
  var maxInGallery = 10;

  var oldCookie = readCookie("gallery1");
  if (!oldCookie)
    oldCookie = "";

  x = oldCookie.split("--");
  if ((x.length - 1) >= maxInGallery){
    alert("Sorry, a maximum of 10 items can be stored in the gallery!");
    return false;
  }

  var item = readCookie("smt1") + "." + readCookie("item") + "."
             + readCookie("itemDescription") + "--";
  if (oldCookie.indexOf(item) != -1){
    alert("This item is already in the Gallery!");
    return false;
  }

  createCookie("gallery1", oldCookie + item, 30);

  drawGallery();
}
// Refresh the Gallery
function drawGallery(){
  var x, y, z, item;
  var galleryImages = "";

  x = readCookie("gallery1");
  if (x != null){
    var itemList = x.split("--");
    for (item=0;item<itemList.length;item++){
      x = itemList[item].split(".");
      if (x[0]){
        if (x[0].substring(0, 1) == "5"){
          y = x[1];
          z = y + ".gif";
        }else{
          y = x[1].substring(0, 1) + x[1].substring(2);
          z = y.toLowerCase() + ".jpg";
        }
        y = x[2].substring(0, 1).toUpperCase() + x[2].substring(1) + " - click to view details";
        var image = "<div><img onclick=\"getFromGallery("+ item +");\" style=\"cursor:pointer;padding-top:10px;margin-left:10px;margin-top:0px;width:69px;\" src=\"/images/products1/" + z + "\"";
        image+=" alt=\"" + y + "\" title=\"" + y + "\" />\n";
        image+= "<img id=\"gallerydoordelete\" onclick=\"deleteFromGallery(" + item + ");\" src=\"/images/cancel.gif\" alt=\"Remove from gallery\" title=\"Remove from Gallery\" /></div>";
        galleryImages = image + galleryImages; // show in reverse order added to gallery
      }
    }
  }

  document.getElementById("galleryimg").innerHTML = galleryImages;
}

// User has clicked on a Gallery item - display it
function getFromGallery(index){
  var itemList = readCookie("gallery1").split("--");  
  x = itemList[index].split(".");

  createCookie("smt1", x[0], 0);
  createCookie("item", x[1], 0);
  createCookie("itemDescription", x[2], 0);

  var elements = document.getElementsByName("smt1");
  var val = setCheckedValue(elements, x[0]);

  showHide();
}
// User wants to delete this from the gallery
function deleteFromGallery(index){
  var newList = "";
  var itemList = readCookie("gallery1").split("--"); 

  for (var i=0; i<(itemList.length - 1); i++){
    if (i != index)
      newList+= itemList[i] + "--";
  }

  createCookie("gallery1", newList, 30);

  drawGallery();
}

//Actually set elements to visble or hidden
function visibleHidden(obj, thereOrNot){
  var x = document.getElementById(obj);
  if (x)
    x.style.visibility = thereOrNot;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj){
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++){
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}

	return "";
}
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue){
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined){
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++){
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()){
			radioObj[i].checked = true;
		}
	}
}

function createCookie(name,value,days){
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
