// sift_elements.js - functions for implementing SIFT in HTML pages (Even for FLASH elements since they are contained in HTML pages).

// The following Arrays contain values that let us test conditions before any tracking is done for a given user action
// siftProtocals (protocols that this SIFT tracking implementation is valid for)
// sift_domains (domains that this SIFT implementation is valid for)
// sift_elements (pages, buttons, other items)
// NOTE: This should be in the JS for efficiency but it is not clear if it will be efficient to publish this list into this
// JS file.  Revise this feature as soon as it becomes inconvenient to manually enter the items here.

var sift_protocols = new Array();
sift_protocols[0] = "http";
sift_protocols[1] = "https";

var sift_domains = new Array();
sift_domains[0] = "www.musiqdirector.com";

var sift_elements = new Array();
sift_elements[0] = 'Video';
sift_elements[1] = 'SignUp';
sift_elements[2] = 'Contact';
sift_elements[3] = 'FAQ';


// The function that pages call in order to be tracked by SIFT
// NOTE: you can call this function from FLASH using Action Script: getURL("Javascript:sift_track_campaign_element('campaign','element');", "_self", "GET");

// DEPRECATED
// WRAPPER to new sift_track_campaign_element
function bamTracker(campaign,element) {
	sift_track_campaign_element (campaign, element);
	return;
}

function sift_track_campaign_element (campaign,element) {

	// Local variables
	var sift_protocol;
	var sift_domain;
	var sift_campaign;
	var sift_element;


	// set sift_protocol for this pageview/action to be validated before logging
	sift_protocol = document.location.protocol;

	// set sift_domain for this pageview/action to be validated before logging
	sift_domain = document.location.hostname;

	// set sift_campaign for this pageview/action to be validated before logging
	if(campaign) {
		sift_campaign = campaign;
	} else {
		sift_campaign = sift_domain;
	}

	// set sift_element for this pageview to be validated before logging
	if(element) {
		sift_element = element;
	} 
	else {
		sift_element = document.location.pathname + document.location.search + document.location.hash;
	}

	if ((sift_referrer = get_query_param('sift_ref')) == 'none') {
		sift_referrer = get_query_param('bam_ref');
	}

	// if no sift_ref, check for bam_ref

	sift_search = document.location.pathname + document.location.search + document.location.hash;


	// set SiftLastElement Cookie to be checked on each page load to allow us to ignore reloads of the content as impressions
	if(!get_cookie('SiftLastElement')) {
		set_cookie('SiftLastElement', sift_element, 1);
		var last_element = '';
	} 
	else {
		var last_element = get_cookie('SiftLastElement');
	}

	// if the content was not reloaded, create an image tag that loads the /SIFT/sift_track_element.php logging application
	if (last_element != sift_element) {

		// if the domain is a valid SIFT domain (You could also test for is_sift_element() and is_sift_protocol() 
		// here if you do not  want to track every single action, for each user on the domain.)
		// if(is_sift_domain(sift_domain) && is_sift_element(sift_element)) 

		if (is_sift_domain(sift_domain)) {

			if (document.getElementById("sift_img")) {
				var sift_img = document.getElementById("sift_img");
			} 
			else {
				var sift_img = document.createElement('img');
			}

			// Set the IMG tag attributes
			sift_img.setAttribute("id", "sift_img");

			sift_img.setAttribute("src", document.location.protocol + "//" + document.domain 
					+ "/SIFT/sift_track_element.php?sift_element=" + escape(sift_element) 
									+ "&sift_campaign=" + sift_campaign 
									+ "&sift_referrer=" + sift_referrer 
									+ "&sift_search=" + sift_search);

			sift_img.setAttribute("alt", "");
			sift_img.setAttribute("width", "1");
			sift_img.setAttribute("height", "1");

			// Set some style attributes to make the IMG blend in a little better with the rest of the document
			sift_img.style.position="absolute";
			sift_img.style.bottom="0";
			sift_img.style.left="0";
			sift_img.style.border="none";

			// Insert the IMG into the document (this is the containing HTML document if you are calling this from Flash)
			document.body.appendChild(sift_img);
			set_cookie('SiftLastElement', sift_element, 1);
		}
	} 
	// No element change
	else {
		return;
	}
}  // end sift_track_campaign_element


function set_cookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
}


function get_cookie(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 null
}


function is_sift_protocol(sift_protocol) {
	for (i = 0; i < sift_protocols.length; i++) {
		if (sift_protocols[i] == sift_protocol) {
			return true;
		}
	}
	return false;
}


function is_sift_domain(sift_domain) {
	for (i=0;i<sift_domains.length;i++) {
		if(sift_domain.indexOf(sift_domains[i])!=-1) {
			return true;
		}
	}
	return false;
}


function is_sift_element(sift_element) {
	for (i=0;i<sift_elements.length;i++) {
		if(sift_elements[i] == sift_element) {
			return true;
		}
	}
	return false;
}

function get_query_param (search_param) {

    if (document.location.search) {
        var url = document.location + '';
        q=url.split('?');

        if (q[1]) {
            //Get all Name/Value pairs from the QueryString
            var pairs = q[1].split('&');
            for (i=0;i<pairs.length;i++) {

                //Get the Name from given Name/Value pair
                var keyval = pairs[i].split('=');

                if (keyval[0] == search_param) {
                //Get the Value from given Name/Value pair and set to the return ID
                    return keyval[1];
                }
            }
        }
    }
	return 'none';
}  // end get_query_param (search_param)

