//////////////////////////////
// 
// Gallery-specific variables
//
//////////////////////////////

var navPageIndex = 0;			// What page are we in the nav
var currentImageIndex = 0;
var maxForward = 0;				// Max moved forward from the UUID. Photos/thumbnails outside this range need to be loaded before being displayed
var maxBackward = 0;			// Max moved backward from the UUID. Photos/thumbnails outside this range need to be loaded before being displayed
var currentImageUUID = '';		// UUID of the currently displayed image
var slideActive = false;		// Prevent overclicking by tracking whether or not thumbnail slider is currently animating
var adCounter = 0;				// Total ads loaded. Used to prevent double-loading upon initial window load

//////////////////////////////
//
// General variables
//
//////////////////////////////

debug.setLevel(4);
// log (5) < debug (4) < info (3) < warn (2) < error (1)

//////////////////////////////
//
// Gallery objects
//
//////////////////////////////

Thumbnail = {
	UpdateArrows: function (galleryUUID) {
		if(navPageIndex === 0) {
			$('previousThumbnails_'+galleryUUID).addClassName('disabled');
		} else {
			$('previousThumbnails_'+galleryUUID).removeClassName('disabled');
		}
		if(navPageIndex == maxPageIndex) {
			$('nextThumbnails_'+galleryUUID).addClassName('disabled');
		} else {
			$('nextThumbnails_'+galleryUUID).removeClassName('disabled');
		}
	},
	DoSlide: function (moveDist) {
		new Effect.Move('thumbnails', {x: moveDist, y: 0, mode: 'relative', duration: 0.5, transition: Effect.Transitions.sinoidal, afterFinish: function() {slideActive = false;}});
	},
	GetPrevious: function (url, emailUrl, gallery, slide, item, loadingIndicator) {
		if(navPageIndex > 0 && slideActive !== true) {
			slideActive = true;
			if(navPageIndex > maxBackward && ($('position_'+item) !== null || item == '')) {
				Thumbnail.CalculateAndDoSlide(item, false, gallery);
			} else {
				if (loadingIndicator === true) {
					$('previousThumbnails_'+gallery).addClassName('loading');
				}
				maxBackward--;
				new Ajax.Updater(
						'thumbnails', 
						'/mystyle/includes/jsp/image_navigator.jsp?'+'galleryUUID='+gallery+'&page='+(maxBackward)+'&containerUrl='+url+'&currentImageUUID='+currentImageUUID+'&emailUrl='+emailUrl, 
					{
					method: 'get',
					insertion: Insertion.Top,
					onComplete: function(transport) {
						if(transport.responseText.match(/.*href.*/)) {
							$('thumbnails').setStyle({left: '-365px'});
							if(slide){
								var currentImagePageIndex = Thumbnail.CalculateCurrentImageNavPage(item);
								var difference = currentImagePageIndex - navPageIndex;
								if(difference !== 0) {
									var amount = ((difference)*365)+'px';
									$('thumbnails').setStyle({left: amount});
								}
								Thumbnail.CalculateAndDoSlide(item, false, gallery); 
							} else {
								slideActive = false;
							}
							Thumbnail.Show(item);
						}
					}
				});
				if (loadingIndicator === true) {
					$('previousThumbnails_'+gallery).removeClassName('loading');
				}
			}
		}
	},
	GetNext: function (url, emailUrl, gallery, slide, item, loadingIndicator) {
		if(navPageIndex < maxPageIndex && slideActive !== true) {
			slideActive = true;
			if(navPageIndex < maxForward && ($('position_'+item) !== null || item === '')) {
				Thumbnail.CalculateAndDoSlide(item, true, gallery);
			} else {
				if (loadingIndicator === true) {
					$('nextThumbnails_'+gallery).addClassName('loading');
				}
				maxForward++;
				new Ajax.Updater(
						'thumbnails', 
						'/mystyle/includes/jsp/image_navigator.jsp?'+'galleryUUID='+gallery+'&page='+(maxForward)+'&containerUrl='+url+'&currentImageUUID='+currentImageUUID+'&emailUrl='+emailUrl, 
					{
					method: 'get',
					insertion: Insertion.Bottom,
					onComplete: function(transport) {
						if(transport.responseText.match(/.*href.*/)) {
							if(slide) {
								Thumbnail.CalculateAndDoSlide(item, true, gallery); 
							} else {
								slideActive = false;
							}
							Thumbnail.Show(item);
						}
					}
				});
				if (loadingIndicator === true) {
					$('nextThumbnails_'+gallery).removeClassName('loading');
				}
			}
		}
	},
	ChangeIndicator: function (url, isFirst, emailUrl, gallery, item, next, loadingIndicator) {
		var photo_count_array = $$('#thumbnails .photo_count');
		photo_count_array.each(function(photo_count_array){
			photo_count_array.hide();
		});
		if (item !== null) {
			if($('position_'+item) === null) {
				if(next) {
					Thumbnail.GetNext(url, emailUrl, gallery, true, item, loadingIndicator);

				} else {
					Thumbnail.GetPrevious(url, emailUrl, gallery, true, item, loadingIndicator);
				}
			} else {
				Thumbnail.Show(item);
				if(!isFirst) {
					Thumbnail.CalculateAndDoSlide(item, next, gallery);
				}
			}
		} else {
			photo_count_array[0].show();
		}
	},
	CalculateAndDoSlide: function(item, next, gallery) {
		if($('position_'+item) !== null) {
			var currentImagePageIndex = Thumbnail.CalculateCurrentImageNavPage(item);
			var difference = currentImagePageIndex - navPageIndex;
			Thumbnail.DoSlide(-365*difference);
			navPageIndex += difference;
			Thumbnail.Show(item);
		} else {
			if(next) {
				navPageIndex++;
				Thumbnail.DoSlide(-365); 
			} else {
				navPageIndex--;
				Thumbnail.DoSlide(365); 
			}
		}
		Thumbnail.UpdateArrows(gallery);
		//analytics_attachGalleryThumbnails(); // MS-414
	},
	CalculateCurrentImageNavPage: function(item) {
		if($('position_'+item) !== null) {
			var indices = $('position_'+item).innerHTML.split(" ");
			return parseInt((parseInt(indices[0] , 10)-1)/5, 10);
		}
		return navPageIndex;
	},
	Initialize: function(url, emailUrl, gallery) {
		new Ajax.Updater(
				'thumbnails', 
				'/mystyle/includes/jsp/image_navigator.jsp?galleryUUID='+gallery+'&page='+navPageIndex+'&currentImageUUID='+currentImageUUID+'&containerUrl='+url+'&emailUrl='+emailUrl, 
				{ 
					method:'get',
					onComplete: function() {
						Thumbnail.UpdateArrows(gallery);
						//analytics_attachGalleryThumbnails(); // MS-414
					}
				});
	},
	Show: function(item) {
		var itemName = 'position_'+item;
		$(itemName).show();
	}
};

