// =====================================================================================================
// Include file needed by any page that needs HBX tracking capabilities.
// by apuga - July 11-30, 2007
// =====================================================================================================
// CALLING THIS SCRIPT
// -------------------
// You must first define any of the mtvhbx_* variables that you wish in script tags in the body section
// (inside script tags) of the page you're calling from. All declarations/assignments are OPTIONAL, but
// if not explicitly declared, HBX system default string codes will be used except when trackArticleID
// is set to true - if it is, the default for hbx.pn will be modified to the filename with the ID.
//
// The variable declarations must appear ABOVE the line with the droplet that calls the javascript.jtml
// include file as the rest of the HBX handling resides in that file.
//
// SAMPLE CALL:
// ------------
// <!-- MTV HBX CODE -->
//	<script language="Javascript">
//		var mtvhbx_trackSearch = true;
//	</script>
// <!-- /MTV HBX CODE -->
// ...
// <droplet src="/includes/javascript.jhtml" >
//
// =====================================================================================================

// We are anticipating that certain values might be set in the file that calls this include.
// This checks to see whether or not the variables are defined at all, if not assign the default values.

if (typeof mtvhbx_pageName == 'undefined') { var mtvhbx_pageName = "PUT+PAGE+NAME+HERE"; }
if (typeof mtvhbx_pageTitle == 'undefined') { var mtvhbx_pageTitle = "title"; }
if (typeof mtvhbx_contentCat == 'undefined') { var mtvhbx_contentCat = "CONTENT+CATEGORY"; }
if (typeof mtvhbx_contentCatDefault == 'undefined') { var mtvhbx_contentCatDefault = "full"; }
if (typeof mtvhbx_trackArticleID == 'undefined') { var mtvhbx_trackArticleID = false; }
if (typeof mtvhbx_trackSearch == 'undefined') { var mtvhbx_trackSearch = false; }
if (typeof mtvhbx_searchResults == 'undefined') {	var mtvhbx_searchResults = 0; }


// Function getParameter accepts a query string variable as a parameter and returns its value
// Returns null if the variable does not exist in the query string
// Pooched from: http://www.janetsystems.co.uk/Articles/JavascriptArticles/tabid/72/itemid/763/Default.aspx
function getParameter( parameterName ) {
  var queryString = window.location.search.substring(1).toLowerCase();
  //alert(queryString);
  //if (queryString.length==0) {return "null";}
  var parameters = new Array();
  parameters = queryString.split('&');
  for(var i = 0; i < parameters.length; i++) {
    //alert(parameters[i]);
    //alert(parameters[i].indexOf(parameterName));
    if (parameters[i].indexOf(parameterName.toLowerCase())>=0) {
      //alert(parameters[i]);
      var parameterValue = new Array();
      parameterValue = parameters[i].split('=');
      return parameterValue[1];
    }
  }
  return "null";
}

// Function to return a filename only (ie: strips out path and query string)
// Modified from a version found here: http://www.math.grin.edu/~rebelsky/Tutorials/JavaScript/EdMedia97/more.html
function extractFileName()
{
  var lastSlash // the position of the last slash in the path
  var fileName  // the name of the file
  var questmarkPos // position of first question mark (in case of parameters being passed)
  var pathName = window.location.pathname + window.location.search;

  lastSlash = pathName.lastIndexOf("/");
  questmarkPos = pathName.indexOf("?");

  if (questmarkPos == -1)
  {
	fileName = pathName.substring(lastSlash+1,pathName.length);
  }
  else
  {
	fileName = pathName.substring(lastSlash+1, questmarkPos);
  }

  return fileName
} // extractFileName


// Below is the heavily-modified HBX "page code" in all its glory
// MTV.ca in-house modifications preceeded in comments by "MTV:"

// <!--WEBSIDESTORY CODE HBX2.0 (Universal)-->
// <!--COPYRIGHT 1997-2005 WEBSIDESTORY,INC. ALL RIGHTS RESERVED. U.S.PATENT No. 6,393,479B1. MORE INFO:http://websidestory.com/privacy-->
// <script language="javascript">
var _hbEC=0,_hbE=new Array;function _hbEvent(a,b){b=_hbE[_hbEC++]=new Object();b._N=a;b._C=0;return b;}
var hbx=_hbEvent("pv");hbx.vpc="HBX0200u";hbx.gn="ehg-ctv.hitbox.com";

//BEGIN EDITABLE SECTION
//CONFIGURATION VARIABLES
hbx.acct="DM561030INRA71EN3;DM561030KMRC71EN3";//ACCOUNT NUMBER(S)


