//<![CDATA[

/*
	name: PriceGrabber
	author : gregory tomlinson
	(c) copyright AOL LLC 2009
	////////////////////////////////
	////////////////////////////////
	dependencies : mootools 1.2, Asset, JsonP [http://o.aolcdn.com/art/_media/sp/jsonp.js] 
	////////////////////////////////
	////////////////////////////////			
*/

var PriceGrabber = new Class({

	Implements: [Options, Events],
		
	options : {
		//url : 'http://toybox.office.aol.com/targeting-web/shoppingDynamicModule.jsp',
		url : 'http://api.shopping.aol.com/targeting-web/shoppingDynamicModule.jsp',
		params : {
			f:'json',
			///c : null,
			isize : 'medium', // possible values - small | medium | large | xlarge
			links : 'offer',
			num:4,
			s : '',
			site : '',
			r : ''			
		},
		ui : {
			containerId : 'priceGrabberWrapper',
			gutsId : 'priceGrabberguts',
			microClass : 'priceGrabberMicroFormat',
			bx : null,
			guts : null
		},
		
		css : {
			itm : 'priceGrabberSngItm',
			itmAlt : 'priceGrabberSngItmAlt',			
			itmImg : 'priceGrabberSngItmImg',
			itmLiBase : 'priceGrabberSngItmTxt_'
		},
		searchTerm : ''
	},
	
	
	initialize : function ( options)
	{
		this.setOptions(options);
		
		this.capture();	
		this.options.ui.bx	= $( this.options.ui.containerId );
		this.options.ui.guts = $( this.options.ui.gutsId );
		if( this.options.ui.bx === null || this.options.ui.guts === null || this.options.searchTerm === '' ) { return null; }
		
		this.connect();
	},
	
	capture : function() {
		var itms = $$( "." + this.options.ui.microClass );	
		if( itms.length <= 0 ) { return false; }
		this.options.searchTerm = itms[0].get('html');
	},
	
	renderObjects : function( products ) {

		for(var i=0; i<products.length; i++) {
			this.renderItem( products[i], i ).inject( this.options.ui.guts );
		}
		this.renderClear().inject( this.options.ui.guts );
		
	},
	
	renderClear : function() {
		var d = new Element("div", { 'class' : 'hr' }).inject( this.options.ui.guts );
		new Element("hr").inject( d );
		return d;	
	},
	
	renderItem : function( itm, num ) {
		var clss = ( num % 2) ? this.options.css.itm + " " + this.options.css.itmAlt : this.options.css.itm;
		var d = new Element("div", {
			'class' : clss
		});
		
		var img = new Element("img", {
			src : itm.img,
			border : 0,
			'class' : this.options.css.itmImg,
			styles : {
				'cursor' : 'pointer'
			}
		}).inject( d );
		
		var ul = new Element("ul").inject( d );
		var data = [ itm.title, itm.price, itm.brand, itm.seller ];
		for(var i=0; i<data.length; i++) {
			
			var li = new Element("li", {
				'class' : this.options.css.itmLiBase + i
			}).inject( ul );			
		
			if( i=== 0 || i === 3 ) {  
		
				new Element("a", {
					href : '#',
					'html' : data[i]
				}).inject( li );			
			
			} else {
				li.set('html', data[i]);			
			}

		}

		d.addEvent( 'click', function(e) {
			this.priceClick( itm.url );
		}.bind(this) );
		
		this.renderClear().inject( d );
		
		return d;
	},
	
	priceClick : function( location ) {
		window.open( location );
	},
	
	/////////////////////////
	// Events
	////////////////////////
	
	connect : function() {
		this.options.params.r = $time();
		this.options.params.site = document.location.host;
		this.options.params.s = this.options.searchTerm;
		this.serviceConnect( this.options.params, this.connectComplete );
	},
	
	serviceConnect : function( parameters, callBack ) {
		//console.log('word up');
		new JsonP( { 
					url : this.options.url, 
					params: parameters, callBackKey : 'c', 
					onComplete : callBack.bind(this)  
					} ).request();	
	},
	
	connectComplete : function( jsonObj ) {
	
		if( jsonObj && jsonObj.response.statusCode === 200 ) { 
			this.options.ui.bx.setStyle( 'display', 'block' );
			this.renderObjects( jsonObj.response.data.products );
		}			
		
		return;
	}
	
});

//]]>
