//The following functions are for the save to my image gallery feature.

function expireCookie(name) {
	document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT"
	//alert('Cookie ' + name + ' expired.')
}

function setCookie(name, value) {
    //to add an item to the gallery call setCookie('mygallery',id) where id is the item's store id.
    var today = new Date()
    var expire = new Date()
    expire.setTime(today.getTime() + 1000*60*60*12*180) //expire in 180 days
    var expires = expire.toGMTString()
    document.cookie = name + "=" + escape(value) + " " + getCookie(name) + ((expires == '') ? "; " : ("; expires=" + expires + ";"))
}

function getCookie(name) {
  var search = name + "="
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search) 
    if (offset != -1) {   // if cookie exists 
      offset += search.length
      end = document.cookie.indexOf(";", offset)
      if (end == -1) end = document.cookie.length
      return unescape(document.cookie.substring(offset, end))
    } else return '' 
  } else return ''
}

function writeGalleryItem(id,img) {
	var idl = id.toLowerCase()
	var base = 'http://www.gibraltarfurniture.com/gibraltar/';
	var out = '<a href=\"' + base + idl + '.html\"><img width=100 src=\"' + img + '\"></a>';
	out = out + '<a title=\"delete from gallery\" style=\"cursor:hand\" onclick=\"removeCookieListItem(\'mygallery\',\'' + id + '\',2,\' \')\"><u>del</u></a>&nbsp;&nbsp;&nbsp;';
	document.write(out);
}

function writeGallery(name) {
	//this writes the HTML to display the gallery.
	//simply call writeGallery('mygallery')
	var itemList = getCookie(name);
	var A = itemList.split(' ');
	if (A.length > 1) {
		for (var i = 1; i <= A.length; i = i + 2) {
			writeGalleryItem(A[i-1],A[i])
			if ((i+1) % 10 == 0) document.write('<p>')
		}
	//document.write('<p><a style=\"cursor:hand\" onclick=\"clearCookie(\'' + name + '\')\"><u>Clear Gallery</u></a>')
	} else document.write('The gallery is currently empty.<p>You can save items of interest to this gallery by clicking the link \"Save to my image gallery\" which will be located just above the first photo for the item or section.')
}

function FindInList(list,item,delim) {
  //returns the ordinal of the item or -1 if not found
  //note: first item position is 0
  var index = list.indexOf(item)
  if (index < 1) return index
  else {
    var temp = list.substring(0,index-2)
	var A = temp.split(delim)
	return A.length
  }
}

function RemoveFromList(list,index,n,delim) {
  //removes n list items beginning at index.
  //note: first index position is 0
  var A = list.split(delim)
  var new_list = ''
  //alert('A.length: ' + A.length + ' index: ' + index)
  for (var i = 0; i < A.length; i++) {
  	//alert('A[' + i + ']: ' + A[i])
    if (i < index || i > index + n - 1)
      if (new_list == '') new_list = A[i]
	  else new_list = new_list + ' ' + A[i]
  }
  return new_list
}

function removeCookieListItem(name,item,n) {
  //for use when cookie value is a list.
  //removes n list items beginning at item.
  if (confirm('Remove ' + item + ' from My Image Gallery?')) {  
    var list = getCookie(name)
    var index = FindInList(list,item,' ')
    if (index == -1) alert('Not found: ' + item)
    else {
      var new_list = RemoveFromList(list,index,n,' ')
      //alert('New list: ' + new_list)
      expireCookie(name)
      setCookie(name,new_list)
      //alert('Item removed.')
      history.go(0)
    }
  }
}
