
AStool = {};

// object display globals
AStool.homeurl = '/sites/edinburghantiques.co.uk/modules/antique_service/';
AStool.rateCookieTimeOut = 7200; // mins, i.e. 5 days
AStool.rated = new Array();
AStool.rates = new Array();
AStool.rateCounter = 0;
AStool.rater = null;
AStool.raterVisible = false;
AStool.tagerVisible = false;
AStool.nid = -1;

// amended array method to return the index of an item
AStool.rated.getIndex = function(value) {
  var indx = -1;
  for (var i = 0;i<this.length;i++) {
    if (this[i]==value) {
      indx = i;
      break;
    }
  }
  return indx;
}


/**
 * setup
 *   add the event handlers for doing stuff
 */

AStool.setup = function() {
  AStool.nid = parseInt($("#ratenid").text());
  AStool.raterVisible = false;

  var rateindex=AStool.rated.getIndex(AStool.nid);

  if (rateindex<0) {
    // not rated, so bind the event handlers
    $("#ASRater > span").click(AStool.doRateing);
    $("#ASRaterButton").click(AStool.rateNID);
    // and set the cursor to clickable
    $("#ASRaterButton > img").css("cursor","pointer");
  } else {
    // already rated, so redraw as such
    if (AStool.nid) $("#ASRaterButton").html('<img src="'+AStool.homeurl+'/images/rated.png" alt="rate this service" width="30px" height="30px" /><div class="antiqueNote">Rated '+AStool.rates[rateindex]+'</div>');
  }

  // bind the tag event handlers
  $("#ASTagButton").click(AStool.tagNID);
  $("#ASTagButton > img").css("cursor","pointer");
  $("#ASTager > img").mouseover(AStool.tagHighlight);
  $("#ASTager > img").mouseout(AStool.tagUnHighlight);
  $("#ASTager > img").click(AStool.doTagging);
}


/**
 * cleanup
 *   cleanup the events we don't want
 */

AStool.cleanup = function() {
  $("#ASRater > span").unbind("click");
  $("#ASRaterButton").unbind("click");
  $("#ASTagButton").unbind("click");
  $("#ASTager > img").unbind();
}


/**
 * setCookie
 *   set the cookie with nid,rank paris
 */

AStool.rateCookieSet = function(mins) {
  if (mins) {
    var date = new Date();
    date.setTime(date.getTime()+(mins*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else var expires = "";
  
  var value = AStool.rated.join('A')+'A'+AStool.rates.join('B');
  document.cookie = "AStoolRate="+value+expires+"; path=/";
};


/**
 * rateCookieGet
 *   get the cookie with and repopulate the arrays
 */

AStool.rateCookieGet = function() {
  var result = null;
  var nameEQ = "AStoolRate=";
  var ca = document.cookie.split(';');
  var value = null;
  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) {
      value=c.substring(nameEQ.length,c.length);
      break;
    }
  }
  if (value) {
    AStool.rateCounter = 0;
    var setA = value.split('A');
    var setB = (setA[setA.length-1]).split('B');
    for (var i = 0;i < setB.length;i++) {
      AStool.rated[AStool.rateCounter] = parseInt(setA[i]);
      AStool.rates[AStool.rateCounter] = parseInt(setB[i]);
      AStool.rateCounter++;
    }
  }
};


/**
 * doRating
 *   actually perform the rating!
 */

AStool.doRateing = function() {
  var value = parseInt($(this).text());
  AStool.rated[AStool.rateCounter] = AStool.nid;
  AStool.rates[AStool.rateCounter] = value;
  AStool.rateCounter++;
  // let the user know its happened!
  $("#ASRaterButton").html('<img src="'+AStool.homeurl+'/images/rated.png" alt="rate this service" width="30px" height="30px" /><div class="antiqueNote">Rated '+value+'</div>');
  $("#ASRater").hide("fast");
  $("#ASRaterButton > img").css("cursor","");

  // show the user the new values based on their rating
  //var prev = $(".antiqueNote:first").text();
  //var words = new Array();
  //words = prev.split(' ');
  //var avg = parseFloat(words[2]);
  //var N = parseInt(words[4]);
  //var newavg = ((avg*N)+value)/(N+1);
  //$(".antiqueNote:first").html('(Average of '+newavg.toFixed(1)+' from '+(N+1)+' ratings).');

  // send it off to the server
  $.ajax({
    type: "GET",
    url: "/antiquesXML/rate/"+AStool.nid+"/"+value,
    dataType: "xml"});

  // set the rate cookie
  AStool.rateCookieSet(AStool.rateCookieTimeOut);
}

/**
 * rateNID
 *   respond to a rate button click
 */

AStool.rateNID = function() {
  if (AStool.rated.getIndex(AStool.nid)<0) {
    if (AStool.raterVisible) {
      $("#ASRater").hide("fast");
      AStool.raterVisible = false;
    } else {
      if (AStool.tagerVisible) {
	$("#ASTager").hide();
	AStool.tagerVisible = false;
      }
      $("#ASRater").show("fast");
      AStool.raterVisible = true;
    }
  }
}

/**
 * tagNID
 *   respond to a tag button click
 */

AStool.tagNID = function() {
  if (AStool.tagerVisible) {
    $("#ASTager").hide("fast");
    AStool.tagerVisible = false;
  } else {
    if (AStool.raterVisible) {
      $("#ASRater").hide();
      AStool.raterVisible = false;
    }
    $("#ASTager").show("fast");
    AStool.tagerVisible = true;
  }
}

/**
 * tagHighlight
 *   Highlight the tag
 */

AStool.tagHighlight = function() {
  var tagname = $(this).attr("alt");
  $("#ASTagButton > .antiqueNote").text("Tag with "+tagname);
  $("#ASTagButton > .antiqueNote").css({ color: "#444" });
}

/**
 * tagUnHighlight
 *   UnHighlight the tag
 */

AStool.tagUnHighlight = function() {
  if (AStool.tagerVisible) $("#ASTagButton > .antiqueNote").text("Tag It");
  $("#ASTagButton > .antiqueNote").css({ color:"#999" });
}

/**
 * doTagging
 *   do the rating!
 */

AStool.doTagging = function() {
  var tagname = $(this).attr("alt");
  AStool.tagerVisible = false;  
  $("#ASTager").hide("fast");
  $("#ASTagButton > .antiqueNote").text("Tagged with "+tagname);

  // get the tag key
  var tagsrc = $(this).attr("src");
  var value = tagsrc.substring(tagsrc.lastIndexOf('tag-')+4,tagsrc.length-4);

  // send of the tagging
  $.ajax({
    type: "GET",
    url: "/antiquesXML/tag/"+AStool.nid+"/"+value,
    dataType: "xml"});

}


/**
 * attachjs
 *
 */

AStool.attachjs = function() {
  AStool.rateCookieGet();
  AStool.setup();
}

if (Drupal.jsEnabled) {
  $(document).ready(AStool.attachjs);
}

