jQuery(function($){
	var url = location.href //grab the page step (form1, form2, completed)
	var tempArray = url.split('/site/');
	tempArray = tempArray[1].split('?'); //seperate the app and the query string and store each
	var appName = tempArray[0];
	if (tempArray[1] != null)
		var queryString = tempArray[1];
	else var queryString = '';
	
	if (appName == 'Donation2') {
		appName = 'donation'; //set the app name
		tempArray = queryString.split('.');
		tempArray = tempArray[1].split('=');
		pageStep = tempArray[1]; //get the page step
		if (pageStep.indexOf('&') > 0){
			tempArray = pageStep.split('&')
			pageStep = tempArray[0]
			}
		if ($('#donFormId').length){ //if the formId is correctly embedded
			var formName = 'null'
			var formId = $('#donFormId').text(); //grab the formId
			formName = formId; //set the default name to the ID number
			if (formId == '1320') formName = 'General'; //assign custom names to commonly used forms
				else if (formId == '1340') formName = 'General IHO-IMO';
				else if (formId == '1400') formName = 'Sudan';
				else if (formId == '1440') formName = 'Sudan IHO-IMO';
				else if (formId == '1360') formName = 'Service';
				else if (formId == '1380') formName = 'Rapid Relief Fund';
				else if (formId == '1420') formName = 'Darfur Wristbands';
				else if (formId == '3002') formName = 'Hunger';
				else if (formId == '3010') formName = 'Hunger IHO-IMO';
				
			pageTracker._trackPageview('/' + appName + '/' + formName + '/' + pageStep); //track the hit for the form step
			
			if (pageStep == 'completed'){ //if the donation is complete, track the transaction
				var transID = $('#trackingCode').text() //assign all variables
				var transAmount = $('#giftAmount').text()
				var city = $('#donorCity').text()
				var state = $('#donorState').text()
				var country = $('#donorCountry').text()
				var levelID = $('#donationLevelId').text()
				pageTracker._addTrans( //track the transaction
				  transID, // order ID - required
				  "", // affiliation or store name
				  transAmount, // total - required
				  "", // tax
				  "", // shipping
				  city, // city
				  state, // state or province
				  country // country
				);
				pageTracker._addItem( //add the donation item to the transaction record
				  transID, // order ID - required
				  levelID, // SKU/code
				  formName, // product name
				  "", // category or variation
				  transAmount, // unit price - required
				  "1" // quantity - required
				);
				pageTracker._trackTrans(); //log the transaction
			}
		}
		else pageTracker._trackPageview('/' + appName + '/unknown/' + pageStep);
	}
	else if (appName == 'Advocacy'){ //for advocacy pages
		appName = 'advocacy';
		tempArray = queryString.split('&'); //split the params
		for (var x = 1; x < tempArray.length; x++){
			propArray = tempArray[x].split('=');
			if ((propArray[0] == 'page') || (propArray[0] == 'pg')) var pageStep = propArray[1];	//identify the page step in the query string
			else if ((propArray[0] == 'id') || (propArray[0] == 'alertId')) var campaignid = propArray[1]; //identify the advoacy campaign id in the query string.
		}
		pageTracker._trackPageview('/' + appName + '/' + campaignid + '/' + pageStep); //track the hit for the form step
	}
	else if (appName == 'PageServer'){
		if (queryString.indexOf('&') > 0){ //remove all session variables from query string
			tempArray = queryString.split('&');
			tempArray = tempArray[0].split('=');
			var page = tempArray[1]; //store only the page
		}
		else {
			tempArray = queryString.split('=');
			var page = tempArray[1];
		}
		pageTracker._trackPageview('/' + appName + '/' + page); //track the hit
	}
	else if ((appName.search(/^S?PageNavigator/i)) >= 0){
		var appArr = appName.split('/')
		pageTracker._trackPageview('/' + appArr[0] + '/' + appArr[1]); //track the hit
	}
	else if (appName == 'Ecard'){
		appName = 'ecard'
		if ($('h3.ObjTitle').text() == 'Thank You for Sending an eCard'){
			var page = $('.eCards a:last').attr('href').split('=')[1] + '/thankyou'
		}
		if (queryString.indexOf('&') > 0){ //remove all session variables from query string
			tempArray = queryString.split('&');
			tempArray = tempArray[0].split('=');
			var page = tempArray[1]; //store only the page
		}
		else {
			tempArray = queryString.split('=');
			var page = tempArray[1];
		}
		

		pageTracker._trackPageview('/' + appName + '/' + page); //track the hit
	}
	else
		pageTracker._trackPageview(); //if the app is not a donation, just log the url as a hit
})
