
var phReq = null;
var phTimer = null;

function phReformatDateTimeStamp ( str, dayOfWeek ) {

	//var months = new Array ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
	var months = new Array ( "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" );
	var days = new Array ( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" );

	var re1 = /(.*)T(.*)/;
	var match = re1.exec( str );
	var dateStr = match[1];
	var timeStr = match[2];

	var re2 = /(.*)-(.*)-(.*)/;
	var dateMatch = re2.exec( dateStr );
	
	var re3 = /(.*)-(.*)/;
	var timeMatch1 = re3.exec( timeStr );
	var timeOffset = timeMatch1[2];
	
	var re4 = /(.*):(.*):(.*)/;
	var timeMatch2 = re4.exec( timeMatch1[1] );
	
	re1 = null; re2 = null; re3 = null; re4 = null;
	
	var date = new Date( dateMatch[1], dateMatch[2] - 1, dateMatch[3], timeMatch2[1], timeMatch2[2], timeMatch2[3] );
	var localDate = date.getTime();
	localDate += 1 * 3600 * 1000;
	date.setTime( localDate );
	
	var month = months[ date.getMonth() ]; 
	var year = date.getYear();
	if ( year < 200 && year >= 101 ) { year = year - 100 };
	if ( year < 70 ) { year = year + 2000 };
	var hour = date.getHours();
	var ampm = "am";
	if ( hour >= 12 ) { ampm = "pm"; };
	if ( hour > 12 ) { hour = hour - 12; };
	var min = date.getMinutes();
	if ( min <= 9 ) { min = "0" + min; };
	
	if ( dayOfWeek != 1 ) {
		var dateStr = month + "&nbsp;" + date.getDate() + ",&nbsp;" + year + "&nbsp;" + hour + ":" + min + ampm + "&nbsp;EST"
	} else {
		var day = date.getDay() - 1;
		if ( day < 0 ) day = day + 7;
		var dateStr = days[ day ];
	}

	var dateStr = month + "&nbsp;" + date.getDate() + ", " + year;

	return dateStr;
}

function phAdjustImageParamsSmall ( str ) {

	

	var re1 = /400x300/gi;
	str = str.replace( re1, '75x75' );

	var re2 = /300x400/gi;
	str = str.replace( re2, '75x75' );

	var re3 = /320x240/gi;
	str = str.replace( re3, '75x75' );
	
	var re4 = /240x320/gi;
	str = str.replace( re4, '75x75' );
	
	var re5 = /180x240/gi;
	str = str.replace( re5, '75x75' );


	var re6 = /width="*300"*/gi;
	str = str.replace( re6, 'width="75"' );

	var re7 = /width="*400"*/gi;
	str = str.replace( re7, 'width="75"' );

	var re8 = /width="*320"*/gi;
	str = str.replace( re8, 'width="75"' );

	var re9 = /height="*400"*/gi;
	str = str.replace( re9, 'height="75"' );						

	var re10 = /height="*300"*/gi;
	str = str.replace( re10, 'height="75"' );						

	var re11 = /height="*240"*/gi;
	str = str.replace( re11, 'height="75"' );						

	var re12 = /height="*180"*/gi;
	str = str.replace( re12, 'height="75"' );
		

	re1 = null; re2 = null; re3 = null; re4 = null; re5 = null; re6 = null; re7 = null; re8 = null; re9 = null; re10 = null;

	

	return str;
}

function phAdjustImageParamsLarge ( str ) {

	var re1 = /400x300/gi;
	str = str.replace( re1, '320x240' );
	
	var re2 = /300x400/gi;
	str = str.replace( re2, '320x240' );


	var re3 = /width="*400"*/gi;
	str = str.replace( re3, 'width="320"' );
		
	var re4 = /width="*300"*/gi;
	str = str.replace( re4, 'width="320"' );

	var re5 = /height="*400"*/gi;
	str = str.replace( re5, 'height="240"' );

	var re6 = /height="*300"*/gi;
	str = str.replace( re6, 'height="240"' );


	re1 = null; re2 = null; re3 = null; re4 = null; re5 = null; re6 = null;

	return str;
}



function phGetImage( str ) {
	
	

	var result = "";
	var image = null;
	var htmlElement = null;
	
	
	htmlElement = document.createElement( "div" );
	htmlElement.innerHTML = str;

	image = htmlElement.getElementsByTagName( "img" )[0];
	
	
	
	if ( image != null && image != "undefined" ) {

		
		
		var src = image.src;
		var width = image.width;
		var height = image.height;
		var alt = image.alt;
		var border = image.border;
		var theStyle = image.getAttribute( "style" );
		var cssClass = image.getAttribute( "class" );
		
		

		if ( width == 0 || width == null ) width = 320;
		if ( height == 0 || height == null ) height = 240;
		if ( border == null ) border = 0;
		
		

		result = "<img src=\"" + src + "\" " +
					"width=\"" + width + "\" " +
					"height=\"" + height + "\" " + 
					"border=\"" + border + "\" " + 
					"class=\"" + cssClass + "\" " +
					"style=\"" + theStyle + "\" " +
					"alt=\"" + alt + "\" " +
					"/>";
	} else {
		result = "";
	}
	
	
	
	htmlElement = null; image = null;
	
	
	return result;

	
};

function phAdjustURL( url ) {
	var result = url;
	
	var re = /http:\/\/instylemag.typepad.com\/partyhopper/gi;
	result = result.replace( re, 'http://partyhopper.instyle.com' );
	
	re = null;
	
	return result;
}


function phProcessReqChange() {
    
    if ( phReq.readyState == 4 ) {
		

 		
        if ( phReq.status == 200 ) {
			
			clearTimeout( phTimer );
			
			
			var listingObject = document.getElementById( "partyHopperBlog" );
			
			if ( listingObject == null ) return;
			listingObject = null;
			
			hideItem( "phLoading" );

			var items = phReq.responseXML.getElementsByTagName("item");
			
		
            for ( var i = 0; i <= 0 && i < items.length; i++ ) {
						
				var prefix = "ph_" + i;
				
				
				var title = getElementTextNS( "", "headline", items[i], 0 );
				var link = getElementTextNS( "", "url", items[i], 0 );
				link = phAdjustURL( link );
				var content = getElementTextNS( "content", "encoded", items[i], 0 );
				var description = getElementTextNS( "", "description", items[i], 0  );
				var creator = getElementTextNS( "dc", "creator", items[i], 0);
				var date = getElementTextNS( "dc", "date", items[i], 0 ); 

				
				
				var contentStr = new String( content );
				
				
				//contentStr = phAdjustImageParamsLarge( phGetImage( contentStr ) );
				contentStr = phAdjustImageParamsSmall( phGetImage( contentStr ) );
				
				substrDesc = description.substring(0,100);
				setInnerHTML( prefix + "_description", substrDesc + "..." );
				
				if (substrDesc != "") {
					contentStr = phAdjustURL( contentStr );
				}
				
				//setHref( prefix + "_link1", link );
				//setHref( prefix + "_link2", link );
				//setInnerHTML( prefix + "_link1", title );
				//setInnerHTML( prefix + "_image", "<a href=\"" + link + "\">" + contentStr + "</a>" );

				if (date != "") {
					setInnerHTML( prefix + "_date", phReformatDateTimeStamp( date, 0 ) );
				}
				
				
				displayItem( prefix );
				

			}

			displayItem( "phListing" );

         } else {
            
         }
    }
}


 
function phLoadXMLDoc( url ) {
	phReq = createXMLHttpRequest();
	if ( phReq != null ) {
		phReq.onreadystatechange = phProcessReqChange;
		phReq.open( "GET", url, true );
		phReq.send( "" );
		
		phTimer = setTimeout( function() { phReq.abort(); }, 15000 );
	};
}
