var xajaxRequestUri="ajax.php";
var xajaxDebug=0;
var xajaxStatusMessages=0;
var xajaxWaitCursor=false;
var xajaxDefinedGet=1;
var xajaxDefinedPost=0;

var intv_getFlickr = null;
var intv_getFlickr_try = 0;



function xajax_getFlickr(){
	clearInterval(intv_getFlickr);
	if (xajaxLoaded != null && xajaxLoaded == true){
		if ((elm = MM_findObj("flickrContainer")) != null){
			if (arguments[1] != null && arguments[1]["tpl"] != null && arguments[1]["tpl"] == 'leftcolumn'){
				elm.innerHTML = ''
					+	'<div class="sideBox180">'
					+		'<div class="sideBox180Head"><h3>IN PICTURES</h3></div>'
					+		'<div class="sideBox180BodyBottom2">'
					+			'<div style="text-align: center;"><img src="' + vars["url_tpl"] + 'img/indicator.gif" width="" height="" alt="" border="0" /></div>'
					+		'</div>'
					+	'</div>';
			}
			else{
				elm.innerHTML = '<div style="margin-top: 20px; text-align: center;"><img src="' + vars["url_tpl"] + 'img/indicator.gif" width="" height="" alt="" border="0" /></div>';
			}
		}
		return xajax.call("xajax_artsouthafrica_get_flickr", arguments, 1);
	}
	else if (intv_getFlickr_try < 10){
		intv_getFlickr_try++;
		intv_getFlickr = setInterval("xajax_getFlickr()", 1000, arguments);
	}
}



