/**
 * Overture wrapper class
 * 
 * @author ikeda@mag2.com
 * @version 0.1
 * 2006/10/10
 * @version 0.2 2009/07/24
 * @version 0.2.1 2009/07/27
 */

/* SYNOPSIS:
 * HTML:
 *   var o = new Mag2Overture();
 *   o.setRenderer("render.js");
 *   o.setRendererCharset("Shift_JIS");
 *   o.setContextKeywords("nihongo");
 *   o.setMaxCount(10);
 *   o.render();
 * 
 * render.js:
 * {
 *   var dataList = beanizeOverture(zSr);
 *   
 *   document.write('<dl class="overture">');
 *   for(var i = 0; i < dataList.length; ++i){
 *     var data = dataList[i];
 *     document.writeln('<dt><a href="' + escapeHtml(data.getClickUrl()) + '">'
 *       + escapeHtml(data.getTitle())
 *       + '</a></dt>');
 *     document.writeln('<dd><a href="' + escapeHtml(data.getClickUrl()) + '">'
 *       + escapeHtml(data.getDescription())
 *       + '</a></dd>');
 *     document.writeln('<dd><a href="' + escapeHtml(data.getClickUrl()) + '">'
 *       + escapeHtml(data.getSiteHost())
 *       + '</a></dd>');
 *   }
 *   document.write('</dl>');
 * }
 */

/**
 * HTMLescape
 * 
 * @param str
 * @return escaped string
 */

function escapeHtml(str){
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "&#39;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	return str;
}

/**
 * URL escape
 *     multi byte letter is use UTF-8
 * URL escape: RFC 3986
 *     escape all the letters except unreserved.
 * UTF-8: RFC3629
 * 
 * @param str
 * @return escaped string
 */
 
function escapeUri(str){
	if(str.match(/^[\x00-\x7f]*$/)){
		// ascii only
		return escape(str);
	}
	
	var encoded = "";
	for(var i = 0; i < str.length; ++i){
		var code = str.charCodeAt(i);	// suppose that UNICODE returns
		if(code == 0x20){	//space(' ')
			encoded += "+";
		}else if((0x61 <= code && code <= 0x7a) //ALPHA-INCAP("a"-"z")
				|| (0x41 <= code && code <= 0x5a) //ALPHA-CAP("A"-"Z")
				|| (0x30 <= code && code <= 0x39) //DIGIT("0".."9")
				|| code == 0x2d //"-"
				|| code == 0x2e //"."
				|| code == 0x5f //"_"
				|| code == 0x7e //"~"
		){
			encoded += String.fromCharCode(code);
		}else if(0x00 <= code && code <= 0x7f){
		//1byte
			var codeStr = "00" + (code & 0xff).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
		}else if(0x0080 <= code && code <= 0x07ff){
		//2bytes
		//00000xxx xxyyyyyy -> 110xxxxx 10yyyyyy
			var codeStr = "00" + (((code >> 6) & 0x1f) | 0xc0).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
			codeStr = "00" + ((code & 0x3f) | 0x80).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
		}else if(0x0800 <= code && code <= 0xffff){
		//3bytes
		//xxxxyyyy yyzzzzzz -> 1110xxxx 10yyyyyy 10zzzzz
			var codeStr = "00" + (((code >> 12) & 0x0f) | 0xe0).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
			codeStr = "00" + (((code >> 6) & 0x3f) | 0x80).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
			codeStr = "00" + ((code & 0x3f) | 0x80).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
		}else if(0x10000 <= code && code <= 0x1fffff){
		//4 bytes
		//00000000 000wwwxx xxxxyyyy yyzzzzzz -> 11110www 10xxxxxx 10yyyyyy 10zzzzz
			var codeStr = "00" + (((code >> 18) & 0x07) | 0xf0).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
			codeStr = "00" + (((code >> 12) & 0x03f) | 0xe0).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
			codeStr = "00" + (((code >> 6) & 0x3f) | 0x80).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
			codeStr = "00" + ((code & 0x3f) | 0x80).toString(16);
			encoded += '%' + codeStr.substr(codeStr.length - 2);
		}
	}
	return encoded;
}




/**
 * Mag2Overture class
 */

