/*
* (c) Otineri.com 2008. All Rights Reserverd.
*/
function FlickrPhoto(id, owner, secret, server, farm, title, ispublic, isfriend, isfamily, license)
{
this.Id = id;
this.Owner = owner;
this.Secret = secret;
this.Server = server;
this.Farm = farm;
this.Title = title;
this.Ispublic = ispublic;
this.Isfriend = isfriend;
this.Isfamily = isfamily;
this.License = license;
}
function SearchBuffer(bufferPhotos, photos)
{
this.License = '';
this.User = '';
this.Keyword = '';
this.Page = null; // masked page
this.Pages = null; // masked total page
this.BufferPage = null;
this.BufferPhotos = bufferPhotos;
this.Photos = photos;
this.IsCached = function (license, user, keyword, page)
{
if (page==null) page = 1;
if (this.License!=license || this.User!=user || this.Keyword!=keyword)
{
return false;
}
else
{
return (Math.ceil(page/10) == this.BufferPage);
}
}
this.GetPhotos = function (license, user, keyword, page)
{
if (page==null) page = 1;
this.Photos.length=0;
this.License = license;
this.User = user;
this.Keyword = keyword;
this.Page = page;
var start = (((eval(page) - 1)%10) * 6);
for (var k=0; k<6 && start<this.BufferPhotos.length; k++)
{
this.Photos[k] = this.BufferPhotos[start++];
}
}
}
function FlickrObj(obj_name, callback, keyword)
{
var triggerobj;
var displacement = 1;
var f_license = "";
var f_keyword = (keyword==null ? "" : keyword);
var f_user = "";
var f_req_page;
var autosearch = (f_keyword!=null && f_keyword!="");
var searchBuffer = new SearchBuffer(new Array(), new Array());
this.SearchBuffer = searchBuffer;
this.MakeRequest = xml_request;
this.InitMenu = initMenu;
this.Handler = handler;
this.AdjustWin = adjustWin;
var imgHash = new Hashtable(); //for image pre-loader
var width = 0;
var height = 0;
var f_obj_name = obj_name;
var f_callback = callback;
var f_caller_id;
var f_click_id;
//	var nameHash = new Hashtable(); //user_id, owner_name hash
this.LinkImage = function linkImage(pid, click_id)
{
f_click_id = click_id;
//		if (nameHash.get(uid)!=null)
//		{
//			f_callback(f_caller_id, f_click_id, nameHash.get(uid));
//			return;
//		}
showMask();
makeRequest('/fa', linkImageHandler, 'method=flickr.photos.getInfo&api_key=b94cfce3cd36e916fbc63f2ab2f451fc&photo_id=' + pid);
}
function linkImageHandler()
{
try
{
if (ajax_abort || http_request.readyState == 4)
{
xHide(xGetElementById("flickr_mask"));
if(!ajax_abort && http_request.status == 200)
{
if (http_request.responseXML != null)
{
nodes = http_request.responseXML.getElementsByTagName('photo');
if (nodes.length==1)
{
var license = nodes[0].getAttribute("license");
nodes = http_request.responseXML.getElementsByTagName('owner');
if (nodes.length==1)
{
//							nameHash.put(searchBuffer.Photos[f_click_id].Owner, nodes[0].getAttribute("username"));
f_callback(f_caller_id, f_click_id, nodes[0].getAttribute("username"), license);
//							nameHash.put(searchBuffer.Photos[f_click_id].Owner, nodes[0].childNodes[0].nodeValue);
//							f_callback(f_caller_id, f_click_id, nodes[0].childNodes[0].nodeValue);
return;
}
}
}
}
}
alert("Cannot execute request, please try again later.");
}
finally
{
http_request=null;
}
}
function xml_request(caller_id, trigger, disp, license, keyword, user, p)
{
if ((xlocationmenu.triggerObj()!=trigger) ||
(!xlocationmenu.isOpen()))
{
if (http_request==null)
{
if (caller_id) f_caller_id = caller_id;
if (trigger) triggerobj = trigger;
if (disp) displacement = disp;
if (license)
{
if (license!=null) f_license = license;
if (keyword!=null) f_keyword = keyword;
if (user!=null) f_user = user;
if (searchBuffer.IsCached(license, user, keyword, p))
{
searchBuffer.GetPhotos(license, user, keyword, p);
restoreState();
}
else
{
showMask();
var url = 'method=flickr.photos.search&per_page=60&api_key=b94cfce3cd36e916fbc63f2ab2f451fc&license=' + license;
if (keyword!=null && keyword!='') url += '&text=' + encodeURIComponent(keyword);
if (user!=null && user!='') url += '&u=' + encodeURIComponent(user);
if (p==null) p = 1;
f_req_page = p;
p = Math.ceil(p/10);
makeRequest('/fa', handler, url + '&page=' + p);
}
}
else
{
this.InitMenu(displacement);
xlocationmenu.onClose();
xGetElementById("locationmenu").innerHTML = "<table id='tb001' border='0'><tr><td>" + getPhotoHTML() + "</td></tr></table>";
restoreState();
adjustWin();
if (autosearch)
{
setTimeout(function() {
autosearch = false;
document.flickr_search_form.keyword.value = f_keyword;
xml_request(null, null, null, document.flickr_search_form.flickr_license[document.flickr_search_form.flickr_license.selectedIndex].value, document.flickr_search_form.keyword.value,"");
},150);
}
}
return true;
}
}
else
{
xlocationmenu.onClose();
}
return false;
}
function showMask()
{
var obj = xGetElementById("flickr_mask");
try{
if(window.ActiveXObject){
obj.style.filter="alpha(opacity=\"60\")";
}else{
obj.style.opacity=0.6;
}
} catch (e) {}
xMoveTo(obj, 0, 0);
xResizeTo(obj, width, height);
xShow(obj);
}
function handler()
{
try
{
if (ajax_abort || http_request.readyState == 4)
{
xHide(xGetElementById("flickr_mask"));
if(!ajax_abort && http_request.status == 200)
{
if (http_request.responseXML != null)
{
nodes = http_request.responseXML.getElementsByTagName('photos');
if (nodes.length==1)
{
searchBuffer.BufferPage = nodes[0].getAttribute('page');
searchBuffer.License = document.flickr_search_form.flickr_license[document.flickr_search_form.flickr_license.selectedIndex].value;
searchBuffer.Keyword = document.flickr_search_form.keyword.value;
searchBuffer.User = document.flickr_search_form.user.value;
searchBuffer.Page = f_req_page;
searchBuffer.Pages = Math.ceil(eval(nodes[0].getAttribute('total')) / 6);
nodes = http_request.responseXML.getElementsByTagName('photo');
searchBuffer.BufferPhotos.length=0;
for (var k=0; k<nodes.length; k++)
{
searchBuffer.BufferPhotos.push(
new FlickrPhoto(nodes[k].getAttribute('id'),
nodes[k].getAttribute('owner'),
nodes[k].getAttribute('secret'),
nodes[k].getAttribute('server'),
nodes[k].getAttribute('farm'),
nodes[k].getAttribute('title'),
nodes[k].getAttribute('ispublic'),
nodes[k].getAttribute('isfriend'),
nodes[k].getAttribute('isfamily'),
searchBuffer.License
)
);
}
searchBuffer.GetPhotos(searchBuffer.License, searchBuffer.User, searchBuffer.Keyword, searchBuffer.Page);
restoreState();
return;
}
}
else return;
}
alert("Cannot execute request, please try again later.");
}
}
finally
{
http_request=null;
}
}
function restoreState()
{
if (f_license)
{
selectItemValue(document.flickr_search_form.flickr_license, searchBuffer.License);
if (f_license=="0") xGetElementById("flickr_owner_block").style.display = "inline";
document.flickr_search_form.keyword.value = searchBuffer.Keyword;
document.flickr_search_form.user.value = searchBuffer.User;
if (searchBuffer.Pages=="0")
{
xGetElementById("flickr_pagetxt").innerHTML = "No photo found";
}
else
{
xGetElementById("flickr_pagetxt").innerHTML = "Page " + searchBuffer.Page + "/" + searchBuffer.Pages;
}
xGetElementById("flickr_left").style.display = (eval(searchBuffer.Page)>1 ? "inline" : "none");
if (eval(searchBuffer.Page)>1)
{
//		 		xGetElementById("flickr_prev_link").href = "javascript:f.MakeRequest(null, null, null, document.flickr_search_form.flickr_license[document.flickr_search_form.flickr_license.selectedIndex].value, document.flickr_search_form.keyword.value, document.flickr_search_form.user.value," + (eval(searchBuffer.Page) - 1) + ");void(0);";
xGetElementById("flickr_prev_link").href = "javascript:f.MakeRequest(null, null, null, '" + searchBuffer.License + "', '" + searchBuffer.Keyword.replace("'", "\\'") + "', '" + searchBuffer.User.replace("'", "\\'") + "'," + (eval(searchBuffer.Page) - 1) + ");void(0);";
}
xGetElementById("flickr_right").style.display = (eval(searchBuffer.Page)<eval(searchBuffer.Pages) ? "inline" : "none");
if (eval(searchBuffer.Page)<eval(searchBuffer.Pages))
{
//		 		xGetElementById("flickr_next_link").href = "javascript:f.MakeRequest(null, null, null, document.flickr_search_form.flickr_license[document.flickr_search_form.flickr_license.selectedIndex].value, document.flickr_search_form.keyword.value, document.flickr_search_form.user.value," + (eval(searchBuffer.Page) + 1) + ");void(0);";
xGetElementById("flickr_next_link").href = "javascript:f.MakeRequest(null, null, null, '" + searchBuffer.License + "', '" + searchBuffer.Keyword.replace("'", "\\'") + "', '" + searchBuffer.User.replace("'", "\\'") + "'," + (eval(searchBuffer.Page) + 1) + ");void(0);";
}
for (var k=0; k<6; k++)
{
xGetElementById("flickr_img" + k).src = "/images/photo-none-small.jpg";
}
adjustWin();
var imgs = new Array();
var link;
imgHash.clear();
for (var k=0; k<6; k++)
{
if (k<searchBuffer.Photos.length)
{
xGetElementById("flickr_title" + k).innerHTML = searchBuffer.Photos[k].Title;
link = "http://farm" + searchBuffer.Photos[k].Farm + ".static.flickr.com/" + searchBuffer.Photos[k].Server + "/" + searchBuffer.Photos[k].Id + "_" + searchBuffer.Photos[k].Secret + "_m.jpg";
imgHash.put(link, xGetElementById("flickr_img" + k));
imgs.push(link);
xGetElementById("flickr_img" + k).alt = searchBuffer.Photos[k].Title;
xGetElementById("flickr_img_link" + k).href = "javascript:" + f_obj_name + ".LinkImage('" + searchBuffer.Photos[k].Id + "'," + k + "); void(0);";
//					xGetElementById("flickr_img_link" + k).href = "javascript:" + f_callback_name + "(" + f_caller_id + "," + k + "); void(0);";
xGetElementById("flickr_block" + k).style.display="inline";
//"http://www.flickr.com./" + encodeURIComponent(searchBuffer.Photos[k].Owner) + "/" + searchBuffer.Photos[k].Id;
}
else
{
xGetElementById("flickr_block" + k).style.display="none";
}
}
adjustWin();
new ImagePreloader(imgs, onpreloaded);
}
}
function onpreloaded(imgs)
{
if (imgs.bSuccess)
{
if (imgHash.get(imgs.src)!=null)
{
imgHash.get(imgs.src).src = imgs.src;
imgHash.remove(imgs.src);
}
else
{
return;
}
}
adjustWin();
}
function adjustWin()
{
if (xWidth(xGetElementById("tb001"))!=width || xHeight(xGetElementById("tb001"))!=height)
{
width = xWidth(xGetElementById("tb001"));
height = xHeight(xGetElementById("tb001"));
xlocationmenu.onOpen(width, height, triggerobj, displacement);
}
}
function initMenu()
{
xlocationmenu.onClose();
xGetElementById("locationmenu").innerHTML = "<table id='tb001'><tr><td><img src='/imgs/wait1.gif' width='16' height='16'></td></tr></table>";
adjustWin();
}
function getPhotoHTML()
{
var htmlarray = new Array();
htmlarray.push('<table style="border:2px solid #57B6DD; width:500px; background-color:transparent; " border="0" cellspacing="0" cellpadding="5">');
htmlarray.push('	<tr>');
for (var k=0; k<6; k++)
{
htmlarray.push('	<td height="100%" valign="bottom" width="33%" align="center">');
htmlarray.push('		<div id="flickr_block' + k + '" style="clear:both; width:95%; margin-bottom:5px; margin-top:8px; display:none;">');
htmlarray.push('		<table cellspacing="0" cellpadding="0" border="0" width="100%">');
htmlarray.push('		<tr>');
htmlarray.push('			<td valign="top" width="100%">');
htmlarray.push('			<div style="width:100%;">');
htmlarray.push('				<span class="round_bg_viewerfg" style="width:100%;">');
htmlarray.push('					<div class="browse_photo_other" align="center" style="width:100%; height:100%; padding-bottom:5px; border-top:none;">');
htmlarray.push('						<a id="flickr_img_link' + k + '" class="photo" href="#"><img border="0" id="flickr_img' + k + '" src="/images/photo-none-small.jpg" width="150"></a>');
htmlarray.push('					</div>');
htmlarray.push('					<b class="round_b">');
htmlarray.push('					<b class="round_b5"></b>');
htmlarray.push('					<b class="round_b4"></b>');
htmlarray.push('						<b class="round_b3"></b>');
htmlarray.push('						<b class="round_b2"><b></b></b>');
htmlarray.push('						<b class="round_b1"><b></b></b></b>');
htmlarray.push('				</span>');
htmlarray.push('				<div class="required_field" style="width:100%; padding-top:3px; padding-left:5px;">');
htmlarray.push('					<div class="overflow" style="width:150px; overflow:hidden;text-overflow:ellipsis;word-break:break-all;white-space:nowrap;"><a href="#"><b id="flickr_title' + k + '">title</b></a></div>');
htmlarray.push('				</div>');
htmlarray.push('			</div>');
htmlarray.push('			</td>');
htmlarray.push('		</tr>');
htmlarray.push('		</table>');
htmlarray.push('		</div>');
htmlarray.push('	</td>');
if (k==2) { htmlarray.push('</tr><tr>'); }
}
htmlarray.push('</tr>');
htmlarray.push('	<tr bgcolor="#f2f2f2">');
htmlarray.push('	<td align="left">');
htmlarray.push('		<div id="flickr_left" class="terms" style="padding-left:5px; margin:3px; display:none;">');
htmlarray.push('		<a id="flickr_prev_link" class="pagenumber" href="#"><b>&lt;&lt;</b></a>');
htmlarray.push('		</div>');
htmlarray.push('		</td>');
htmlarray.push('		<td align="center">');
htmlarray.push('		<div id="flickr_pagetxt" class="terms"></div>');
htmlarray.push('		</td>');
htmlarray.push('		<td align="right">');
htmlarray.push('		<div id="flickr_right" class="terms" style="padding-right:5px; margin:3px; display:none;">');
htmlarray.push('		<a id="flickr_next_link" class="pagenumber" href="#"><b>&gt;&gt;</b></a>');
htmlarray.push('		</div>');
htmlarray.push('		</td>');
htmlarray.push('	</tr>');
htmlarray.push('	<tr>');
htmlarray.push('	<td colspan="3">');
htmlarray.push('		<div class="terms" style="margin-top:10px;">');
htmlarray.push('		<form name="flickr_search_form">');
htmlarray.push('			<b>Search Flickr.com</b><br><div style="margin-bottom:8px;">License: ');
htmlarray.push('			<select name="flickr_license" onchange="return; xGetElementById(\'flickr_owner_block\').style.display = (this.selectedIndex==0 ? \'inline\' : \'none\'); f.AdjustWin();">');
htmlarray.push('				<option value="0">All rights reserved.</option>');
//		htmlarray.push('				<optgroup label="-- Creative Commons License --">');
//		htmlarray.push('				<option value="4">Attribution</option>');
//		htmlarray.push('				<option value="2">Attribution Non-commercial</option>');
//		htmlarray.push('				<option value="6">Attribution No Derivatives</option>');
//		htmlarray.push('				<option value="5">Attribution Share Alike</option>');
//		htmlarray.push('				<option value="1">Attribution Non-commercial Share Alike</option>');
//		htmlarray.push('				<option value="3">Attribution Non-commercial No Derivatives</option>');
htmlarray.push('				<option value="1,2,3,4,5,6" selected>Creative Commons License</option>');
htmlarray.push('				</optgroup>');
htmlarray.push('			</select></div>');
htmlarray.push('			<div style="margin-bottom:5px;"><span id="flickr_owner_block" style="display:inline;">Enter username / email: <input name="user" type="text"></span></div>');
htmlarray.push('			<div style="margin-bottom:8px;">and/or keyword: <input name="keyword" type="text"></div>');
htmlarray.push('			<input type="button" value="SEARCH" onclick="f.MakeRequest(null, null, null, document.flickr_search_form.flickr_license[document.flickr_search_form.flickr_license.selectedIndex].value, document.flickr_search_form.keyword.value, document.flickr_search_form.user.value);">');
htmlarray.push(' or <input type="button" value="Cancel" onclick="xlocationmenu.onClose();">');
htmlarray.push('		</form>');
htmlarray.push('		</div>');
htmlarray.push('	</td>');
htmlarray.push('	</tr>');
htmlarray.push('</table><div id="flickr_mask" class="menu"><table border="0" width="100%" height="100%"><tr><td align="center" valign="middle"><img src="/imgs/wait1.gif"></td></tr></div');
return htmlarray.join("");
}
}