var sCodeCustom = (function ($) {
	function getCookie( cookie_name ) {
		var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
		if ( results ) {
			return ( unescape( results[2] ) );
		} else {
			return null;
		}
	}

	function deleteCookie( cookie_name ) {
		var cookie_date = new Date();
		cookie_date.setTime( cookie_date.getTime() - 1 );
		document.cookie = cookie_name += "=; path=/; expires=" + cookie_date.toGMTString();
	}

	function cleanURL(url) {
		var urlRemoveHash, urlRemoveQuery, urlResult;

		urlRemoveHash = url.split('listen/#');

		if ( typeof urlRemoveHash[1] != 'undefined' ) {
			urlRemoveHash = urlRemoveHash[1];
			urlRemoveQuery = urlRemoveHash.split('?');
		} else {
			urlRemoveQuery = url.split('?');
		}
		urlResult = urlRemoveQuery[0];

		if ( urlResult.indexOf( '/search/' ) != -1 ) {
			urlResult = '/search/';
		}

		urlResult = urlResult.replace('http://'+location.hostname, '');

		urlResult = urlResult.replace('/:', '/');

		return urlResult;
	}

	function getSearchVariable(variable) {
		var result = '',
			hash = location.hash.toString(),
			hashSplit = hash.split('/');

		if ( hash.indexOf( '/search/' ) != -1 ) {
			if ( variable === 'mode' ) {
				result = hashSplit[2];
			} else if ( variable === 'query' ) {
				result = hashSplit[3];
			}
		}

		return result;
	}

	function download() {
		var albumDownload = $('.album-download'), trackDownload = $('.track-download'),
			dlString, artist = $('.headline h1').text(), album = $('.headline h2').text(), track;

		albumDownload.click(function(e){
			dlString = 'DL | ' + artist + ' | ' + album + ' | full';

			s.eVar24 = dlString;
			s.prop18 = dlString;
			s.events = 'event5';
			void(s.t());
		});

		trackDownload.click(function(){
			track = $(this).parent('td').siblings('.track-name').text();
			dlString = 'DL | ' + artist + ' | ' + album + ' | track | ' + track;

			s.eVar24 = dlString;
			s.prop18 = dlString;
			s.events = 'event5';
			void(s.t());
		});
	}

	function inSection( needle, haystack ) {
		var i = needle.length;
		while (i--) {
			if ( haystack.indexOf(needle[i]) != -1 ) {
				return needle[i];
			}
		}
		return -1;
	}

	function objInSection( needle, haystack ) {
		for ( i in needle ) {
			if ( haystack.indexOf(needle[i].slug) != -1 ) {
				return needle[i].name;
			}
		}
		return -1;
	}

	function init() {
		var linkTags, oatmeal, snid, emid, rsc, fref, url, cleanedURL,
			tmpReferrer, tmpReferrerStripped, searchMode, searchQuery,
			prop3Sections, section, sectionURL;

		oatmeal = new Cookie({
			name : 'oatmeal',
			path : '/',
			pairDelimiter : '&',
			keyValueDelimiter : '='
		});
		oatmeal.read();

		snid = oatmeal.get('sn');
		emid = getCookie('EMID');
		rsc = oatmeal.get('rsc');
		fref = oatmeal.get('t');

		url = EMUSIC_GLOBAL.canonicalURL();
		if ( typeof url == 'undefined' ) {
			url = location.href.toString();
			cleanedURL = cleanURL(url);
		}
		else {
			cleanedURL = url.replace('http://'+location.hostname, '');
		}

		tmpReferrer = getCookie('omniture_tmp_referrer');
		searchMode = getSearchVariable('mode');
		searchQuery = getSearchVariable('query');

		// s is the global Omniture Site Catalyst object
		s.eVar54=s.getQueryParam('ecid');
		s.prop54=s.eVar54;

		s.eVar18 = snid;
		s.prop12 = snid;

		s.eVar19 = emid;
		s.prop13 = emid;

		s.eVar20 = rsc;
		s.prop14 = rsc;

		s.eVar21 = fref;
		s.prop15 = fref;

		s.pageURL = url;

		s.eVar16 = cleanedURL;
		s.pageName = cleanedURL;

		// set prop3 based on grouping in ticket WEBANALYTICS-17
		if ( cleanedURL === '/') {
			section = 'home';
			s.prop3 = section;
		} else {
			sectionURL = location.href.toString();
			sectionURL = sectionURL.replace('http://'+location.hostname+'/listen/#', '');
			sectionURL = sectionURL.replace(':', '');
			prop3Sections = {
				'browse' : {
					'slug' : '/browse/',
					'name' : 'Browse'
				},
				'search' : {
					'slug' : '/search/',
					'name' : 'Search'
				},
				'charts' : {
					'slug' : '/charts/',
					'name' : 'Charts'
				},
				'music-news' : {
					'slug' : '/music-news/',
					'name' : 'Music-news'
				},
				'book-news' : {
					'slug' : '/book-news/',
					'name' : 'Book-news'
				},
				'music-genres' : {
					'slug' : '/music-genres/',
					'name' : 'Music Genre'
				},
				'book-genres' : {
					'slug' : '/book-genres/',
					'name' : 'Book Genre'
				},
				'album' : {
					'slug' : '/album/',
					'name' : 'Album'
				},
				'artist' : {
					'slug' : '/artist/',
					'name' : 'Artist'
				},
				'label' : {
					'slug' : '/label/',
					'name' : 'Label'
				},
				'book' : {
					'slug' : '/book/',
					'name' : 'Book'
				},
				'radio' : {
					'slug' : '/radio/',
					'name' : 'Radio'
				},
				'community' : {
					'slug' : '/messageboard/',
					'name' : 'Community'
				},
				'corp-info' : {
					'slug' : '/info/about/',
					'name' : 'Corp info (about)'
				},
				'help' : {
					'slug' : '/info/help/',
					'name' : 'Help'
				},
				'contact' : {
					'slug' : '/info/contact/',
					'name' : 'Help'
				},
				'welcome' : {
					'slug' : '/welcome/',
					'name' : 'Landing Page'
				},
				'campaign' : {
					'slug' : '/campaign/',
					'name' : 'Landing Page'
				},
				'registration' : {
					'slug' : '/registration/',
					'name' : 'Registration'
				},
				'login' : {
					'slug' : '/log-in.php',
					'name' : 'Login'
				},
				'profile' : {
					'slug' : '/profile/',
					'name' : 'Profile'
				},
				'account' : {
					'slug' : '/account/',
					'name' : 'Account'
				},
				'closed-pitch' : {
					'slug' : '/home/',
					'name' : 'Closed Pitch'
				}
			}
			section = objInSection( prop3Sections, sectionURL);
			if ( section != -1 ) {
				s.prop3 = section;
			}
		}

		// clean out download eVar and prop, and reset events to event1
		s.eVar24 = '';
		s.prop18 = '';
		s.events = 'event1';

		if (tmpReferrer) {
			tmpReferrerStripped = tmpReferrer.split('?');
			tmpReferrerStripped = tmpReferrerStripped[0];

			if ( tmpReferrerStripped.indexOf('emusic.com') == -1 && tmpReferrerStripped.indexOf('emusic-test.com') == -1 ){
				s.referrer = tmpReferrer;
			}
		}

		deleteCookie('omniture_tmp_referrer');

		if ( searchMode != '' ) {
			s.eVar5 = searchMode;
			s.prop5 = searchMode;
		} else {
			s.eVar5 = '';
			s.prop5 = '';
		}

		if ( searchQuery != '' ) {
			s.eVar6 = searchQuery;
			s.prop6 = searchQuery;
		} else {
			s.eVar6 = '';
			s.prop6 = '';
		}

		if ( $('div#album').length && $('body.logged-in').length ) {
			download();
		}
	}

	return {
		init: init
	}
}(jQuery));