function getFlickrCallback(result, post, errors){
	// If success
	if (errors == null || errors.length == 0){
		// If result empty
		if (result == null || result["total"] == null || result["total"] == ""){
			errors = new Array(1);
		}
	}

	// Prevent from showing on home page if only 1 photo
	if (!(errors != null && errors.length > 0)){
		if (post['tpl'] == 'home' 
			&& (result["photos"].length == null || result["photos"].length <= 1)
		){
			errors = new Array(1);
		}
	}

	// Errors
	if (errors != null && errors.length > 0){
		if ((elm = MM_findObj("flickrContainer")) != null){
			switch (post['tpl']){
				case 'leftcolumn' :
					elm.innerHTML = ''
					+	'<div class="sideBox180">'
					+		'<div class="sideBox180Head"><h3>IN PICTURES</h3></div>'
					+		'<div class="sideBox180BodyBottom1">Error retrieving photos.</div>'
					+	'</div>';
					break;


				case 'home':
					elm.innerHTML = '';
					break;
			}
		}
	}
	else{
		if ((elm = MM_findObj("flickrContainer")) != null){
			post['tags'] = trim(post['tags']);
			post['tags_encoded'] = post['tags'].replace(new RegExp('\\s', 'g'), '');

			switch (post['tpl']){
				case 'leftcolumn' :
					var html = ''
					+	'<div class="sideBox180">'
					+		'<div class="sideBox180Head"><h3>IN PICTURES</h3></div>'
					+		'<div class="sideBox180BodyBottom1">'
					+			'<div id="flickrMainImg"></div>'
					+			'<div>'
					+				'<table cellpadding="0" cellspacing="0" border="0">';

					if (result["photos"][1] != null){
						html += ''
						+				'<tr>'
						+					'<td valign="top"><div style="margin-top: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][1] != null ? result["photos"][1] : '', post['group_id'], 'square', 78, 78) + '</div></td>'
						+					'<td valign="top"><div style="margin-top: 3px; margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][2] != null ? result["photos"][2] : '', post['group_id'], 'square', 78, 78) + '</div></td>'
						+				'</tr>';
					}

					if (result["photos"][3] != null){
						html += ''
						+				'<tr>'
						+					'<td valign="top"><div style="margin-top: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][3] != null ? result["photos"][3] : '', post['group_id'], 'square', 78, 78) + '</div></td>'
						+					'<td valign="top"><div style="margin-top: 3px; margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][4] != null ? result["photos"][4] : '', post['group_id'], 'square', 78, 78) + '</div></td>'
						+				'</tr>';
					}

					html += ''
					+					'<tr>'
					+						'<td colspan="2" align="right">'
					+							'<div class="resourceActions" style="margin-top: 5px;">'
					+								'<a class="bullet" href="http://www.flickr.com/groups/' + post['group_name'] + '/pool/tags/' + post['tags_encoded'] + '">View All</a>'// + (result['total'] > Number(post['perpage']) ? ' (' + post['perpage'] + ' / ' + result['total'] + ')' : '') // ' (' + (result['total'] - Number(post['perpage'])) + ' more)' : '')
					+							'</div>'
					+						'</td>'
					+					'</tr>'
					+				'</table>'
					+			'</div>'
					+		'</div>'
					+	'</div>';
//					+			'<div class="resourceSecondaryDetailGrey" style="margin-top: 10px; background: url(' + vars["url_tpl"] + 'img/icon-flickr.gif) 0 4px no-repeat; padding-left: 20px;">'
//					+				'<b>We welcome contributions.</b> Please tag with "<b>' + post['tags'] + '</b>"'  + (post['tags'].indexOf(" ") != -1 ? ' (single tag)' : '') + ' and send to <a href="http://www.flickr.com/groups/' + post['group_name'] + '/">our Flickr group</a>.'
//					+			'</div>';

					elm.innerHTML = html;

					setFlickrMain({
						'img':		getFlickrPhotoHTML(post['tpl'], result["photos"][0], post['group_id'], "medium", 160),
						'title':	escapeHTML(result["photos"][0]["title"]),
						'owner':	escapeHTML(result["photos"][0]["ownername"]),
						'date':		escapeHTML(result["photos"][0]["dateadded_lang"])
					});
					break;


				case 'home':
					elm.innerHTML = ''
					+	'<div style="margin-top: 20px; width: 358px; overflow: hidden;">'
					+		'<div style="padding: 7px; background: #DDD;">'
					+			'<div style="width: 344px; overflow: hidden;">'
					+					'<table cellpadding="0" cellspacing="0" border="0">'
					+						'<tr>'
					+							'<td valign="top"><div id="flickrMainImg"></div></td>'
					+							'<td valign="top">'
					+								'<table cellpadding="0" cellspacing="0" border="0">'
					+									'<tr>'
					+										'<td valign="top"><div style="margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][0] != null ? result["photos"][0] : '', post['group_id'], 'square', 49, 49) + '</div></td>'
					+										'<td valign="top"><div style="margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][1] != null ? result["photos"][1] : '', post['group_id'], 'square', 49, 49) + '</div></td>'
					+									'</tr>'
					+									'<tr>'
					+										'<td valign="top"><div style="margin-top: 3px; margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][2] != null ? result["photos"][2] : '', post['group_id'], 'square', 49, 49) + '</div></td>'
					+										'<td valign="top"><div style="margin-top: 3px; margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][3] != null ? result["photos"][3] : '', post['group_id'], 'square', 49, 49) + '</div></td>'
					+									'</tr>'
					+									'<tr>'
					+										'<td valign="top"><div style="margin-top: 3px; margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][4] != null ? result["photos"][4] : '', post['group_id'], 'square', 49, 49) + '</div></td>'
					+										'<td valign="top"><div style="margin-top: 3px; margin-left: 3px;">' + getFlickrPhotoHTML(post['tpl'], result["photos"][5] != null ? result["photos"][5] : '', post['group_id'], 'square', 49, 49) + '</div></td>'
					+									'</tr>'
					+								'</table>'
					+							'</td>'
					+						'</tr>'
					+						'<tr>'
					+							'<td valign="top">'
					+								'<h3 class="resourceTitle" style="margin-top: 5px;" id="flickrMainTitle"></h3>'
					+								'<div class="resourceSecondaryDetail">'
					+									'<span class="resourceDateAvailable" id="flickrMainOwner"></span><span class="resourceDateAvailable" id="flickrMainDate"></span>'
					+								'</div>'
					+							'</td>'
					+							'<td valign="bottom" align="right">'
					+								'<div class="resourceActions">'
					+									'<a class="bullet" href="http://www.flickr.com/groups/' + post['group_name'] + '/pool/tags/' + post['tags_encoded'] + '">View All</a>'// + (result['total'] > Number(post['perpage']) ? ' (' + post['perpage'] + ' / ' + result['total'] + ')' : '') // ' (' + (result['total'] - Number(post['perpage'])) + ' more)' : '')
					+								'</div>'
					+							'</td>'
					+						'</tr>'
					+					'</table>'
					+				'</div>'
					+			'</div>'
					+		'</div>';
//					+		'<div class="resourceSecondaryDetailGrey" style="margin-top: 10px; background: url(' + vars["url_tpl"] + 'img/icon-flickr.gif) 0 4px no-repeat; padding-left: 20px;">'
//					+			'<b>We welcome contributions.</b> Please tag with "<b>' + post['tags'] + '</b>"'  + (post['tags'].indexOf(" ") != -1 ? ' (single tag)' : '') + ' and send to <a href="http://www.flickr.com/groups/' + post['group_name'] + '/">our Flickr group</a>.'
//					+		'</div>';

					setFlickrMain({
						'img':		getFlickrPhotoHTML(post['tpl'], result["photos"][0], post['group_id'], "medium", 240),
						'title':	escapeHTML(result["photos"][0]["title"]),
						'owner':	escapeHTML(result["photos"][0]["ownername"]),
						'date':		escapeHTML(result["photos"][0]["dateadded_lang"])
					});
					break;
			}
		}
	}
}