Photo = {
	Load: function (containerUrl, galleryUUID, itemUUID, emailUrl, isFirst, next, loadingIndicator) {
	var url;
	var pars;
	if(itemUUID) {
			if(containerUrl.indexOf('#') > 0) {
				containerUrl = containerUrl.substring(0, containerUrl.indexOf('#'));
			}
			//url = containerUrl+'#'+item;
			url = containerUrl+'#'+itemUUID; // MS-670, fixed a bug that contaminate containerUrl with "whowedding ... beontv ..."
			pars = 'galleryUUID=' + galleryUUID + '&containerUrl='+ escape(url) + '&uuid=' + itemUUID + '&emailUrl='+emailUrl;
		} else {
			pars = 'galleryUUID=' + galleryUUID + '&containerUrl='+ escape(containerUrl) + '&emailUrl='+emailUrl;
		}

		new Ajax.Updater('item_container', '/mystyle/photos/includes/jsp/gallery_item.jsp', {
			parameters: pars,
			onComplete: function() {
				Thumbnail.ChangeIndicator(containerUrl, isFirst, emailUrl, galleryUUID, itemUUID, next, loadingIndicator);
				//debug.log("Gallery: onComplete for ", galleryUUID, ":", itemUUID, " (galleryUUID:itemUUID)");
				analytics_gallery_handlePhotoContainerLoad(galleryUUID, itemUUID);
				analytics_attachGalleryDynamicElements(trackingPath);	
				
		    },
			evalScripts: true
		});

		// Re-attach event listeners to page elements that get reloaded when the photo container changes
		// e.g., next/prev arrows
				
		// this little bit makes sure the ads don't load twice upon initial window load
		// since ads are globally loaded upon initial window load
		if (adCounter > 0) {
			LoadAds();  // (re)load iframe ads
		}
		adCounter=adCounter+1;
	}
};

