﻿// stock.js
// uglyfriend stock alerts client side script

var offers = new Object();

var c_activationText = "When new stock is available you will receive a pop-up alert just like the very one " +
  "you are reading! Now don't sit staring at the page, go about your work, and when there is stock we shall " +
  "alert you.";
var c_audioText = "Press the OK button below if you want to know what 'sound' you should be " +
  "listening out for when new stock arrives.\n\nIf you like surprises, press CANCEL, turn your " +
  "speakers up and carry on with your day - if stock comes in you'll know about it!";
  
var c_headerText = "The following merchants have stock available:";
var c_footerText = "Click link on site to visit store!";
var c_audioSrc = "";

var browser_safari = (navigator.appVersion.indexOf("Safari") != -1);

function doAlerts(siteName)
{
  // execute only if cookie set to enable alerts
  if (getCookie("alertenabled") == "true" || getCookie("audioenabled") == "true") 
  {   
    
    // set checkboxes on page refresh
    if (document.getElementById("alertCheck")) {
      document.getElementById("alertCheck").checked = (getCookie("alertenabled") == "true"); }
    if (document.getElementById("audioCheck")) {
      document.getElementById("audioCheck").checked = (getCookie("audioenabled") == "true"); }
    
    var alertCount = 0;
    var alertText = "";
    
    var oldCookieStr = getCookie(window.location.pathname)
    var newCookieStr = ""
    for (id in offers)
    {
      if (offers[id].availability == "available" && findAvailability(oldCookieStr, id) != "ava")
      {
        alertCount += 1;
        alertText += "\\n\\u2022 " + offers[id].name.toUpperCase();
      }
      // set cookie containing current availaiblity
      newCookieStr += id + ":" + offers[id].availability.substr(0, 3) + ',';
    }
    setCookie(window.location.pathname, newCookieStr);
    
    // display the alert
    if (alertCount > 0)
    {
      // focus window
      window.focus();
      // play sound
      if (getCookie("audioenabled") == "true" && c_audioSrc != "") {
        playSound(c_audioSrc); }
      // show popup
      if (getCookie("alertenabled") == "true") {
        setTimeout("window.alert(\"" + siteName + " ALERT!\\n" + c_headerText + "\\n" + alertText + "\\n\\n" + c_footerText + "\\n\");", (browser_safari ? 500 : 50)); }
    }
  }
}

function primeAlerts() {
  // set cookies from offers array
  var newCookieStr = ""
  for (id in offers)
  {
    // set cookie containing current availaiblity
    newCookieStr += id + ":" + offers[id].availability.substr(0, 3) + ',';
  }
  setCookie(window.location.pathname, newCookieStr);
}

function toggleAlert()
{
  var alertCheck = document.getElementById("alertCheck");
  setCookie("alertenabled", alertCheck.checked);
  
  // show activation alert
  if (alertCheck.checked)
  { 
    primeAlerts();
    window.alert(c_activationText); 
  }
}

function toggleAudio()
{
  var alertCheck = document.getElementById("audioCheck");
  setCookie("audioenabled", alertCheck.checked);
  
  if (alertCheck.checked) { 
    // also turn on pop-up alerts
    var popupCheck = document.getElementById("alertCheck");
    popupCheck.checked = true;
    setCookie("alertenabled", true);
    // prime cookies with availability
    primeAlerts();
    // show activation alert
    if(window.confirm(c_audioText)) { playSound(c_audioSrc); } 
  }
}

function findAvailability(codedString, id)
{
  if (codedString.length>0)
  {
    c_start = codedString.indexOf(id + ":");
    if (c_start != -1)
    { 
      c_start = c_start + id.length + 1; 
      c_end = codedString.indexOf(",",c_start);
      if (c_end == -1) c_end = codedString.length;
      return codedString.substring(c_start,c_end);
    } 
  }
  return "";
}

// unwrap fn takes a single item array and returns the object within as silly wordpress 
// likes to encode quotes inside my script blocks, but not if they're in square brackets. 

function unwrap(singleItemArray) {
  return singleItemArray[0];
}


/* generic js cookie manipulation functions */

function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

function setCookie(c_name, value)
{
  document.cookie= c_name + "=" + escape(value);
}

function popWin(url) {
  var w = window.open(url, "_blank", "width=440,height=312,left=100,top=100,menubar=no,toolbar=no,location=no,resizable=yes");
  return false;
}

function playSound(src) {
  document.getElementById("embedHolder").innerHTML = 
    "<embed src=\"" + src +"\" autostart=\"true\" playcount=\"1\" loop=\"false\" height=\"0\" width=\"0\" />";
  // excluding type=\"application/x-mplayer2\" attribute from embed tag for now...
  }