function Mag2Overture(){
	this.init();
}

/**
 * @param src
 * @param charset
 */
Mag2Overture.prototype.include = function(src, charset){
	if(charset != void(0)){
		document.writeln('<script type="text/javascript" src="' + escapeHtml(src) + '" charset="' + charset + '"></script>');
	}else{
		document.writeln('<script type="text/javascript" src="' + escapeHtml(src) + '"></script>');
	}
	return;
}

/**
 * initialize Mag2Overture
 */
Mag2Overture.prototype.init = function(){
	this.setRenderer(void(0));
	this.setRendererCharset(void(0));
	this.setFeedUri('http://im.ov.yahoo.co.jp/js_flat/');
	this.setPartner("magmag_jp_cat_ctxt1");
	this.setContextId(void(0));
	this.setContextUrl(location.href);
	this.setContextCategory(void(0));
	this.setContextKeywords(void(0));
	this.setMarket('jp');
	this.setType(void(0));
	this.setMaxCount(void(0));
	this.setCategoryMaps(void(0));
	this.setUrlFilters(void(0));
	
	return;
}

Mag2Overture.prototype.render = function(){
	this.include(this.getRequestUri(), "utf-8");
	if(this.getRendererCharset())
		this.include(this.getRenderer(), this.getRendererCharset());
	else
		this.include(this.getRenderer());
	
	
	return;
}

Mag2Overture.prototype.getDataList = function(){
	this.include(this.getRequestUri(), "utf-8");
}

Mag2Overture.prototype.encodeURI = function(uri){
	return encodeURIComponent(uri).replace(/\./g, '%2E');
}

Mag2Overture.prototype.getRequestUri = function(){
	var uri = this.getFeedUri();
	uri += '?source=' + escapeUri(this.getPartner());
	
	if(this.getContextId() != void(0))
		uri += '&ctxtId=' + escapeUri(this.getContextId());
	if(this.getType() != void(0))
		uri += '&type=' + escapeUri(this.getType());
	if(this.getMaxCount() != void(0))
		uri += '&maxCount=' + this.getMaxCount();
	if(this.getContextUrl() != void(0))
		uri += '&ctxtUrl=' + this.encodeURI(this.getContextUrl());

	uri += '&outputCharEnc=utf8';

	return uri;
}



Mag2Overture.prototype.setRenderer = function(value){
	this.renderer = value;
	return;
}

Mag2Overture.prototype.getRenderer = function(){
	return this.renderer;
}


Mag2Overture.prototype.setRendererCharset = function(value){
	this.rendererCharset = value;
	return;
}

Mag2Overture.prototype.getRendererCharset = function(){
	return this.rendererCharset;
}


Mag2Overture.prototype.setFeedUri = function(value){
	this.feedUri = value;
	return;
}

Mag2Overture.prototype.getFeedUri = function(){
	return this.feedUri;
}

Mag2Overture.prototype.setPartner = function(value){
	this.partner = value;
	return;
}

Mag2Overture.prototype.getPartner = function(){
	return this.partner;
}

Mag2Overture.prototype.setContextId = function(value){
	this.contextId = value;
	return;
}

Mag2Overture.prototype.setCtxtId = Mag2Overture.prototype.setContextId;

Mag2Overture.prototype.getContextId = function(){
	return this.contextId;
}

Mag2Overture.prototype.getCtxtId = Mag2Overture.prototype.getContextId;


Mag2Overture.prototype.setContextUrl = function(value){
	this.contextUrl = value;
	return;
}

Mag2Overture.prototype.setCtxtUrl = Mag2Overture.prototype.setContextUrl;

Mag2Overture.prototype.getContextUrl = function(){
	return this.contextUrl;
}

Mag2Overture.prototype.getCtxtUrl = Mag2Overture.prototype.getContextUrl;


Mag2Overture.prototype.setContextCategory = function(value){
	this.contextCategory = value;
	return;
}

Mag2Overture.prototype.setCtxtCat = Mag2Overture.prototype.setContextCategory;

Mag2Overture.prototype.getContextCategory = function(){
	return this.contextCategory;
}

Mag2Overture.prototype.getCtxtCat = Mag2Overture.prototype.getContextCategory;


