var flickrshows = [],
flickrshow = function (t, s) {
	var _ = this;
	_.animate = function (e, a, j, s, x) {
		if ('undefined' !== typeof _.constant.intervals[x]) clearInterval(_.constant.intervals[x]);
		_.constant.intervals[x] = setInterval(function () {
			var i = Math.round(e.style[a].replace(/([a-zA-Z]{2})$/, ''));
			var k = Math.round(j - i);
			if ((Math.abs(k)) > 1) {
				e.style[a] = Math.floor(i + (k / 2)) + 'px'
			} else {
				e.style[a] = j + 'px';
				clearInterval(_.constant.intervals[x])
			}
		},
		s / 1.5)
	};
	_.addEvent = function (o, t, f) {
		if (o.attachEvent) {
			o['e' + t + f] = f;
			o[t + f] = function () {
				o['e' + t + f](window.event, o)
			};
			o.attachEvent('on' + t, o[t + f])
		} else {
			o.addEventListener(t, f, false)
		}
	};
	_.hideButtons = function () {
		if ((_.constant.isLoading === true) || (_.constant.isButtonsOpen === false)) {
			return
		}
		_.constant.isButtonsOpen = false;
		_.animate(_.elements.b, 'top', _.elements.t.offsetHeight, _.settings.speed, 'b')
	};
	_.showButtons = function () {
		if ((_.constant.isLoading === true) || (_.constant.isButtonsOpen === true)) {
			return
		}
		_.constant.isButtonsOpen = true;
		_.animate(_.elements.b, 'top', _.elements.t.offsetHeight - 40, _.settings.speed, 'b')
	};
	_.updateImageDetails = function () {
		var i = _.elements.i.childNodes[_.constant.imageCurrent].childNodes[0];
		if ('undefined' === typeof i) return;
		_.elements.b.childNodes[3].innerHTML = (_.constant.imageCurrent + 1) + '/' + _.constant.imageTotal + ' - ' + i.getAttribute('flickrshow-t')
	};
	_.updateImageDimensions = function () {
		var a, im, cw, ch, nh, nw;
		a = _.elements.i.getElementsByTagName('LI');
		for (var i in a) {
			if ('object' !== typeof a[i]) continue;
			im = a[i].getElementsByTagName('IMG');
			for (var j in im) {
				if ('object' !== typeof im[j]) continue;
				cw = im[j].offsetWidth;
				ch = im[j].offsetHeight;
				if (cw > ch) {
					nw = _.elements.t.offsetWidth + (_.elements.t.offsetHeight / 100);
					nh = Math.ceil((nw / cw) * ch)
				} else {
					nh = _.elements.t.offsetHeight + (_.elements.t.offsetHeight / 100);
					nw = Math.ceil((nh / ch) * cw)
				}
				try {
					im[j].style.height = nh + 'px';
					im[j].style.left = Math.round((_.elements.t.offsetWidth - nw) / 2) + 'px';
					im[j].style.position = 'absolute';
					im[j].style.top = Math.round((_.elements.t.offsetHeight - nh) / 2) + 'px';
					im[j].style.width = nw + 'px'
				} catch(e) {}
			}
		}
	};
	_.left = function () {
		if (_.constant.isLoading === true) return;
		_.constant.imageCurrent = ((_.constant.imageCurrent - 1) < 0) ? _.constant.imageTotal - 1 : _.constant.imageCurrent - 1;
		_.animate(_.elements.i, 'left', '-' + (_.constant.imageCurrent * _.elements.t.offsetWidth), _.settings.speed, 'i');
		_.updateImageDetails()
	};
	_.right = function () {
		if (_.constant.isLoading === true) return;
		_.constant.imageCurrent = ((_.constant.imageCurrent + 2) > _.constant.imageTotal) ? 0 : _.constant.imageCurrent + 1;
		_.animate(_.elements.i, 'left', '-' + (_.constant.imageCurrent * _.elements.t.offsetWidth), _.settings.speed, 'i');
		_.updateImageDetails()
	};
	_.play = function () {
		if (_.constant.isPlaying === false) {
			_.constant.isPlaying = true;
			_.elements.b.childNodes[2].style.backgroundImage = 'url(' + _.constant.uri + 'img/' + _.settings.theme + '/is.png)';
			_.constant.intervals['p'] = setInterval(function () {
				_.right()
			},
			_.settings.interval)
		} else {
			_.constant.isPlaying = false;
			_.elements.b.childNodes[2].style.backgroundImage = 'url(' + _.constant.uri + 'img/' + _.settings.theme + '/ip.png)';
			clearInterval(_.constant.intervals['p'])
		}
	};
	_.jsonLoad = function () {
		if ('object' !== typeof flickrshows[_.constant.unique]) return;
		clearInterval(_.constant.intervals['json']);
		if ('undefined' !== typeof flickrshows[_.constant.unique]['error']) {
			_.elements.w.innerHTML = '<p style="color:#A00;font:normal normal 400 12px/18px helvetica,arial,sans-serif;left:0;margin:0;padding:0;posiiton:absolute;text-align:center;top:0;width:100%">' + flickrshows[_.constant.unique]['error'] + '</p>'
		} else {
			_.constant.imageTotal = flickrshows[_.constant.unique].length;
			for (var i in flickrshows[_.constant.unique]) {
				var b = document.createElement('img');
				b.setAttribute('flickrshow-t', flickrshows[_.constant.unique][i].title);
				b.setAttribute('flickrshow-u', flickrshows[_.constant.unique][i].url);
				b.setAttribute('rel', i);
				b.style.cursor = 'pointer';
				b.style.display = 'block';
				b.style.margin = '0';
				b.style.padding = '0';
				if ((_.elements.t.offsetHeight > 500) || (_.elements.t.offsetWidth > 500)) {
					b.src = flickrshows[_.constant.unique][i].src_l + '?' + _.constant.unique
				} else if ((_.elements.t.offsetHeight < 240) || (_.elements.t.offsetWidth < 240)) {
					b.src = flickrshows[_.constant.unique][i].src_s + '?' + _.constant.unique
				} else {
					b.src = flickrshows[_.constant.unique][i].src_m + '?' + _.constant.unique
				}
				var c = document.createElement('li');
				c.style.left = (i * _.elements.t.offsetWidth) + 'px';
				c.style.height = _.elements.t.offsetHeight + 'px';
				c.style.margin = '0';
				c.style.overflow = 'hidden';
				c.style.padding = '0';
				c.style.position = 'absolute';
				c.style.top = '0';
				c.style.width = _.elements.t.offsetWidth + 'px';
				c.appendChild(b);
				_.elements.i.appendChild(c);
//				_.addEvent(b, 'click', function (e, i) {
//					document.location.href = (('undefined' !== typeof i) ? i: //e.target).getAttribute('flickrshow-u')
//				});
				_.addEvent(b, 'load', function (e, i) {
					_.constant.imageLoaded++;
					var a = Math.round((_.constant.imageLoaded / _.constant.imageTotal) * 240);
					_.animate(_.elements.l.childNodes[0], 'width', ((a <= 36) ? 36 : a), 'i');
					if (_.constant.imageLoaded === _.constant.imageTotal) _.showLoad()
				})
			}
		}
	};
	_.showLoad = function () {
		_.updateImageDimensions();
		_.updateImageDetails();
		_.elements.w.removeChild(_.elements.l);
		_.elements.i.style.visibility = 'visible';
		_.constant.isLoading = false;
		if (_.settings.autoplay === true) _.play()
	};
	_.constant = {
		intervals: [],
		imageCurrent: 0,
		imageLoaded: 0,
		imageTotal: 0,
		isButtonsOpen: false,
		isLoading: true,
		isPlaying: false,
		unique: Math.round(Math.random() * 1000000000000),
		uri: 'http://v7.flickrshow.com/'
	};
	_.elements = {
		b: null,
		i: null,
		l: null,
		t: null,
		w: null
	};
	_.settings = {
		autoplay: false,
		group: '',
		hide_buttons: false,
		interval: 3000,
		page: '1',
		per_page: '50',
		set: '',
		speed: 100,
		tags: '',
		theme: 'black',
		user: ''
	};
	_.elements.t = document.getElementById(t);
	for (var i in _.settings) {
		if ('undefined' !== typeof s[i]) _.settings[i] = s[i]
	}
	if ('undefined' !== typeof s.flickr_group) _.settings.group = s.flickr_group;
	if ('undefined' !== typeof s.flickr_photoset) _.settings.set = s.flickr_photoset;
	if ('undefined' !== typeof s.flickr_tags) _.settings.tags = s.flickr_tags;
	if ('undefined' !== typeof s.flickr_user) _.settings.user = s.flickr_user;
	var d, ts;
	switch (_.settings.theme) {
	case 'white':
		ca = '#FFF';
		cb = '#EEE';
		cc = '#DDD';
		cd = '#111';
		break;
	default:
		_.settings.theme = 'black';
		ca = '#000';
		cb = '#111';
		cc = '#222';
		cd = '#FFF';
		break
	}
	_.elements.t.innerHTML = '<div id="' + t + '-fs-w" style="background:transparent;height:' + _.elements.t.offsetHeight + 'px;margin:0;overflow:hidden;padding:0;position:relative;width:' + _.elements.t.offsetWidth + 'px"><div id="' + t + '-fs-l" style="background:' + cb + ';border-radius:12px;height:24px;left:50%;margin:-12px 0 0 -120px;overflow:hidden;padding:0;position:absolute;top:50%;width:240px;-moz-border-radius:12px;-webkit-border-radius:12px"><div id="' + t + '-fs-l-b" style="background:' + cc + ';border-radius:12px;height:24px;left:0;margin:0;padding:0;position:absolute;top:0;width:0;-moz-border-radius:12px;-webkit-border-radius:12px"></div></div><ul id="' + t + '-fs-i" style="background:transparent;height:' + _.elements.t.offsetHeight + 'px;left:0;list-style:none;margin:0;padding:0;position:absolute;top:0;visibility:hidden;width:' + _.elements.t.offsetWidth + 'px"></ul><div id="' + t + '-fs-b" style="background:transparent url(' + _.constant.uri + 'img/' + _.settings.theme + '/b2.png);height:40px;margin:0;padding:0;position:absolute;top:' + _.elements.t.offsetHeight + 'px;width:' + _.elements.t.offsetWidth + 'px"><div id="' + t + '-fs-b-l" style="background:' + ca + ' url(' + _.constant.uri + 'img/' + _.settings.theme + '/il.png) 50% 50% no-repeat;border-radius:12px;cursor:pointer;height:24px;left:auto;margin:0;padding:0;position:absolute;right:40px;top:8px;width:24px;-moz-border-radius:12px;-webkit-border-radius:12px"></div><div id="' + t + '-fs-b-r" style="background:' + ca + ' url(' + _.constant.uri + 'img/' + _.settings.theme + '/ir.png) 50% 50% no-repeat;border-radius:12px;cursor:pointer;height:24px;left:auto;margin:0;padding:0;position:absolute;right:8px;top:8px;width:24px;-moz-border-radius:12px;-webkit-border-radius:12px"></div><div id="' + t + '-fs-b-p" style="background:' + ca + ' url(' + _.constant.uri + 'img/' + _.settings.theme + '/ip.png) 50% 50% no-repeat;border-radius:12px;cursor:pointer;height:24px;left:8px;margin:0;padding:0;position:absolute;right:auto;top:8px;width:24px;-moz-border-radius:12px;-webkit-border-radius:12px"></div><p id="' + t + '-fs-b-d" style="background:transparent url(' + _.constant.uri + 'img/' + _.settings.theme + '/b0.png);border-radius:12px;color:' + cd + ';font:normal normal 600 11px/24px helvetica,arial,sans-serif;height:24px;left:40px;margin:0;overflow:hidden;padding:0 0;position:absolute;right:auto;text-align:center;text-shadow:none;text-transform:capitalize;top:8px;width:' + (_.elements.t.offsetWidth - 112) + 'px;-moz-border-radius:12px;-webkit-border-radius:12px">Title Goes Here</p></div></div>';
	_.elements.w = _.elements.t.childNodes[0];
	_.elements.l = _.elements.t.childNodes[0].childNodes[0];
	_.elements.i = _.elements.t.childNodes[0].childNodes[1];
	_.elements.b = _.elements.t.childNodes[0].childNodes[2];
	if (false === _.settings.hide_buttons) {
		_.addEvent(_.elements.t, 'mouseover', function (e) {
			_.showButtons()
		});
		_.addEvent(_.elements.t, 'mouseout', function (e) {
			_.hideButtons()
		});
		_.addEvent(_.elements.b.childNodes[0], 'click', function (e) {
			_.left()
		});
		_.addEvent(_.elements.b.childNodes[1], 'click', function (e) {
			_.right()
		});
		_.addEvent(_.elements.b.childNodes[2], 'click', function (e) {
			_.play()
		})
	}
	var j = document.createElement('script');
	j.type = 'text/javascript';
	j.src = _.constant.uri + 'flickrshow.json?group=' + _.settings.group + '&page=' + _.settings.page + '&per_page=' + _.settings.per_page + '&set=' + _.settings.set + '&tags=' + _.settings.tags + '&user=' + _.settings.user + '&unique=' + _.constant.unique;
	(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(j);
	_.constant.intervals['json'] = setInterval(function () {
		_.jsonLoad()
	},
	100);
	return {}
};
