// URI UTF8 Decoding object 
var Url = {
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		} 
		return string;
	}
}

function scan_images() {
	// hover to show flags
	$('div.image').unbind();
	$('div.image').hover(function() {
		$(this).addClass('imagehover');
		$('div.hdetails', this).css('visibility', 'visible');
	},
	function() {
		$(this).removeClass('imagehover');
		$('div.hdetails', this).css('visibility', 'hidden');
	});
}

function scan_video() {
	// hover to show flags
	$('div.video').unbind();
	$('div.video').hover(function() {
		$(this).addClass('videohover');
		$('div.hdetails', this).css('visibility', 'visible');
	},
	function() {
		$(this).removeClass('videohover');
		$('div.hdetails', this).css('visibility', 'hidden');
	});
}

function scan_entries () {
	// hover to show flags
	$('div.entry').unbind();
	$('div.entry').hover(function() {
		$(this).addClass('entryhover');
		$('span.flags', this).css('visibility', 'visible');
		$('div.hdetails', this).css('visibility', 'visible');
	},
	function() {
		$(this).removeClass('entryhover');
		$('span.flags', this).css('visibility', 'hidden');
		$('div.hdetails', this).css('visibility', 'hidden');
	});
	
	function show_images() {
		var entry = $(this).parent().parent('div.entry');
		if ($('div.images_ct', entry).length > 0) {
			$('div.images_ct', entry).slideUp('fast', function() {
				$('div.images_ct', entry).remove();
				var button = $('.images_btn', entry);
				$('img', button).attr('src', robby.baseurl+'/images/gray_arrow_down.gif');
				$(button).removeClass('hoverbutton_hover');
				$(button).hover(function() {
					$(this).addClass('hoverbutton_hover');
				},
				function() {
					$(this).removeClass('hoverbutton_hover');
				});
			});
			return;
		}
		var domain = $('a.title', entry).attr('href');
		domain = domain.replace(/^http\:\/\//, '');
		domain = domain.replace(/\?.+$/, '');
		var gparams = {
			rsz: 'large',
			v: '1.0',
			key: robby.gapikey,
			start: 0,
			q: 'site:'+domain,
			safe: 'off'
		};
		var button = $('.images_btn', entry);
		$('img', button).attr('src', robby.baseurl+'/images/loading.gif');
		$.getJSON('http://ajax.googleapis.com/ajax/services/search/images?callback=?', gparams, function(data) {
			if (data.responseData.results.length > 0) {
				var html = "<div class=images_ct><div align=right>"+robby.pgoogle+"</div>";
				var i=1;
				$.each(data.responseData.results, function() {
					if (i++ <= 5) 
						html += '<a class="lightbox" href="'+this.url+'"><img src="'+this.tbUrl+'" height=60 border=0 class=image></a> ';
				});
				html += '</div>';
				var images = $(html);
				images.css('display', 'none');
				// $(entry).append(images);
				if ($('.similar_ct', entry).length > 0) {
					$('.similar_ct', entry).before(images);
				} else {
					$(entry).append(images);
				}
				images.slideDown('fast', function() {
					$('img', button).attr('src', robby.baseurl+'/images/gray_arrow_up.gif');
					$('img', button).attr('width', '11');
					$('a.lightbox').lightBox();
					$(button).unbind();
					$(button).addClass('hoverbutton_hover');
					$(button).click(show_images);
				});
			} else {
				$('img', button).attr('src', robby.baseurl+'/images/gray_arrow_down.gif');
				$(button).fadeOut('slow');
			}
		});
	}

	function show_similar() {
		var entry = $(this).parent().parent('div.entry');
		var url = $('a.title', entry).attr('href');
		url = url.replace(/^http:\/\//, '');
		url = url.replace(/\/$/, '');
		var gparams = {
			rsz: 'large',
			v: '1.0',
			key: robby.gapikey,
			start: 0,
			q: 'related:'+url+' '+$('#searchstr').attr('value'),
			safe: 'off'
		};
		var button = $('.similar_btn', entry);
		$('img', button).attr('src', robby.baseurl+'/images/loading.gif');
		$.getJSON('http://ajax.googleapis.com/ajax/services/search/web?callback=?', gparams, function(data) {
			if (data.responseData.results.length > 0) {
				$(entry).append("<div class=similar_ct><div align=right>"+robby.pgoogle+"</div></div>");
				var similar = $('.similar_ct', entry);
				$(similar).css('display', 'none');
				$.each(data.responseData.results, function() {
					this.durl = this.unescapedUrl;
					this.durl = Url.decode(this.durl);
					this.durl = this.durl.replace(/^http\:\/\/(www\.)?/, '');
					this.durl = this.durl.replace(/\/$/, '');
					this.content = this.content.replace(/<.+?>/g, '');
					$(similar).append('<div class=related><a class=title href="'+this.unescapedUrl+'">'+this.titleNoFormatting+'</a><div>'+this.content+'</div><div class=details><span class=url>'+this.durl+'</span></div></div>');
				});
				$(similar).slideDown('fast');
				$('img', button).attr('src', robby.baseurl+'/images/gray_arrow_up.gif');
				$(button).addClass('hoverbutton_hover');
				$(button).unbind();
				$(button).click(function() {
					$(button).removeClass('hoverbutton_hover');
					$(similar).slideUp('fast', function() {
						$(similar).remove();
					});
					$(this).unbind();
					$('img', button).attr('src', robby.baseurl+'/images/gray_arrow_down.gif');
					$(this).removeClass('hoverbutton_hover');
					$(button).hover(function() {
						$(this).addClass('hoverbutton_hover');
					},
					function() {
						$(this).removeClass('hoverbutton_hover');
					});
					$(button).click(show_similar);
				});
			} else {
				$('img', button).attr('src', robby.baseurl+'/images/gray_arrow_down.gif');
				$(button).fadeOut('slow');
			}
		});
	}

	// add hidden details container. Hover to show it
	var similarCap = robby.lang == 'english' ? 'Similar' : 'Παρόμοιες';
	var imagesCap = robby.lang == 'english' ? 'Images' : 'Εικόνες';
	$('.hdetails', 'div.entry').remove();
	// check if it's a video
	
	$('div.entry').append('<div class=hdetails align=left><span class="hoverbutton similar_btn"><img src="'+robby.baseurl+'/images/gray_arrow_down.gif" border=0 width=11> '+similarCap+'</span><span class="hoverbutton images_btn"><img src="'+robby.baseurl+'/images/gray_arrow_down.gif" border=0 width=11> '+imagesCap+'</span></div>');
	$('span.hoverbutton').hover(function() {
		$(this).addClass('hoverbutton_hover');
	},
	function() {
		$(this).removeClass('hoverbutton_hover');
	});
	$('div.hdetails', 'div.entry').css('visibility', 'hidden');
	$('span.similar_btn').click(show_similar);
	$('span.images_btn').click(show_images);
}