Mag2Overture.prototype.setContextKeywords = function(value){
	this.contextKeywords = value;
	return;
}

Mag2Overture.prototype.setCtxtKeywords = Mag2Overture.prototype.setContextKeywords;

Mag2Overture.prototype.setKeywords = Mag2Overture.prototype.setContextKeywords;

Mag2Overture.prototype.getContextKeywords = function(){
	return this.contextKeywords;
}

Mag2Overture.prototype.getCtxtKeywords = Mag2Overture.prototype.getContextKeywords;

Mag2Overture.prototype.getKeywords = Mag2Overture.prototype.getContextKeywords;


Mag2Overture.prototype.setMarket = function(value){
	this.market = value;
	return;
}

Mag2Overture.prototype.setMkt = Mag2Overture.prototype.setMarket;

Mag2Overture.prototype.getMarket = function(){
	return this.market;
}

Mag2Overture.prototype.getMkt = Mag2Overture.prototype.getMarket;


Mag2Overture.prototype.setType = function(value){
	this.type = value;
	return;
}

Mag2Overture.prototype.getType = function(){
	return this.type;
}

Mag2Overture.prototype.setMaxCount = function(value){
	this.maxCount = value;
	return;
}

Mag2Overture.prototype.getMaxCount = function(){
	return this.maxCount;
}


/**
 * undocumented, maybe have to be extended later.
 * in that case, you have to keep compatibilities.
 * @param value 
 */
Mag2Overture.prototype.setCategoryMaps = function(value){
	this.categoryMaps = value;
	return;
}

/**
 * undocumented, maybe have to be extended later.
 * in that case, you have to keep compatibilities.
 * @param value 
 */
Mag2Overture.prototype.setCatMaps = Mag2Overture.prototype.setCategoryMaps;

/**
 * undocumented, maybe have to be extended later.
 * in that case, you have to keep compatibilities.
 * @return 
 */
Mag2Overture.prototype.getCategoryMaps = function(){
	return this.categoryMaps;
}

/**
 * undocumented, maybe have to be extended later.
 * in that case, you have to keep compatibilities.
 * @return 
 */
Mag2Overture.prototype.getCatMaps = Mag2Overture.prototype.getCategoryMaps;


/**
 * undocumented, maybe have to be extended later.
 * in that case, you have to keep compatibilities.
 * @param value 
 */
Mag2Overture.prototype.setUrlFilters = function(value){
	this.urlFilters = value;
	return;
}

/**
 * undocumented, maybe have to be extended later.
 * in that case, you have to keep compatibilities.
 * @return 
 */
Mag2Overture.prototype.getUrlFilters = function(){
	return this.urlFilters;
}


function OvertureBean(){
	this.init();
}

OvertureBean.prototype.init = function(){
	this.setDescription(null);
	this.setClickUrl(null);
	this.setTitle(null);
	this.setSiteHost(null);
	return;
}

OvertureBean.prototype.setDescription = function(value){
	this.description = value;
	return;
}

OvertureBean.prototype.getDescription = function(){
	return this.description;
}

OvertureBean.prototype.setClickUrl = function(value){
	this.clickUrl = value;
	return;
}

OvertureBean.prototype.getClickUrl = function(){
	return this.clickUrl;
}

OvertureBean.prototype.setTitle = function(value){
	this.title = value;
	return;
}

OvertureBean.prototype.getTitle = function(){
	return this.title;
}

OvertureBean.prototype.setSiteHost = function(value){
	this.siteHost = value;
	return;
}

OvertureBean.prototype.getSiteHost = function(){
	return this.siteHost;
}

/**
 * @param zSr
 * @return Array
 */
function beanizeOverture(zSr){
	var reservedSize = 6;
	var unitSize = 6;
	var dataList = new Array();
	
	for(var i = reservedSize; i < zSr.length; i += unitSize){
		var bean = new OvertureBean();
		bean.setDescription(zSr[i + 0]);
		bean.setClickUrl(zSr[i + 2]);
		bean.setTitle(zSr[i + 3]);
		bean.setSiteHost(zSr[i + 4]);
		
		dataList[dataList.length] = bean;	//push is not support IE5/MacOS9
	}
	return dataList;
}
