
function init(spy_json) {
	
	var spy = new RabbleSpy("thirdRight", 5, spy_json);

}

var RabbleSpy = Class.create();
Object.extend(RabbleSpy.prototype, {

	initialize: function(el, num_items, json) {
		
		this.el = dce("div");
		this.height = $(el).offsetHeight;
		this.el.style.height= this.height;
        this.el.style.overflow = "hidden";
		$(el).appendChild(this.el);

		this.current_item = 0;
        this.page_num = 1;
		this.items = spy_json.result;
	
		this.item_height = (this.height / num_items) - 2;
		this.num_items = num_items;

		this.new_items_timer = window.setInterval(this.get_new_items.bind(this), 10 * 1000);	
		this.next_item_timer = window.setInterval(this.next_item.bind(this), 2000);	
	},
	
	get_new_items: function() {
        
        if(++this.page_num == 3) this.page_num = 1;
        var params = "per_page=" + 20 + "&page_num=" + this.page_num;
        var request = new Ajax.Request("/Spy",
							{
								method: 'get',
								parameters: params,
								onComplete: this.new_items_callback.bind(this),
								onFailure: function() { }.bind(this),
								onException: function(t, e) { }.bind(this)
							});

	},
    
    new_items_callback: function(xhr) {
        
        var response = eval("(" + xhr.responseText + ")");
        this.items = response.result;
        this.current_item = 0;
    },

	next_item: function() {

		if(++this.current_item == this.items.length) this.current_item = 0;

		if(this.el.childNodes.length == this.num_items) {
			
			this.el.removeChild(this.el.lastChild);
		}

		/* okay we're all set to throw a new one into the mix */
		var div = this.render_item(this.current_item);
				
		if(0 == this.el.childNodes.length) {
			
			//div.style.border = "none";
			this.el.appendChild(div);
		}
		else {
			
			div.style.opacity = "0";
			Effect.Appear(div, {duration: 0.5});
			this.el.insertBefore(div, this.el.firstChild);
		}

        if(this.el.childNodes.length == this.num_items) {

            this.el.lastChild.style.border = "none";
        }
	},

	render_item: function(index) {

		var item = this.items[index];
		var div = dce("div");	
		div.style.height = this.item_height;
		div.style.width = this.el.offsetWidth - 5 + "px";
		div.style.borderBottom = "2px solid #C1D5DE";	
		
		var left_div = dce("div");
		left_div.style.cssFloat = "left";
		left_div.style.styleFloat = "left";
		left_div.style.width = "90px";

		if(item.image_id) {

			var img = dce("img");
			img.setAttribute("src", rbl_media_base_url + item.image_id + "/F85_60");
			left_div.appendChild(img);
		}
		div.appendChild(left_div);

		var right_div = dce("diV");
		right_div.style.cssFloat = "left";
        right_div.style.styleFloat = "left";
        right_div.style.position = "relative";
		right_div.style.width = this.el.offsetWidth - 90 - 5 + "px";
		
        /* build the right pane */ 
		
        var top_div = dce("div");
        top_div.style.height = "12px";
        top_div.style.overflow = "hidden";
        top_div.style.wordWrap = "break-word";
        var title = dce("span");
        title.style.fontFamily = "verdana";
        title.style.fontWeight = "bold";
        title.style.fontSize = "8pt";
        title.appendChild(ctn(item.title));
        top_div.appendChild(title);

        var bottom_div = dce("div");
        bottom_div.style.marginTop = "5px";
        bottom_div.style.height = "38px";
        bottom_div.style.overflow = "hidden";
        var lb = dce("div");
        var rb = dce("div");
        bottom_div.appendChild(lb);
        bottom_div.appendChild(rb);
        
        lb.style.cssFloat = "left";
        lb.style.styleFloat = "left";
        lb.style.marginRight = "5px";
        rb.style.cssFloat = "left";
        rb.style.styleFloat = "left";
        var ic = dce("img");
        if("CHANNEL" == item.type) {

            ic.setAttribute("src", "/images/1/channel_view/channelIconSm.gif");
        }
        else if("POST" == item.type) {

            ic.setAttribute("src", "/images/1/channel_view/postIconSm.gif");
        }
        else if("EVENT" == item.type) {

            ic.setAttribute("src", "/images/1/view_post/eventIconSm.gif");
        }
        else if("GROUP" == item.type) {

            ic.setAttribute("src", "/images/1/view_group/groupIconSm.gif");
        }
        lb.appendChild(ic);

        var content = dce("span");
        content.style.fontFamily = "verdana";
        content.style.fontSize = "8pt";
        content.appendChild(ctn(item.content));
        rb.style.width = "80px";
        rb.style.wordWrap = "break-word";
        rb.style.overflow = "hidden";
        rb.appendChild(content);

        right_div.appendChild(top_div);
        right_div.appendChild(bottom_div);
        div.appendChild(right_div);
		
		div.onmouseover = function() { 
			
			div.style.cursor = "pointer";
			div.style.backgroundColor = "#e5f1f1"; 
		};
		div.onmouseout = function() {
			
			div.style.backgroundColor = "white"; 
		};
        
        var a = dce("a");
        a.setAttribute("href", "javascript:void(null)");
        if("CHANNEL" == item.type) {

            a.setAttribute("href", "/View/Channel/" + item.id);
        }
        else if("POST" == item.type) {

            a.setAttribute("href", "/View/Post/" + item.id);
        }
        else if("EVENT" == item.type) {

            a.setAttribute("href", "/View/Post/" + item.id);
        }
        else if("GROUP" == item.type) {

            a.setAttribute("href", "/View/Group/" + item.id);
        }

        a.appendChild(div);
        var d = dce("div");
        d.appendChild(a)
		return d;
	}

});

