var feed2JS4VimeoTOObj;
var feed2JS4FlickrTOObj;
var insertVimeoTOObj;

var currentVimeoVideo;
var currentFlickrPhoto;

function parseFeed2JS4Vimeo(feedContainerId, largeContainerId, thumbWidth, largeWidth) {
	if(thumbWidth < 50) {
		thumbWidth = 100;
	}
	
	thumbHeight = parseInt((thumbWidth * .675));
	
	videoFeed2JSObj=document.getElementById(feedContainerId);
	//alert(videoFeed2JSObj);
	if(videoFeed2JSObj) {
		listItems = videoFeed2JSObj.getElementsByTagName('li');
		//alert(listItems);
		if(listItems) {
			clearTimeout(feed2JS4VimeoTOObj);
			for(x in listItems) {
				listObj = listItems[x];
				if(listObj.nodeName == 'LI') {
					allLinks = listObj.getElementsByTagName('a');
					if(allLinks) {
						titleLink = allLinks[0];
						videoURL = titleLink.href;
						titleLink.onclick = function() { return false; };
						vimeoClipID = videoURL.replace(/http:\/\/vimeo.com\//,"");
						//alert(videoURL);
						videoObjId = 'vimeoVideo' + vimeoClipID;
						thumbVideoCode = '<img src="images/video_thumb.gif" onclick="showLargeVimeoVideo(\'' + largeContainerId  + '\', \'' + vimeoClipID + '\', \'' + largeWidth + '\');" width="' + thumbWidth + '" height="' + thumbHeight + '" />';
						tempHTML = '<div class="vimeo-video" onclick="showLargeVimeoVideo(\'' + largeContainerId  + '\', \'' + vimeoClipID + '\', \'' + largeWidth + '\');">' + thumbVideoCode + '</div>';
						listObj.innerHTML = tempHTML + listObj.innerHTML;
						
					}
				}
			}
		} else {
			feed2JS4VimeoTOObj = setTimeout('parseFeed2JS4Vimeo()', 100);
		}
	} else {
		clearTimeout(feed2JS4VimeoTOObj);
	}
}

function insertVimeoLink(clipId, feedContainerId, largeContainerId, thumbWidth, largeWidth, position) {
	position = parseInt(position);
	
	if(isNaN(position)) {
		position = 0;
	}
	
	if(thumbWidth < 50) {
		thumbWidth = 100;
	}
	
	thumbHeight = parseInt((thumbWidth * .675));
	
	videoFeed2JSObj=document.getElementById(feedContainerId);
	if(videoFeed2JSObj) {
		listContainer = videoFeed2JSObj.getElementsByTagName('ul');
		if(listContainer) {
			clearTimeout(insertVimeoTOObj);
			tempListObj = document.createElement('li');
			
			thumbVideoCode = '<img src="images/video_thumb.gif" onclick="showLargeVimeoVideo(\'' + largeContainerId  + '\', \'' + clipId + '\', \'' + largeWidth + '\');" width="' + thumbWidth + '" height="' + thumbHeight + '" />';
			tempHTML = '<div class="vimeo-video" onclick="showLargeVimeoVideo(\'' + largeContainerId  + '\', \'' + vimeoClipID + '\', \'' + largeWidth + '\');">' + thumbVideoCode + '</div>';
			
			tempListObj.innerHTML = tempHTML;
		} else {
			insertVimeoTOObj = setTimeout('insertVimeoLink()', 100);
		}
	} else {
		clearTimeout(insertVimeoTOObj);
	}	
}

function showLargeVimeoVideo(divObjId, vimeoClipID, videoWidth) {
	// DON'T KEEP REPLACING IT WITH ITSELF IF YOU CAN AVOID IT
	/* SINCE THIS IS ONCLICK WE DON'T CARE	
	if(currentVimeoVideo == vimeoClipID) {
		return false;
	}*/
	
	currentVimeoVideo = vimeoClipID;
	
	if(videoWidth < 100) {
		videoWidth = 400;
	}
	
	videoHeight = parseInt((videoWidth * .675));
	
	largeVideoHTML = '<object width="' + videoWidth + '" height="' + videoHeight + '"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoClipID + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoClipID + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' + videoWidth + '" height="' + videoHeight + '"></embed></object>';
	
	replaceWithHTML(divObjId, largeVideoHTML);
}

function parseFeed2JS4Flickr(feedContainerId, largeContainerId, thumbWidth, largeWidth) {
	if(thumbWidth < 50) {
		thumbWidth = 100;
	}
	
	thumbHeight = parseInt((thumbWidth * .675));
	
	photoFeed2JSObj=document.getElementById(feedContainerId);
	//alert(videoFeed2JSObj);
	if(photoFeed2JSObj) {
		listItems = photoFeed2JSObj.getElementsByTagName('li');
		//alert(listItems);
		if(listItems) {
			clearTimeout(feed2JS4FlickrTOObj);
			imageCollection = new Array();
			
			i = 0;
			for(x in listItems) {
				listObj = listItems[x];
				if(listObj.nodeName == 'LI') {
					allImages = listObj.getElementsByTagName('img');
					if(allImages) {
						for(n in allImages) {
							if(allImages[n].nodeName == 'IMG') {
								tempWidth = allImages[n].width;
								tempHeight = allImages[n].height;
								if(tempWidth && tempHeight) {
									tempProportion = tempHeight / tempWidth;
								} else {
									tempProportion = .675;
								}
								
								if(tempProportion < .675) {
									allImages[n].width = thumbWidth;
									allImages[n].height = parseInt(thumbWidth * tempProportion);
								} else {
									allImages[n].height = thumbHeight;
									allImages[n].width = parseInt(thumbHeight / tempProportion);
								}
								
								imageCollection[i] = allImages[n];
								i++;
							}
						}
					}
				}
			}
			
			tempHTML = '<ul class="rss-items">';
			totalImages = imageCollection.length;
			//alert(totalImages);
			perRow = 3;
			numRows = Math.ceil(totalImages / perRow);
			i = 0;
			for(row = 0; row < numRows; row++) {
				tempHTML += '<li class="rss-item">';
				for(col = 0; col < perRow; col++) {
					tempHTML += '<div class="photoBox"><a href="' + imageCollection[i].src + '" target="_blank" onclick="showLargeFlickrPhoto(\'' + largeContainerId + '\', this.firstChild, \'' + largeWidth + '\');return false"><img src="' + imageCollection[i].src + '" width="' + imageCollection[i].width + '" height="' + imageCollection[i].height + '" /></a></div>';
					i++;
				}
				tempHTML += '</li>';
			}
			tempHTML += '</ul>';
			
			photoFeed2JSObj.innerHTML = tempHTML;
		} else {
			feed2JS4FlickrTOObj = setTimeout('parseFeed2JS4Flickr()', 100);
		}
	} else {
		clearTimeout(feed2JS4FlickrTOObj);
	}
}

function showLargeFlickrPhoto(divObjId, imageObj, photoWidth) {
	var photoSrc = imageObj.src;
	
	// DON'T KEEP REPLACING IT WITH ITSELF IF YOU CAN AVOID IT
	/* SINCE THIS IS ONCLICK WE DON'T CARE
	if(currentFlickrPhoto == photoSrc) {
		return false;
	}*/
	
	currentFlickrPhoto = photoSrc;
	
	if(photoWidth < 100) {
		photoWidth = 400;
	}
	
	photoHeight = parseInt((photoWidth * .675));
	
	tempWidth = imageObj.width;
	tempHeight = imageObj.height;
	if(tempWidth && tempHeight) {
		tempProportion = tempHeight / tempWidth;
	} else {
		tempProportion = .675;
	}
	
	if(tempProportion < .675) {
		newWidth = photoWidth;
		newHeight = parseInt(photoWidth * tempProportion);
	} else {
		newHeight = photoHeight;
		newWidth = parseInt(photoHeight / tempProportion);
	}
	
	largeImageHTML = '<img src="' + photoSrc + '" width="' + newWidth + '" height="' + newHeight + '" border="0" />';
	
	replaceWithHTML(divObjId, largeImageHTML);
}

function replaceWithHTML(divObjId, htmlCode) {
	divObj = document.getElementById(divObjId);
	if(divObj) {
		divObj.innerHTML = htmlCode;
	}
}