UUID = {
	GetIndex: function(gallery, item) {
		new Ajax.Updater('notanid', '/mystyle/includes/jsp/navIndexFinder.jsp?galleryUUID='+gallery+'&uuid='+item, {
			method:'get', 
			onComplete: function(transport) {
				navPageIndex = parseInt(transport.responseText, 10);
				maxBackward = navPageIndex;
				maxForward = navPageIndex;
			}
		});
	}
};

GalleryPoll = {
	ProcessPoll: function(poll_id, choice_selected, content_id, supp_id, sectionPath,
							containerUrl, emailUrl, gallery) {
		GalleryPoll.DisableVoting();
		if(sectionPath===null || sectionPath==""){
			sectionPath="/mystyle/";
		}
		if(isNaN(choice_selected)){
			return false;
		}
		// Add a GalleryItemId parameter to let processPoll know it is coming from photo gallery
		//
		// TODO: this should originate from photo gallery callers of this 
		// method such as layoutPhotos.tag, thisOrThatGalleryPoll.tag, etc.

		var myAjax = new Ajax.Request(sectionPath+"services/processPoll.jsp",
			{
				method: 'get',
				parameters: "poll_id=" + poll_id + "&choice_selected=" + choice_selected+"&content_id="+content_id+"&supp_id="+supp_id+"&GalleryItemId=placeholderForNow",
				onComplete: function() { 
					Photo.Load(containerUrl, gallery, content_id, emailUrl, false, true, false);
					//GalleryPoll.ShowResults(containerUrl, content_id, uuid, emailUrl, poll_id, galleryUUID, storyTitle);
				}
			});
			return true;
	},
	DisableVoting: function () {
		var poll_link_array = $$('.poll_choice_link');
		poll_link_array.each(function(poll_link_array){
			poll_link_array.removeAttribute('onclick');
		});
	},
	ShowResults: function(containerUrl, currentUUID, nextUUID, emailUrl, pollId, galleryUUID, storyTitle) {
		var pars = new Ajax.Updater('tt_gallery_poll', '/mystyle/photos/includes/jsp/thisorthat_pollresults.jsp', {
			method:'get', 
			parameters: 'currentUUID='+currentUUID+'&nextUUID='+nextUUID+'&pollId='+pollId+'&galleryUUID='+galleryUUID+'&storyTitle='+escape(storyTitle)+'&containerUrl='+escape(containerUrl)+'&emailUrl='+escape(emailUrl),
			evalScripts: true
		});
	}
};



//////////////////////////////
// 
// Helper functions for hover & accordion
//
//////////////////////////////

/* suckerfish hover is used for IE6 thumbnail navigator hover styles */
sfHover = function() {
	if (document.getElementById("thumbnails")) {
		var sfEls = document.getElementById("thumbnails").getElementsByTagName("a");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			};
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			};
		}
	}
};

if (window.attachEvent) {
	window.attachEvent("onload", sfHover);
}

// suckerfish hover for IE6 this or that voting hover issues.
// This is called from layoutPhotos.tag because the element to apply
// this big bandage to is loaded dynamically 

IEisSoBrokeHover = function() {
	var sfEls = $$('h4.tt_vote');
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" fixIE";
		};
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" fixIE\\b"), "");
		};
	}
};


function loadAccordions() {
	if ($$('#photo_categories .accordion_toggle').size() > 0) {
		var photo_categories = new accordion("photo_categories", {
			onEvent: 'mouseover'
		});
		photo_categories.activate($$('#photo_categories .accordion_toggle')[0]);
	}
}

// photo category accordions
Event.observe(window, 'load', loadAccordions, false);






//////////////////////////////
//
// Analytics functions for galleries
//
//////////////////////////////


var loadOnceEventTrackersLoaded = 'false';

// Fire events to SiteCensus
function analytics_fireEvent_siteCensus(_sctitle) {
	// _sctitle = Title of the Object
	var ScImgSrc;

	var ScRandom = Math.ceil(Math.random()*1000000000);
	ScImgSrc = 'http://secure-us.imrworldwide.com/cgi-bin/m?ci=us-mystyle';
	ScImgSrc += '&amp;cg=us'; 
	ScImgSrc += '&amp;si=' + encodeURIComponent(_sctitle);   
	ScImgSrc += '&amp;cc=1';     //Cookie Check (Always on)
	ScImgSrc += '&amp;rnd=' + ScRandom;

	// create the 1x1 tracking image
	new Insertion.Bottom('photo_body', '<img src="'+ScImgSrc+'" style="display:none;" border="0" alt=""/>');
}

