/*
 *  Function:   track
 *
 *  Purpose:    Used by many page elements to track certain things. This can be used
 *              to track page views, comments made, videos watched, etc....
 *
 *  Parameters: siteId - the id of the site that you are on
 *              category - the category to track (i.e. PAGEVIEWS)
 *              detailTypeId - the detail type id to track (i.e. 1 = article)
 *              objectId - the id of the object you are tracking (i.e. articleId, videoId, commentId)
 *
 *  Returns:    Nothing
 */
function track(siteId, category, detailTypeId, objectId, divId) {
  var userId = (GetChip('ptvzon','sursid') != null) ? GetChip('ptvzon','sursid') : "0";  
  var timestamp = new Date();

  var img  = "<img src='http://www.tracking.performgroup.com/tracking/CallAction.html?SITEID=" + siteId;
      img += "&CATEGORY=" + category; 
      img += "&DETAILTYPEID=" + detailTypeId;
      img += "&OBJECTID=" + objectId;
      img += "&USERID=" + userId;
      img += "&TIMESTAMP=" + timestamp.getTime();
      img += "' alt='tracking' width='1' height='1' style='display:none' />";
  
  if(divId) {
    $("#" + divId).html(img);
  } else {
    document.write(img);
  }
  return;
}

/*
 *	Function:	setFavouritesCookieFromDB
 * 
 *  Purpose:	This function retrieves the specified users favourites object for the specified site, from the DB using 
 *              the tracking framework and creates a cookie in the users browser by parsing the results.
 * 
 *  Parameters: siteId - the id of the site that you are on
 *          	favouriteObjectName - the name of the favourite object to be retrieved from the DB
 *              
 * 
 * 	Returns:	Nothing
 */
function setFavouritesCookieFromDB(siteId, favouriteObjectName) {
  var userId = (GetChip('ptvzon','sursid') != null) ? GetChip('ptvzon','sursid') : "0";  
  var userName = GetChip('ptvzon','userlogin');
  var path =  "/tracking/GetUserInteractionDetails.html?SITEID="+ siteId;
      path += "&USERID="+userId;
      path += "&CATEGORY=USERINTERACTION&USERINTERACTIONTYPE=USERFAVOURITESSNAPSHOT";
      path += "&FAVOURITEOBJECTID="+favouriteObjectName;

  $.ajax({
    url: path,
    dataType: "xml",
    success: function(data) {
      var cookieValue = "";
      var assettId = "";
      var teaserImagePath = "";
      var videoHi = "";
      var videoLo = "";
      var title = "";
      var assetTsrTxt = "";
      var curl = "";
              
      $("assett", data).each(function() {
        assettId = $(this).attr("id");
        $("property", $(this)).each(function() {
          if ($(this).attr("name") != undefined && $(this).attr("name") == 'teaserImagePath') {
            teaserImagePath = $(this).text();
          } else if ($(this).attr("name") != undefined && $(this).attr("name") == 'videoLo') {
            videoLo = $(this).text();
          } else if ($(this).attr("name") != undefined && $(this).attr("name") == 'videoHi') {
            videoHi = $(this).text();
          } else if ($(this).attr("name") != undefined && $(this).attr("name") == 'title') {
            title = $(this).text();
          } else if ($(this).attr("name") != undefined && $(this).attr("name") == 'teaser') {
            assetTsrTxt = $(this).text();
          } else if ($(this).attr("name") != undefined && $(this).attr("name") == 'curl') {
            curl = $(this).text();
          }
        });
        
        cookieValue += assettId + ',"'; 
        cookieValue += teaserImagePath + '","'; 
        cookieValue += escape(videoHi) + '","'; 
        cookieValue += escape(videoLo) + '","'; 
        cookieValue += escape(title) + '","'; 
        cookieValue += escape(assetTsrTxt) + '","';
        cookieValue += escape(curl) + '",';
      });
      
      SetCookie(userName+"_"+favouriteObjectName,cookieValue,never);
    }
  });
}

/*
 * 	Function:  	trackInteraction
 * 
 * 	Purpose:	This function tracks a user interaction for a specified user for a specified site
 * 
 * 	Parameters:	siteId - id of the current site
 *              category - the category name for the interaction eg 'USERINTERACTION' 
 *              userInteractionType -  the user interaction type for the interaction eg 'USERFAVOURITESSNAPSHOT'
 *              interactionParameters - an ampersand separated list of name/value pairs which describe this interaction 
 * 	
 * 	Returns:	Nothing
 */
function trackInteraction(siteId, category, userInteractionType, interactionParameters) {
  var userId = (GetChip('ptvzon','sursid') != null) ? GetChip('ptvzon','sursid') : "0";
  var timestamp = new Date();
  var path = "/tracking/CallAction.html?SITEID="+siteId;
      path +="&USERID="+userId;
      path +="&CATEGORY="+category;
      path +="&USERINTERACTIONTYPE="+userInteractionType;            
      path +="&TIMESTAMP="+timestamp.getTime();            
      path +="&"+interactionParameters;
  $.get(path);
}

/*
 * 	Function: 	trackCookieFavouritesSnapShot
 * 
 * 	Purpose:	This function takes a snap shot of a users favourites cookie using the tracking framework
 * 
 * 	Parameters:	siteId - id of the current site
 * 				detailTypeId - The detail type id for the favourites assetts
 * 				favouriteObjectName - The name for the favourites object
 *              favouriteObjectCookieValue - the value of the cookie wewe are taking a snapshot of
 * 
 * 	Returns:	Nothing
 * 
 */
function trackCookieFavouritesSnapShot(siteId, detailTypeId, favouriteObjectName, favouriteObjectCookieValue) {
  var favouriteSnapShotValue = "";
  var interactionParameters = "";
  
  if (favouriteObjectCookieValue != null) {
    var stringFaveArray = eval('[' + favouriteObjectCookieValue + '];');
    if (stringFaveArray.length != 0) {
      for (var i=0;i<stringFaveArray.length-1;) {
        if (i != stringFaveArray.length-7) {
          favouriteSnapShotValue += stringFaveArray[i] + ',';
        } else {
          favouriteSnapShotValue += stringFaveArray[i];
        }
        i = i + 7;
      }
    }
  }
  
  interactionParameters += "ASSETTS_DETAIL_TYPEID="+detailTypeId;
  interactionParameters += "&FAVOURITEOBJECTID="+favouriteObjectName;
  interactionParameters += "&FAVOURITE_ASSETTS="+favouriteSnapShotValue;
  
  trackInteraction(siteId, "USERINTERACTION", "USERFAVOURITESSNAPSHOT", interactionParameters);
}

