
/************************************************************
	Title: 	My Favs JS
	Author:	AgencyNet
	Date: 	Sept 08
	Desc:		Gets JSON Info regarding a users myFavs
			
	Requires:
		mootools V 1.1

************************************************************/

var MyFavorites = new Class({ 
	options: {
		favsDivID		: 'dashMyFavs',
		runOnlyOnce	: 0
	},
//~~~~~~~~~~~~~~~~~~~~~~>>>>>><<<<<<~~~~~~~~~~~~~~~~~~~~~~//
	initialize: function(){
		//only do this once per page load
		if(this.options.runOnlyOnce == 0){
			
			//Get div to put table in
			this.dashFavsID = $(this.options.favsDivID);
			
			//addLoader
			this.myLoader = $('ajaxLoader');
			this.myLoader.setStyle('display','block');
			
			//Create table		
			this.myTable = new HtmlTable({
				rows: [ [{content:'Tip Title', properties: {'class': "tip heading"}},{content: 'View', properties:{'class':"viewTip heading"}}]	]
			});
			this.myTable.table.injectInside(this.dashFavsID);
			
			//Get JSON object
			this.getJSONObject();
			this.options.runOnlyOnce = 1;
		}
	},
//~~~~~~~~~~~~~~~~~~~~~~>>>>>><<<<<<~~~~~~~~~~~~~~~~~~~~~~//
	buildOutTable: function(element){
		if(element.length > 0){
			var tempTip = null;
			var tempView = null;
			element.each(function(el, i){
				tempTip =  "<a href=\"" + el.entryURL + "\">" + el.entryTitle + "</a>";
				tempView = "<a href=\"" + el.entryURL + "\"> </a>";
				//if an odd number use alt on table
				if( i%2 == 0){
					this.myTable.push([{content: tempTip, properties: {'class':"tipDetail"}}, {content: tempView, properties: {'class':"viewTipButton"}}]);
				} else {
					this.myTable.push([{content: tempTip, properties: {'class':"tipDetail alt"}}, {content: tempView, properties: {'class':"viewTipButton alt"}}]);
				}
			}.bind(this));
		} else {
			this.myTable.push([{content: "You dont have any tips saved at this point", properties: {'class':"tipDetail"}}, {content: " ", properties: {'class':"viewTipButton"}}]);
		}
	},
//~~~~~~~~~~~~~~~~~~~~~~>>>>>><<<<<<~~~~~~~~~~~~~~~~~~~~~~//
	getJSONObject: function(){
		//Get JSON object, parse into correct arrays
		var url = "/mt/plugins/ClippIt/clippit.cgi?__mode=view&a="+commenter_id;
		//var url = "/js/tempJson.txt";
		var request = new Json.Remote(url, {
			method:'get',
			onComplete: function(jsonObj) {
				this.buildOutTable(jsonObj.favorites);
				this.myLoader.setStyle('display','none');
			}.bind(this)
		},this).send();	
	}
});

MyFavorites.implement(new Events);

/*function getMyFavs(author_id) {
	new Ajax("/mt/plugins/ClippIt/clippit.cgi?__mode=view&a="+author_id, {method: 'get', onSuccess: function(res){alert(res);}}).request();
*/

//Just for reference
//window.onDomReady(MyFavorites.initialize.bind(MyFavorites));