function analytics_gallery_makeTrackingPath(galleryTitle, itemTitle, itemCaption, galleryUUID, item){
	var trackingPath;
	
	// TODO: move provide tracking paths for other page types 
	trackingPath = galleryTitle + "/" + itemTitle + "__" + itemCaption;

	return trackingPath;
}


function analytics_gallery_getPageName_namedGallery(galleryUUID, galleryTitle, itemUUID, itemTitle, siteSection){
	var pageName;
	
	var itemTitleClean = analytics_cleanItemTitle(itemTitle);
	pageName = siteSection + ":gallery:photos:" + galleryTitle + ":" + itemUUID + ":" + itemTitleClean;
	
	return pageName;
}

function analytics_gallery_getPageName_genericGallery(galleryPhotoTitle, galleryTitle, itemUUID, itemTitle){
	var pageName;
	
	var itemTitleClean = analytics_cleanItemTitle(itemTitle);
	pageName = "gallery:photos:" + galleryTitle + ":" + itemUUID + ":" + itemTitleClean;
	
	return pageName;
}


//Map galleryUUIDs 
//
//NOTE - This is a very brittle function. This determination should be done by the server during 
//page generation and the page should merely report it back to analytics
//
// This mapping taken from /resources/jsp/photos/gallery.jsp

function analytics_gallery_getSiteSectionFromGalleryUUID(galleryUUID) {
	switch(galleryUUID)
	{
	case "2": // Cleanhouse Before & After
		return "cleanhouse";
	
	case "129": // Running in Heels
		return "runninginheels";
		
	case "139": // Messiest Home
		return "messiesthome";
		
	case "10": // Dress My Nest
		return "dressmynest";
			
	case "202": // Dallas Divas
		return "dallasdivas";
		
	default: // generic gallery
		return "";	
	}
}
	

// Analytics - send tracking events to analytics services for photo container views
function analytics_gallery_trackPhotoContainerView(galleryUUID, galleryTitle, itemUUID, itemTitle, trackingPath){
	debug.log('Gallery - analytics: trackPhotoContainerView - START');	

	// Fire Omniture photo tracking event
	var siteSection = analytics_gallery_getSiteSectionFromGalleryUUID(galleryUUID);
	var pageName;

	if (siteSection){
		debug.log('Gallery - analytics: firing Omniture tracking event for named gallery');
		pageName = analytics_gallery_getPageName_namedGallery(galleryUUID, galleryTitle, itemUUID, itemTitle, siteSection);
		debug.log("Gallery - analytics: pageName = ", pageName);
		analytics_fireEvent_Omniture_TrackPhotoView_namedGallery(pageName, galleryUUID, galleryTitle, itemUUID, itemTitle, siteSection);
	}
	else {
		debug.log('Gallery - analytics: firing Omniture tracking event for generic gallery');	
		pageName = analytics_gallery_getPageName_genericGallery(galleryUUID, galleryTitle, itemUUID, itemTitle);		
		debug.log("Gallery - analytics: pageName = ", pageName);
		analytics_fireEvent_Omniture_TrackPhotoView_genericGallery(pageName, galleryUUID, galleryTitle, itemUUID, itemTitle, galleryTitle);
	}
	
	// Fire Google Analytics tracking event
	debug.log('Gallery - analytics: firing Google Analytics tracking event');	
	pageTracker._trackPageview('/photos_ajax/' + trackingPath);
	
	// Fire SiteCensus tracking event
	debug.log('Gallery - analytics: firing SiteCensus tracking event');	
	analytics_fireEvent_siteCensus('/photos_ajax/' + trackingPath);
 
	debug.log('Gallery - analytics: trackPhotoContainerView - END');	
}

function analytics_gallery_getCurrentPhotoTitle(){
	// Get images by classname loaded into the photo gallery for tracking purposes
	var mainGalleryPhotos = $('images_container').getElementsByClassName('photoGalleryPicture');

	var galleryPhotoTitle = mainGalleryPhotos[0].title;
	
	return galleryPhotoTitle;
}

// Attach event listeners to dynamic page elements (e.g., photo prev/next buttons) 
//
// Since these elements change for every photo container, they need to be re-attached 
// every time the container is reloaded (i.e., while browsing through the gallery)

