AE.namespace('AE.run');
if(!AE.run.detailPOA) {

AE.run.detailPOA = function() {

	this.config = {
		target				:'',			// 元素对象的 ID
		actionUrl			:''				// 请求地址
	};
};

AE.run.detailPOA.prototype = {

	init: function(customConfig) {
		this.config = YL.merge(this.config, customConfig || {});
		var _self = this;
		var config = _self.config;

		var obj = get(config.target);

		_self.loadCss('http://style.aliexpress.com/css/4v/wholesale/buyer/poa.css');

		YAHOO.util.Connect.asyncRequest("GET", config.actionUrl, {
			success:function(data){_self.showProductList(data.responseText, obj, _self);},
			failure:function(data){},
			timeout:5000
		}, null);
	},

	showProductList: function(jsonStr, obj, _self) {
		var json = eval('(' + jsonStr + ')');
		var items = '';
		if(json && json.products && json.products.length > 0) {
			for(var i=0; i<json.products.length; i++) {
				items += _self.buildItem(json, i);
			}
			obj.innerHTML = _self.buildHeader(json) + items + _self.buildFooter(json);
		}
	},

	loadCss: function(file){
		var head = document.getElementsByTagName('head').item(0);
		css = document.createElement('link');
		css.href = file;
		css.rel = 'stylesheet';
		css.type = 'text/css';
		head.appendChild(css);
	},

	buildHeader: function(json) {
		var header = '<div id="poa">'
					+	'<div class="caption clearfix">'
					+		'<h2>Wholesale Products You Might be interested in</h2>'
					+		'<a class="view-more" href="' + json.keywordsUrl + '" target="_blank" rel="nofollow">Go to <img src="http://img.alibaba.com/images/cms/upload/wholesale/promotion/poa1022/aliexpress_logo.gif" alt="AliExpress" /></a>'
					+	'</div>'
					+	'<div class="content">'
					+		'<ul>';

		return header;
	},

	buildFooter: function(json) {
		var footer = 		'</ul><div style="clear: both;"></div>'
					+		'<div class="escrow">'
					+			'<a rel="nofollow" href="http://alipay.alibaba.com" target="_blank"><img border="0" align="absmiddle" src="http://img.alibaba.com/images/eng/escrow/icon/logo_s.gif" alt="Escrow"></a>Secure your payments online.'
					+		'</div>'
					+	'</div>'
					+'</div>';

		return footer;
	},

	buildItem: function(json, index) {
		var item = '<li>';
		if(index <= 0) {
			item = '<li class="first">';
		}

		item += 	'<div class="thumbnail">'
				+		'<a href="' + json.products[index].detailUrl + '" target="_blank" rel="nofollow"><img src="' + json.products[index].imgUrl + '" title="' + json.products[index].notHightLightSubject + '" /></a>'
				+	'</div>'

				+	'<div class="product-name">'
				+		'<a href="' + json.products[index].detailUrl + '" title="' + json.products[index].notHightLightSubject + '" target="_blank" rel="nofollow">' + json.products[index].subject + '</a>'
				+	'</div>'

				+	'<div class="price">'
				+		'<span class="currency">US $</span>'
				+		'<span class="value">' + json.products[index].priceRange + '</span>';

		if(json.products[index].unit && json.products[index].unit.length > 0) {
			item += '&nbsp;<span class="separator">/&nbsp;</span><span class="unit">' + json.products[index].unit + '</span>';
		}

		item += 	'</div>';

		if(json.products[index].minOrder) {
			item += '<div class="order">'
				+		'Min. Order: ' + json.products[index].minOrder
				+	'</div>';
		}

		item	 += '<div class="view-details">'
				+		'<a href="' + json.products[index].detailUrl + '" target="_blank" rel="nofollow">View details</a>'
				+	'</div>';

		if(json.products[index].freeShipping || json.products[index].freeShipping == 'true') {
			item += '<a class="freeshipping-label" href="' + json.products[index].detailUrl + '" target="_blank" rel="nofollow"></a>';
		}

		item += 	'</li>';

		return item;
	}
};
}