function setFlickrMain(args){
	if ((elmImg = MM_findObj("flickrMainImg")) != null){
		elmImg.innerHTML = args["img"];
	}
	if ((elmTitle = MM_findObj("flickrMainTitle")) != null){
		elmTitle.innerHTML = args["title"];
	}
	if ((elmOwner = MM_findObj("flickrMainOwner")) != null){
		elmOwner.innerHTML = ''; //args["owner"] + " &ndash; ";
	}
	if ((elmDate = MM_findObj("flickrMainDate")) != null){
		elmDate.innerHTML = args["date"];
	}
	return false;
}



function getFlickrPhotoHTML(tpl, photo, group_id, size, width, height){
	if (photo != null && photo["sizes"] != null && photo["sizes"][size] != null){
		if (size == "square"){
			if (tpl == 'home'){
				return '<a onmouseover="setFlickrMain({'
				+	'\'img\': \'' + escapeHTML(getFlickrPhotoHTML(tpl, photo, group_id, "medium", 240)) + '\','
				+	'\'title\': \'' + escapeHTML(photo["title"]) + '\','
				+	'\'owner\': \'' + escapeHTML(photo["ownername"]) + '\','
				+	'\'date\': \'' + escapeHTML(photo["dateadded_lang"]) + '\''
				+	'}); return false;" href="http://www.flickr.com/photos/' + escapeHTML(photo["owner"]) + '/' + escapeHTML(photo["id"]) + '/in/pool-' + escapeHTML(group_id) + '"><img src="?media_thumb=' + encodeURIComponent(photo["sizes"][size]["source"]) + '&w=' + width + '&h=' + height + '&s=2&q=100" alt="' + escapeHTML(photo["title"]) + '" title="' + escapeHTML(photo["title"]) + '" width="' + width + '" height="' + height + '" border="0" /></a>';
			}
			else{
				return '<a href="http://www.flickr.com/photos/' + escapeHTML(photo["owner"]) + '/' + escapeHTML(photo["id"]) + '/in/pool-' + escapeHTML(group_id) + '"><img src="?media_thumb=' + encodeURIComponent(photo["sizes"][size]["source"]) + '&w=' + width + '&h=' + height + '&s=2&q=100" alt="' + escapeHTML(photo["title"]) + '" title="' + escapeHTML(photo["title"]) + '" width="' + width + '" height="' + height + '" border="0" /></a>';
			}
		}
		else{
			var ratio_w = Number(photo["sizes"][size]["width"]) / width;
			var height = Math.floor(Number(photo["sizes"][size]["height"]) / ratio_w);

			return '<a href="http://www.flickr.com/photos/' + escapeHTML(photo["owner"]) + '/' + escapeHTML(photo["id"]) + '/in/pool-' + escapeHTML(group_id) + '"><img src="?media_thumb=' + encodeURIComponent(photo["sizes"][size]["source"]) + '&w=' + width + '&q=100" alt="' + escapeHTML(photo["title"]) + '" title="' + escapeHTML(photo["title"]) + '" width="' + width + '" height="' + height + '" border="0" /></a>';
		}
/*
		if (width != null
			&& width != Number(photo["sizes"][size]["width"])
		){
			if (height != null){
				return '<a href="http://www.flickr.com/photos/' + escapeHTML(photo["owner"]) + '/' + escapeHTML(photo["id"]) + '/in/pool-' + escapeHTML(group_id) + '"><img src="?media_thumb=' + encodeURIComponent(photo["sizes"][size]["source"]) + '&w=' + width + '&h=' + height + '&s=2&q=100" alt="' + escapeHTML(photo["title"]) + '" title="' + escapeHTML(photo["title"]) + '" width="' + width + '" height="' + height + '" border="0" /></a>';
			}
			else{
				var ratio_w = Number(photo["sizes"][size]["width"]) / width;
				var height = Math.floor(Number(photo["sizes"][size]["height"]) / ratio_w);

				return '<a href="http://www.flickr.com/photos/' + escapeHTML(photo["owner"]) + '/' + escapeHTML(photo["id"]) + '/in/pool-' + escapeHTML(group_id) + '"><img src="?media_thumb=' + encodeURIComponent(photo["sizes"][size]["source"]) + '&w=' + width + '&q=100" alt="' + escapeHTML(photo["title"]) + '" title="' + escapeHTML(photo["title"]) + '" width="' + width + '" height="' + height + '" border="0" /></a>';
			}
		}
		else{
			return '<a href="http://www.flickr.com/photos/' + escapeHTML(photo["owner"]) + '/' + escapeHTML(photo["id"]) + '/in/pool-' + escapeHTML(group_id) + '"><img src="' + escapeHTML(photo["sizes"][size]["source"]) + '" alt="' + escapeHTML(photo["title"]) + '" title="' + escapeHTML(photo["title"]) + '" width="' + escapeHTML(photo["sizes"][size]["width"]) + '" height="' + escapeHTML(photo["sizes"][size]["height"]) + '" border="0" /></a>';
		}
*/
	}
	else{
		return "";
	}
}



xajax_getFlickr("getFlickrCallback", xajax_getFlickr_args);