function analytics_gallery_attachDynamicElements(trackingPath) {
	debug.log('Gallery - analytics: attaching to dynamic elements - START');	
		if ($('prev_overlay')) {
			debug.log('Gallery - analytics: attaching to dynamic element - prev_overlay');	

			$('prev_overlay').observe('click', function(){
					pageTracker._trackPageview('/events/photos/prev_overlay/' + trackingPath);
				});
			}
		if ($('next_overlay')) {
			debug.log('Gallery - analytics: attaching to dynamic element - next_overlay');	
			$('next_overlay').observe('click', function(){
				pageTracker._trackPageview('/events/photos/next_overlay/' + trackingPath);
				});
			}
		if ($('prev_bottom')) {
			debug.log('Gallery - analytics: attaching to dynamic element - prev_bottom');	
			$('prev_bottom').observe('click', function(){
				pageTracker._trackPageview('/events/photos/prev_bottom/' + trackingPath);
				});
			}
		if ($('next_bottom')) {
			debug.log('Gallery - analytics: attaching to dynamic element - next_bottom');	
			$('next_bottom').observe('click', function(){
				pageTracker._trackPageview('/events/photos/next_bottom/' + trackingPath);
				});
			}

		debug.log('Gallery - analytics: attaching to dynamic elements - END');	
}

// Attach event listeners to page elements that do not change (e.g., thumbnail strip prev/next buttons)
//
// Since these page elements are not affected by AJAX page updates, they only need to be attached once

