function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location);
}


/*=========================================*/
/*== Position Paypal box under displayPhoto    ==*/
/*=========================================*/
YE.onDOMReady(MovePaypalContent);

function MovePaypalContent()
{
    var paypalObj = document.getElementById("paypalContainer");
    var displayPhoto = document.getElementById("displayPhoto");
    var smugmug= document.getElementById("smugmug");
    if (displayPhoto && paypalObj && smugmug)
    {
        var paypalSetObj = paypalObj.parentNode.removeChild(paypalObj);
        displayPhoto.appendChild(paypalSetObj);
        paypalSetObj.style.display = "inline";
    }


/*=========================================*/
/*== Position Paypal Dropdown -          ==*/
/*=========================================*/
    var paypalObj = document.getElementById("paypalDropdownC");
    var cartButtonsWrapper = document.getElementById("cartButtonsWrapper");
    var smugmug= document.getElementById("smugmug");
    if (cartButtonsWrapper && paypalObj && smugmug)
    {
        var paypalSetObj = paypalObj.parentNode.removeChild(paypalObj);
        cartButtonsWrapper.appendChild(paypalSetObj);
        paypalSetObj.style.display = "inline";
    }    
}

// RECENT GALLERIES HACK

// This hack replaces your Featured Galleries with Recent Galleries as determined by the RSS feed.
//
// RSS parsing code from Paul Sobocinski found here:
// http://www.xml.com/pub/a/2006/09/13/rss-and-ajax-a-simple-news-reader.html?page=5

// NOTE: You must have one gallery set to featured, as a placeholder to make the Featured Box show up
// This placeholder gallery will not show up once the hack is in place.

// Rename Featured Galleries to Recent Galleries
// YE.onContentReady('recentPhotosBoxTitle', function() {this.innerHTML=''});

// Specify how many recent galleries you'd like to see
var rsscount = 4 ;

// Specify your RSS feed for Recent Galleries (replace XXXXXX with your site name)
// ImageCount needs to be "padded" if you have a lot of private galleries mixed in with your public 
// galleries.  If you are not seeing 6 (or however many) recent galleries, try increasing ImageCount
//
// NOTE:  If you are using a CNAME, you need to change http://XXXXXX.smugmug.com to 
// http://yourcustomdomain.com

var rssurl = "http://nzsnaps.com/hack/feed.mg?Type=nickname&Data=talkiet&format=rss200&ImageCount=10";

// Added a line to the doOnLoad function that is also used by the removeLinkFromImg() function and others.  
// So you may only need to append the if//getRSS clause below.

function doOnLoad() {
  if (YD.hasClass(document.body, "homepage")) //
     getRSS();
}

//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	this.author;
	this.comments;
	this.pubDate;

	//optional objects
	// this.category;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	// this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	// this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
		//chanElement.removeChild(itemElements[i]);
	}

	var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	var tmpElement = null;
	// this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS()
{
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		alert("not supported");

	//prepare the xmlhttprequest object
	xhr.open("GET",rssurl,true);
	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4)
		{
			if (xhr.status == 200)
			{
				if (xhr.responseText != null)
					processRSS(xhr.responseXML);
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xhr.status + " received: " + xhr.statusText);
		}
	}

	//send the request
	xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml)
{
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS);
}

//shows the RSS content in the browser
function showRSS(RSS)
{

    divTags = YD.getElementsByClassName("boxBottom", "div", YD.get("recentPhotosBox"));
    
    while(divTags[0].firstChild) divTags[0].removeChild(divTags[0].firstChild);    

//populate the items

    for (var i=0; i<rsscount && i<RSS.items.length; i++)
    {
        galimage = RSS.items[i].description;

        galimage = galimage.replace(/^.+src="(.*?)" .*/, "$1") ;
        galimage = galimage.replace(/(.*-)Th(-\d+)?(\.\w+)/i, "$1Ti$2$3") ;

        pubdate = RSS.items[i].pubDate ;
        pubdate.match(/^\w{3}, (\d\d) (\w{3}) (\d{4}) (\d\d):(\d\d):\d\d -\d\d\d\d/) ;

        // Fix up date formatting
        if (RegExp.$4>12) { meridian = 'pm'; hrs = RegExp.$4-12 } else { meridian = 'am'; if (RegExp.$4==0) { hrs = 12 } else { hrs = RegExp.$4-0 }} ;

        fixdate = "updated: " + RegExp.$2 + " " + RegExp.$1 + ", " + RegExp.$3;
// removed this to remove time and PST: + " " + hrs + ":" + RegExp.$5 + meridian + " PST" 

        miniBox = document.createElement("div");
        miniBox.className = "miniBox miniminiBox";

        photoBox = document.createElement("div");
        photoBox.className = "photo";
        miniBox.appendChild(photoBox);

        photoLink = document.createElement("a");
        photoLink.setAttribute("href", RSS.items[i].link);
        photoBox.appendChild(photoLink);

        photoImg = document.createElement("img");
        photoImg.setAttribute("border", "0");
        photoImg.setAttribute("alt", RSS.items[i].title);
        photoImg.setAttribute("title", RSS.items[i].title);
        photoImg.src = galimage;
        photoImg.className = "imgBorder";
        photoLink.appendChild(photoImg);

        albumTitle = document.createElement("p");
        albumTitle.className = "albumTitle";
        miniBox.appendChild(albumTitle);
        albumLink = document.createElement("a");
        albumLink.className = "nav";
        albumLink.setAttribute("href", RSS.items[i].link);
        albumTitle.appendChild(albumLink);

        albumLinkText = document.createTextNode(RSS.items[i].title);
        albumLink.appendChild(albumLinkText);

        albumUpdated = document.createElement("p");
        albumUpdated.className = "updated";
        miniBox.appendChild(albumUpdated);
        albumUpdatedText = document.createTextNode(fixdate);
        albumUpdated.appendChild(albumUpdatedText);

        spacerDiv = document.createElement("div");
        spacerDiv.className = "spacer";
        miniBox.appendChild(spacerDiv);

        divTags[0].appendChild(miniBox);
        // insertAfter(miniBox, divTags[0].childNodes[divTags[0].childNodes.length-2]);

    }
    spacerDiv = document.createElement("div");
    spacerDiv.className = "spacer";
    divTags[0].appendChild(spacerDiv);

    //we're done
    return true;
}

var xhr;

