﻿var userDict = new Dictionary();

$(document).ready(function() {

    $(".techTag").hover(
        function(e) {

            // show technology panel
            var tagID = $(this).attr("tagID");
            var xMouse = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
            var context = { left: xMouse, top: $(this).offset().top, id: tagID };

            // if the technology's already downloaded, get it from the dictionary, otherwise call service
            if (userDict.Lookup(tagID) == null) {
                ToshibaMultimedia.Services.ScriptService.GetTechnologyTag(tagID, language, onServiceCompleted, onError, context);
            }
            else {
                onServiceCompleted(userDict.Lookup(tagID), context, "");
            }
        },
        function() {
            // hide technology panel
            $("#technologyPanel").stop().animate({ "opacity": "0" }, "fast");
        }
    );
});

// show service call error
function onError(e) {
   
} 

// position and show technology panel
function onServiceCompleted(result, context, methodName) {
    if (result.length > 0) {
        $("#technologyPanel").html(result);
        $("#technologyPanel").css("position", "absolute");
        $("#technologyPanel").css("z-index", 1000);
        $("#technologyPanel").css("left", context.left);
        $("#technologyPanel").css("top", context.top + 17);
        $("#technologyPanel").css("alpha", 0);
        $("#technologyPanel").show();
        $("#technologyPanel").stop().animate({ "opacity": "1" }, "fast");
        
        // add downloaded technology to the dictionary
        if (userDict.Lookup(context.id) == null)
            userDict.Add(context.id, result);
    }
}

/* technology dictionary */

function Lookup(key) 
{
  return(this[key]);
}

function Delete() 
{
  for (c=0; c < Delete.arguments.length; c++) 
  {
    this[Delete.arguments[c]] = null;
  }
  // Adjust the keys (not terribly efficient)
  var keys = new Array()
  for (var i=0; i<this.Keys.length; i++)
  {
    if(this[this.Keys[i]] != null)
      keys[keys.length] = this.Keys[i];
  }
  this.Keys = keys;
}

function Add() 
{
  for (c=0; c < Add.arguments.length; c+=2) 
  {
    // Add the property
    this[Add.arguments[c]] = Add.arguments[c+1];
    // And add it to the keys array
    this.Keys[this.Keys.length] = Add.arguments[c];
  }
}

function Dictionary() 
{
  this.Add = Add;
  this.Lookup = Lookup;
  this.Delete = Delete;
  this.Keys = new Array();
}

//------------------------
// cookie control
// -----------------------

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);
}