function analytics_gallery_attachStaticElements(galleryTitle, galleryUUID) {
	if (loadOnceEventTrackersLoaded != 'true') {
		if ($('previousThumbnails_'+galleryUUID)) {
			$('previousThumbnails_'+galleryUUID).observe('click', function(){
				pageTracker._trackPageview('/events/photos/thumb_scroll_left/');
			});
		}
		
		if ($('nextThumbnails_'+galleryUUID)) {
			$('nextThumbnails_'+galleryUUID).observe('click', function(){
				pageTracker._trackPageview('/events/photos/thumb_scroll_right/');
			});
		}
		
		if ($('photo_categories')) {
			$A($('photo_categories').getElementsByTagName('a')).each(function(s){
	            //if (!s.hasClassName('more')) {
				if (!$(s).hasClassName('more')) { // MS-414, make sure s is "Prototype"ize or failure in Internet Explorer will arise
					$(s).observe('click', function(){
						pageTracker._trackPageview('/events/photos/related_photo/' + galleryTitle);
					});
				} else {
					$(s).observe('click', function(){
						pageTracker._trackPageview('/events/photos/related_more/' + galleryTitle);
					});
				}
			});
		}
		
		if ($('our_picks')) {
			$A($('our_picks').getElementsByTagName('a')).each(function(s){
				$(s).observe('click', function(){
					pageTracker._trackPageview('/events/photos/our_picks/' + galleryTitle);
				});
			});
		}
		
		if ($('top_galleries')) {
			$A($('top_galleries').getElementsByTagName('a')).each(function(s){
				$(s).observe('click', function(){
					pageTracker._trackPageview('/events/photos/top_10_galleries/' + galleryTitle);
				});
			});
		}
		
		if ($('gallery_categories')) {
			$A($('gallery_categories').getElementsByTagName('a')).each(function(s){
				if (!$(s).hasClassName('all')) { // MS-414, make sure s is "Prototype"ize or failure in Internet Explorer will arise
					$(s).observe('click', function(){
						pageTracker._trackPageview('/events/photos/categories/' + s.innerHTML.stripTags().replace(/[\.,;!#\$\/:\?'\(\)\[\]_\-\\]/g, "").replace(/ /gi, "-").replace("&nbsp", "-")); // MS-414
					});
				} else {
					$(s).observe('click', function(){
						pageTracker._trackPageview('/events/photos/all_photos');
					});
				}
			});
		}
		
		if ($('next_gallery_link')) {
			$A($('next_gallery_link').getElementsByTagName('a')).each(function(s){
				$(s).observe('click', function(){
					pageTracker._trackPageview('/events/photos/top_10_galleries/' + galleryTitle);
				});
			});
		}
	
	loadOnceEventTrackersLoaded = 'true';
	}
}

// Attach event listeners to thumbnail page elements
//
// NOTE - This appears depreciated per MS-414
function analytics_gallery_attachThumbnails() {
	$A($('thumbnails').getElementsByTagName('a')).each(function(s){
		if (!$(s).hasClassName('eventTrackingLoaded')) {
			$(s).observe('click', function(){
				pageTracker._trackPageview('/events/photos/thumb_click/' + $('gallery_title').innerHTML.stripTags().replace(/[\.,;!#\$\/:\?'\(\)\[\]_\-\\]/g, "").replace(/ /gi, "-").replace("&nbsp", "-")); // MS-414
			});
			$(s).addClassName('eventTrackingLoaded');
		}
	});
}
	
function analytics_gallery_handlePhotoContainerLoad(galleryUUID, itemUUID){
	debug.log("Gallery - analytics: handlePhotoContainerLoad - START"); 

	var galleryTitle = analytics_cleanString($('gallery_title').innerHTML.stripTags());
	var itemTitle = analytics_cleanString($('item_title').innerHTML.stripTags());

	// Since itemCaption can be very long, only work with the first few characters
	var itemCaption = $('item_caption').innerHTML.stripTags();
	itemCaption = itemCaption.truncate(100);
	itemCaption = analytics_cleanString(itemCaption);

	// Now that itemCaption is clean, truncate it again to the final string
	itemCaption = itemCaption.truncate(20);
	
	var galleryPhotoTitle = analytics_gallery_getCurrentPhotoTitle();
	galleryPhotoTitle = chomp(galleryPhotoTitle);

	debug.log('Gallery - analytics: galleryUUID = ',  galleryUUID);
	debug.log('Gallery - analytics: galleryTitle = ',  galleryTitle);
	debug.log('Gallery - analytics: itemUUID = ',  itemUUID);
	debug.log('Gallery - analytics: itemTitle = ',  itemTitle);
	debug.log('Gallery - analytics: itemCaption = ',  itemCaption);	
	debug.log('Gallery - analytics: GalleryPhotoTitle = ',  galleryPhotoTitle);


	var trackingPath = analytics_gallery_makeTrackingPath(galleryTitle, itemTitle, itemCaption, galleryUUID, item);
	debug.log('Gallery - analytics: trackingPath = ',  trackingPath);	
	
	analytics_gallery_trackPhotoContainerView(galleryUUID, galleryTitle, itemUUID, itemTitle, trackingPath);

	// Re-attach event listeners to page elements that get reloaded when the photo container changes
	// e.g., next/prev arrows
	analytics_gallery_attachDynamicElements(trackingPath);	
	
	// Attach event listeners to page elements that remain static across photo container changes
	// NOTE: This should get moved to onReady() or equivalent - there's no reason to run it on every photo container load
	analytics_gallery_attachStaticElements(galleryTitle, galleryUUID);	
	debug.log("Gallery - analytics: handlePhotoContainerLoad - END"); 

}

//////////////////////////////
//
// Analytics helper functions 
//
//////////////////////////////


//Helper function to remove whitespace at the start and end of a string
function chomp(raw_text)
{ 
	raw_text = raw_text.replace(/^(\n|\r|\s)+/, '');
	raw_text = raw_text.replace(/(\n|\r|\s)+$/, '');
	return raw_text;  
}

// This is specifically for MyStyle galleries
//
// Remove " (before)" and " (after)" when reporting photos from the Clean House galleries.
// We do this to fake a logical container - e.g., "A sample room" that contains photos labelled
// "A sample room (Before)" and "A sample room (After)"
//
// NOTE - This is a very dumb function that should be revisited if MyStyle galleries get more complex
function analytics_cleanItemTitle(itemTitle){
	var itemTitleClean = itemTitle;
	itemTitleClean = chomp(itemTitleClean);
	itemTitleClean = itemTitleClean.replace(/\(before\)$/i, '');
	itemTitleClean = itemTitleClean.replace(/\(after\)$/i, '');
	itemTitleClean = chomp(itemTitleClean);
	
	return itemTitle;
}

// Basic string cleaning to remove whitespace and problematic characters
function analytics_cleanString(stringToClean){
	var cleanString = chomp(stringToClean);
	cleanString = cleanString.replace(/[\.,;!#\$\/:\?'\(\)\[\]_\-\\]/g, "");
	cleanString = cleanString.replace(/ /gi, "_");
	cleanString = cleanString.replace("&nbsp", "_");
	
	cleanString = cleanString.replace(/ /g,"-"); // replace white spaces with underscores
	cleanString = cleanString.replace(/'/g,"-"); // replace single quotes with underscores
	cleanString = cleanString.replace(/"/g,"-"); // replace double quotes with underscores
	
	return cleanString;
}