// MTV: section below replaces: hbx.pn="PUT+PAGE+NAME+HERE";
if (mtvhbx_trackArticleID)
{
	mtvhbx_pageName = extractFileName();

	if (getParameter('id') !== null)
	{
		hbx.pn= mtvhbx_pageName + "+-+ID=+" + getParameter('id');//PAGE NAME(S)
		// spits out (example): /aboutus/ABOUT US TEST1 - ID= 1606
	}
	else if (getParameter('cid') !== null)
	{
		hbx.pn= mtvhbx_pageName + "+-+CID=+" + getParameter('cid');//PAGE NAME(S)
	}
	else
	{
		hbx.pn= mtvhbx_pageName + "+-+ID=unknown";//PAGE NAME(S)
	}
}
else if (mtvhbx_trackSearch)
{
	// This branch grabs the search parameters on search result pages
	// alert ("Get Param (input) = " + getParameter('input'));

	getFileName = extractFileName();

	searchString = getParameter('input');
	// Change all spaces and '%20' codes to plus signs for HBX compatibility
	// the Replace method takes a regular expression as the first parameter.
	// the /gi flags that follow it set it to change globally (all instances) and make it case insensitive
	searchString = searchString.replace (/%20/gi,"+");
	searchString = searchString.replace (/ /gi,"+");
	// hbx.pn= mtvhbx_pageName + "+-+input=+" + searchString;//PAGE NAME(S)
	hbx.pn= getFileName; //PAGE NAME(S)

	// This section is special search term handling for HBX
	// This code sample taken from the HBX installation_guide.pdf, pg. 146

	var ev1 = _hbEvent("search"); // required definition to create custom event
	ev1.keywords = searchString; // required value

	ev1.results = mtvhbx_searchResults; // integer value required. Any number of results, or 0 to have the keyword displayed in the Failed Keywords report

	ev1.attr1 = getFileName; // optional, can be any string search attribute
	//ev1.attr2 = ""; // optional, can be any string search attribute
	//ev1.attr3 = ""; // optional, can be any string search attribute
	//ev1.attr4 = ""; // optional, can be any string search attribute
}
else
{
	hbx.pn= mtvhbx_pageName;//PAGE NAME(S)
}

// MTV: Section below replaces: hbx.mlc="CONTENT+CATEGORY";//MULTI-LEVEL CONTENT CATEGORY
hbx.mlc= mtvhbx_contentCat;//MULTI-LEVEL CONTENT CATEGORY

// MTV: Section below replaces: hbx.pndef="title";//DEFAULT PAGE NAME
hbx.pndef= mtvhbx_pageTitle;//DEFAULT PAGE NAME

// MTV: Section below replaces: hbx.ctdef="full";//DEFAULT CONTENT CATEGORY
hbx.ctdef= mtvhbx_contentCatDefault;//DEFAULT CONTENT CATEGORY




//OPTIONAL PAGE VARIABLES
//ACTION SETTINGS
hbx.fv="";//FORM VALIDATION MINIMUM ELEMENTS OR SUBMIT FUNCTION NAME
hbx.lt="none";//LINK TRACKING
hbx.dlf="n";//DOWNLOAD FILTER
hbx.dft="n";//DOWNLOAD FILE NAMING
hbx.elf="n";//EXIT LINK FILTER

//SEGMENTS AND FUNNELS
hbx.seg="";//VISITOR SEGMENTATION
hbx.fnl="";//FUNNELS

//CAMPAIGNS
hbx.cmp="";//CAMPAIGN ID
hbx.cmpn="";//CAMPAIGN ID IN QUERY
hbx.dcmp="";//DYNAMIC CAMPAIGN ID
hbx.dcmpn="";//DYNAMIC CAMPAIGN ID IN QUERY
hbx.dcmpe="";//DYNAMIC CAMPAIGN EXPIRATION
hbx.dcmpre="";//DYNAMIC CAMPAIGN RESPONSE EXPIRATION
hbx.hra="";//RESPONSE ATTRIBUTE
hbx.hqsr="";//RESPONSE ATTRIBUTE IN REFERRAL QUERY
hbx.hqsp="";//RESPONSE ATTRIBUTE IN QUERY
hbx.hlt="";//LEAD TRACKING
hbx.hla="";//LEAD ATTRIBUTE
hbx.gp="";//CAMPAIGN GOAL
hbx.gpn="";//CAMPAIGN GOAL IN QUERY
hbx.hcn="";//CONVERSION ATTRIBUTE
hbx.hcv="";//CONVERSION VALUE
hbx.cp="null";//LEGACY CAMPAIGN
hbx.cpd="";//CAMPAIGN DOMAIN

//CUSTOM VARIABLES
hbx.ci="";//CUSTOMER ID
hbx.hc1="";//CUSTOM 1
hbx.hc2="";//CUSTOM 2
hbx.hc3="";//CUSTOM 3
hbx.hc4="";//CUSTOM 4
hbx.hrf="";//CUSTOM REFERRER
hbx.pec="";//ERROR CODES

//INSERT CUSTOM EVENTS

//END EDITABLE SECTION

//REQUIRED SECTION. CHANGE "YOURSERVER" TO VALID LOCATION ON YOUR WEB SERVER (HTTPS IF FROM SECURE SERVER)
//</script>

// MTV: Removed below section as we aren't going to call this dynamically
// <script language="javascript1.1" src="http://www.mtv.ca/scripts/hbx.js"></script>
//<!--END WEBSIDESTORY CODE-->