
//////////////////////////////////////
// Begin Shared Functions           //
// These functions are available in //
// the site, blog and CMS           //
//////////////////////////////////////

if( ! document.SCCommon )
{
	SCCommon =
	{
		isLive : function()
		{
			return document.location.hostname.indexOf( "local" ) == -1
				&& document.location.hostname.indexOf( "cms" ) == -1
				&& document.location.hostname.indexOf( "qa" ) == -1;
		},

		// Return all values in the document cookie
		// Be fore-warned that associative arrays do not report the array.length property correctly!
		getAssociativeCookieArray : function()
		{
			var rawCookieVars = document.cookie.split( ';' );
			var cookieVars = new Array();

			for( var i = 0; i < rawCookieVars.length; i++ )
			{
				var rawCookieVar = rawCookieVars[ i ];

				while( rawCookieVar.charAt( 0 ) == ' ' )
				{
					rawCookieVar = rawCookieVar.substring( 1, rawCookieVar.length );
				}

				var cookieBits = rawCookieVar.split( '=' );
				cookieVars[ cookieBits[ 0 ] ] = unescape( cookieBits[ 1 ] );
			}

			return cookieVars;
		},

		setCookieValue : function( name, value, number_of_days )
		{
		    assignment_property = name + '=' + escape( value );

		    expires_property = '';
		    if( number_of_days )
		    {
		        // If number_of_days is non-empty, this is longer than a session-only cookie
		        var expDate = new Date();
		        expDate.setTime( expDate.getTime() + ( number_of_days * 24 * 60 * 60 * 1000 ) );
		        expires_property = '; expires=' + expDate.toGMTString();
		    }

		    cookie_payload = assignment_property + expires_property + '; path=/; domain=.sittercity.com';
		    document.cookie = cookie_payload;
		},

		//Return all values in the URL querystring
		//Be fore-warned that associative arrays do not report the array.length property correctly!
		getAssociativeQueryStringArray : function( qs )
		{
            if ( !qs ) 
            {
			    qs = '&' + document.location.search.replace( /\?/g, '&' ) + '&';
            }
		    a_get = new Array();
		    a_get_pairs = qs.split( '&' );
		    for( i = 0; i < a_get_pairs.length; i++ )
		    {
		        a_pair = a_get_pairs[ i ].split( '=' );
		        if( a_pair.length == 2 )
		        {
		            a_get[ a_pair[ 0 ] ] = unescape( a_pair[ 1 ].replace( /\+/g,' ' ) );
		        }
		    }
		    return a_get;
		}
	};

}

//////////////////////////////////////
// End Shared Functions             //
// These functions are available in //
// the site, blog and CMS           //
//////////////////////////////////////




/// #1: Fire pix.html ///

// pull the query string off of the pix.js request, not including the ?
var self_query_string = 
    getLastChild(document.lastChild).getAttribute('src').replace(/.*\?/, '');

// generate a request to pix.html, carrying over the parent page query string
var pixhref = '/pix.html' + document.location.search;

// If the above query string exists, append an '&', if not, append a '?'
pixhref += ( pixhref.indexOf( '?' ) == -1 ) ? '?' : '&';

// Throw in a couple of our own query string parameters
pixhref += 'pixreferral=' + encodeURIComponent(document.referrer) 
pixhref += '&cache=' + Math.random();

// If the pix.js request had a query string, append it as well
if (self_query_string.length > 0) {
    pixhref += '&' + self_query_string;
}

pixhref = pixhref.replace( /&amp;/g, '&' );

// place the pixel
o_pix = new Image();
o_pix.src = pixhref;

function getLastChild( el )
{
    return ( el.lastChild && el.lastChild.nodeName != '#text' ) 
        ? getLastChild(el.lastChild)
        : el;
}


