// Pictopia RSS Parser

var xmlhttp=false;

// NOTE: Switch URL to production server below before deploying live!
//var rssFile='http://dev-d.pictopia.com/perl/feeds?provider_id=' + prov_id + '&display=' + display + lastordered + '&limit=10';
var rssFile='http://pictopia.com/perl/feeds?provider_id=' + prov_id + '&display=' + display + lastordered + '&limit=10';
var position=1;

function rssload() {
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = false;
        	}
		}
    }
	if(xmlhttp) {
		xmlhttp.onreadystatechange = processReqChange;
		xmlhttp.open("GET", rssFile, true);
		xmlhttp.send(null);
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (xmlhttp.readyState == 4) {
        // Request is loaded. Parse Data Here
        parseXML(images);
        if (xmlhttp.status == 200) {
            // ...processing statements go here...
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
        }
    }
}

function parseXML(item_no) {
	// Parse XML file
	var xmlDoc = xmlhttp.responseXML;
		
	// Channel Data (FULL VIEW)
	var Title = xmlDoc.getElementsByTagName('title').item(0).firstChild.data;
	var Link = xmlDoc.getElementsByTagName('link').item(0).firstChild.data;
	var Desc = xmlDoc.getElementsByTagName('description').item(0).firstChild.data;
	
	if(display=='0') {
	content = '<div class="ptpWidgetTop"><img src="/ptp/std3/img/pictopialogo_mini.gif" align="absmiddle" border="0" /> &#160; '
			+ '<small>The Latest Photos</small><br /><a href="http://pictopia.com/perl/gal?provider_id=' 
			+ prov_id + '"><b>' + Title.substr(32) + '</b></a></div>';
	} else {
		content = '';
	}
	
	
	// Article display amount
	if(!item_no) {
		item_no = xmlDoc.getElementsByTagName('item').length;
	}
	
	total_items = xmlDoc.getElementsByTagName('item').length;
	
	// Article display amount (FULL VIEW)
	if(display=="0") {
		content += '<div class="ptpWidget_articles">(' + item_no + ') Article Number: '
				+ '<a href="javascript:parseXML(' + images + ');">Default</a>';
				+ '<a href="javascript:parseXML(3);">3</a>';
			if(((total_items)*1)-1 >= 10) {
				content += '| <a href="javascript:parseXML(10);">10</a>';
			}
				content += ' | <a href="javascript:parseXML(null);">All</a></div>';
	} else {
		content += '';
	}
	
	// Item Data
	for (i = 0; i < item_no; i++ ) {
		itemtitle = xmlDoc.getElementsByTagName('title')[i+1];
		image = xmlDoc.getElementsByTagName('enclosure')[i];
		imageLink = xmlDoc.getElementsByTagName('link')[i+1];
		desc = xmlDoc.getElementsByTagName('description')[i+1];
		date = xmlDoc.getElementsByTagName('pubDate')[i];
		guid = xmlDoc.getElementsByTagName('guid')[i];
		
		if (guid != null && image != null && desc != null && date != null && imageLink != null) {
			//itemtitle = itemtitle.firstChild.data;
			try{
				image_url = image.getAttribute('url');
				image_link = imageLink.firstChild.data;
				desc = desc.firstChild.data;
				date = date.firstChild.data;
				
				if(itemtitle != null) {
					itemtitle = guid.firstChild.data;
				}		
 			}catch(e){} 
			// FULL VIEW
			if(display=="0") {
			content += '<div id="ptpWidget_items"><a href="' + image_link + '" target="_new">'
					+ '<img src="' + image_url + '" align="left" width="73" height="73" border="0" id="image" /></a>'
					+ '<div id="ptpWidget_text"><div class="ptpWidget_header"><a href="' + image_link + '" target="_new">' 
					+ itemtitle + '</a></div>' + '<b>' + date + '</b><br />' + desc.substr(0,160) + ' ...&#160; '
					+ '<a href="' + image_link + '" target="_new"><small>more</small></a></div></div><br clear="both" />';
			} else if (display=="1") {
			content += '<div id="ptpWidget_thumbview"><a href="' + image_link + '" target="_new">'
					+ '<img src="' + image_url + '" width="73" height="73" border="0" id="image" /></a>'
					+ '<br />' + itemtitle + '</div>';
			} else if (display=="2") {
			content += '<div id="ptpWidget_thumbview2"><a href="http://pictopia.com/perl/gal?provider_id=' 
					+ prov_id + '&name=Latest%20Photos" target="_new">'
					+ '<img src="' + image_url + '" width="98" height="98" border="0" id="image" /></a>'
					+ '</div>';
			} else if (display=="3") {
			content += '<div id="ptpWidget_thumbview3"><a href="' + image_link + '" target="_new">'
					+ '<img src="' + image_url + '" width="128" height="128" border="0" id="image" title="' + itemtitle + '" /></a></div>';
			} else {
			content += '<b>No Display Chosen</b>';
			}
		}
	}
	
	
	
	// Page Generator
	document.getElementById('ptpWidget').innerHTML = content;
}

