var searchTerms = '', order = 'relevance', time = 'all_time', page = 1;

window.addEvent('domready', function(){
	$('form').addEvent('submit', function(){
		searchTerms = $('text').get('value');
		page = 1;
		search(searchTerms, order, time, page);
		return false;
	});
});

function search(searchTerms, order, time, page) {
	new Request.JSONP({
		url: 'http://gdata.youtube.com/feeds/api/videos',
		data: {
			'vq': searchTerms,
			'racy': 'include',
			'max-results': 15,
			'start-index': (page - 1) * 15 + 1,
			'orderby': order,
			'time': time,
			'alt': 'json-in-script'
		},
		onComplete: function(data){
			$('entry').empty();
			$('entry').grab(new Element('p', {
				'html': 'results '
					+ data.feed.openSearch$startIndex.$t
					+ ' - '
					+ (data.feed.openSearch$startIndex.$t + data.feed.entry.length - 1)
					+ ' of about '
					+ data.feed.openSearch$totalResults.$t
					+ '&nbsp;&nbsp;'
					+ '<select id="order" onchange="order=this.value;page=1;search(searchTerms, order, time, page);">'
					+ '<option value="published">Newest</option>'
					+ '<option value="rating">Rating</option>'
					+ '<option value="viewCount">View Count</option>'
					+ '<option value="relevance" selected="selected">Relevance</option>'
					+ '</select>'
					+ '&nbsp;'
					+ '<select id="time" onchange="time=this.value;page=1;search(searchTerms, order, time, page);">'
					+ '<option value="today">Today</option>'
					+ '<option value="this_week">This week</option>'
					+ '<option value="this_month">This month</option>'
					+ '<option value="all_time" selected="selected">Anytime</option>'
					+ '</select>'
			}));
			$('order').set('value', order);
			$('time').set('value', time);
			data.feed.entry.each(function(entry) {
				$('entry').grab(new Element('div', {
					'class' : 'clear',
					'html'  : '<h2>'
							+ '<a href="javascript:(function(){Mediabox.open('
							+ '\'' + entry.link[0].href + '\', '
							+ '\'' + entry.title.$t.split("'").join("’").split('"').join('”') + '\', '
							+ '\'' + parseInt(document.documentElement.clientWidth * 0.75) + ' ' + parseInt(document.documentElement.clientHeight * 0.75) + '\''
							+ ');})()">'
								+ entry.title.$t
							+ '</a>'
						+ '</h2>'
						+ '<a href="javascript:(function(){Mediabox.open('
						+ '\'' + entry.link[0].href + '\', '
						+ '\'' + entry.title.$t.split("'").join("’").split('"').join('”') + '\', '
						+ '\'' + parseInt(document.documentElement.clientWidth * 0.75) + ' ' + parseInt(document.documentElement.clientHeight * 0.75) + '\''
						+ ');})()">'
							+ '<img src="' + entry.media$group.media$thumbnail[0].url + '" width="120" height="90" class="left" />'
						+ '</a>'
						+ '<p class="content">'
							+ entry.content.$t
						+ '</p>'
				}));
			});
			isPrevLink(data.feed.link) ?
				$('prev').setStyle('display', ''):
				$('prev').setStyle('display', 'none');
			isNextLink(data.feed.link) ?
				$('next').setStyle('display', ''):
				$('next').setStyle('display', 'none');
		}
	}).send();
}

function prev() {
	search(searchTerms, order, time, --page);
	new Fx.Scroll(window,{duration:500}).toTop();
}

function next() {
	search(searchTerms, order, time, ++page);
	new Fx.Scroll(window,{duration:500}).toTop();
}

function isPrevLink(link) {
	for (var i=0;i<link.length;i++)
		if (link[i].rel == 'previous')
			return true;
}

function isNextLink(link) {
	for (var i=0;i<link.length;i++)
		if (link[i].rel == 'next')
			return true;
}

