var SearchDetail = {

	openDetail: function(content, detail, api, options) {		
		var method = 'open' + api + "Detail";
		if (method in this) {
/* 			content.slideUp();
			detail.height(window.innerHeight);
			$('iframe', detail).css('height', '80%');
			detail.slideDown();
			
			var self = this;
			$('#detail-header > a#backButton').click(function() {
				self.closeDetail(content, detail);
			}); */
			
			this[method].apply(this, [content, detail, options]);
		} else {
//			Application.console.log("Detail method not supported: " + method);
		}
	},
	
	closeDetail: function(content, detail) {
		detail.slideUp();
		content.slideDown();
		
		$('iframe', detail).attr('src', "about:blank");
	},

	setTitle: function(detail, title) {
		$('#detail-header > h1').text(title);
	},
	
	setOpenInTab: function(detail, link) {
		var aLink = $('#detail-header > a#openInTab', detail);
		aLink.attr('href', link);
	},
	
	onDetailLoad: function(frame) {
		var safeChildWindow = frame.contentWindow;
		var unsafeChildWindow = safeChildWindow.wrappedJSObject ? safeChildWindow.wrappedJSObject : safeChildWindow;
		var href = unsafeChildWindow.location.href;
		if (href.indexOf('.wikipedia.org') != -1) {
			var childDoc = unsafeChildWindow.document;
			$('#header', childDoc).hide();
			$('#nav', childDoc).hide();
			$('#language_notice', childDoc).hide();
			
			var langCode = href.substring('http://'.length, href.indexOf('.'));
			unsafeChildWindow.setTimeout(function() {
				$('body', childDoc).find('a').each(function(index) {
					if ($(this).attr('href')) {
						var aHref = $(this).attr('href');
						
						if (aHref.charAt(0) !== '#') {
							$(this).attr('target', '_blank');
						}

						if (aHref.charAt(0) === '/') {
							$(this).attr('href', 'http://' + langCode + ".wikipedia.org" + aHref);
						}
					}
				});				
			}, 0)
		}
	},
	
	openWikipediaDetail: function(content, detail, options) {
		var langCode = '';
		if (!('langCode' in options)) {
			langCode = 'zh';
		} else {
			langCode = options.langCode;
		}
		
		if (!('title' in options) || !options.title) {
			return;
		}
		
		this.setTitle(detail, "Wikipedia: " + options.title);
		var wikiLink = "http://" + langCode + ".m.wikipedia.org/wiki/" + options.title.replace(/ /g, "_");
		var tabLink = "http://" + langCode + ".wikipedia.org/wiki/" + options.title.replace(/ /g, "_");
		window.open(tabLink, '_blank');
//		$('iframe', detail).attr('src', tabLink);
//		this.setOpenInTab(detail, tabLink);
	},	
};
