/*
	jQuery Plugin spy (leftlogic.com/info/articles/jquery_spy2)
	(c) 2006 Remy Sharp (leftlogic.com)
	$Id: spy.js,v 1.4 2006/09/30 11:05:04 remy Exp $
    // 2009/01/16 [PEACH]
    // Add paramID to send object value to server side
*/
$.fn.spy = function(settings) {    
	var spy = this;
	spy.epoch = new Date(1970, 0, 1);
	spy.last = '';
	spy.parsing = 0;
	spy.waitTimer = 0;
	spy.json = null;
    spy.paramID = settings.paramID;

	if (!settings.ajax) {
		alert("An AJAX/AJAH URL must be set for the spy to work.");
		return;
	}
	
	spy.attachHolder = function() {
		// not mad on this, but the only way to parse HTML collections
		if (o.method == 'html')
            if($("#"+spy[0].id+"_spyTmp").length > 0)
                $("#"+spy[0].id+"_spyTmp").empty();
            else
                $('body').append('<div style="display: none!important;" id="'+spy[0].id+'_spyTmp"></div>');
	}

    if($("#"+spy[0].id+"SpyRunning").length > 0)
        $("#"+spy[0].id+"SpyRunning").val(1);
    else
        $('body').append("<input id='"+spy[0].id+"SpyRunning' type='hidden' value='1' />");
        
    if($("#"+spy[0].id+"SpyStop").length > 0)
        $("#"+spy[0].id+"SpyStop").val(0);
    else
        $('body').append("<input id='"+spy[0].id+"SpyStop' type='hidden' value='0' />");

	// returns true for 'no dupe', and false for 'dupe found'
	// latest = is latest ajax return value (raw)
	// last = is previous ajax return value (raw)
	// note that comparing latest and last if they're JSON objects
	// always returns false, so you need to implement it manually.
	spy.isDupe = function(latest, last) {
		if ((last.constructor == Object) && (o.method == 'html'))
			return (latest.html() == last.html());
		else if (last.constructor == String)
			return (latest == last);
		else
			return 0;
	}
	
	spy.timestamp = function() {
	    var now = new Date();
		return Math.floor((now - spy.epoch) / 1000);
	}
	
	spy.parse = function(e, r) {
		spy.parsing = 1; // flag to stop pull via ajax
		if (o.method == 'html') {
			$('div#'+spy[0].id+'_spyTmp').html(r); // add contents to hidden div
		} else if (o.method == 'json') {
			eval('spy.json = ' + r); // convert text to json
		}
		if ((o.method == 'json' && spy.json.constructor == Array) || o.method == 'html') {
			if (spy.parseItem(e)) {
				spy.waitTimer = window.setInterval(function() {
                    if($("#"+spy[0].id+"SpyStop").val() == 1){
                        //console.log("STOP SPY "+spy[0].id);
                        spy.parsing = 0;
                        clearInterval(spy.waitTimer);
                        $("#"+spy[0].id+"_spyTmp").empty();
                        return false;
                    }
					if ($("#"+spy[0].id+"SpyRunning").val() == 1) {
						if (!spy.parseItem(e)) {
							spy.parsing = 0;
							clearInterval(spy.waitTimer);
						}
					}
				}, o.pushTimeout);
			} else {
				spy.parsing = 0;
			}
		} else if (o.method == 'json') { // we just have 1
			eval('spy.json = ' + r)
			spy.addItem(e, spy.json);
			spy.parsing = 0;
		}
	}
	
	// returns true if there's more to parse
	spy.parseItem = function(e) {
		if (o.method == 'html') {
			// note: pre jq-1.0 doesn't return the object
			var i = $('div#'+spy[0].id+'_spyTmp').find('div:first').remove();
			if (i.size() > 0) {
				i.hide();
				spy.addItem(e, i);
			}		
			return ($('div#'+spy[0].id+'_spyTmp').find('div').size() != 0);
		} else {
			if (spy.json.length) {
				var i = spy.json.shift();
				spy.addItem(e, i);
			}

			return (spy.json.length != 0);
		}
	}
	
	spy.addItem = function(e, i) {
		if (! o.isDupe.call(this, i, spy.last)) {
			spy.last = i; // note i is a pointer - so when it gets modified, so does spy.last
			$('#' + e.id + ' > div:gt(' + (o.limit - 2) + ')').remove();
			$('#' + e.id + ' > div:gt(' + (o.limit - o.fadeLast - 2) + ')').fadeEachDown();
			o.push.call(e, i);
			$('#' + e.id + ' > div:first').fadeIn(o.fadeInSpeed);
		}
	}
	
	spy.push = function(r) {
		$('#' + spy[0].id).prepend(r);
	}
	
	var o = {
		limit: (settings.limit || 10),
		fadeLast: (settings.fadeLast || 5),
		ajax: settings.ajax,
		timeout: (settings.timeout || 3000),
		pushTimeout: (settings.pushTimeout || settings.timeout || 3000),
		method: (settings.method || 'html').toLowerCase(),
		push: (settings.push || spy.push),
		fadeInSpeed: (settings.fadeInSpeed || 'slow'), // 1400 = crawl
		timestamp: (settings.timestamp || spy.timestamp),
		isDupe: (settings.isDupe || spy.isDupe),
        paramID: (settings.paramID || spy.paramID)
	};

	spy.attachHolder();

	return this.each(function() {
		var e = this;
	    var timestamp = o.timestamp.call();
		var lr = ''; // last ajax return
		spy.ajaxTimer = window.setInterval(function() {
			if (($("#"+spy[0].id+"SpyRunning").val()==1) && (!spy.parsing)) {
                if(o.paramID == null)
                {
                    $.post(o.ajax, { 'timestamp': timestamp }, function(r) {
                        spy.parse(e, r);
                    });
                }
                else
                {       
                        ///////////////////// MODIFY BY PEACH FOR PASSPARAMETER
                        var listParam = o.paramID;
                        var parameterString = "";
                        for(var j=0; j<listParam.length;j++){
                            parameterString += " '"+listParam[j]+"': '"+$( "#"+listParam[j]).val()+"' ";
                            parameterString += ", "
                        }
                        parameterString = "var tempParameter = { "+parameterString + "'timestamp':"+timestamp+" };";
//                        if($.browser.mozilla)
//                            console.log("tempParameter is "+parameterString);
                        eval(parameterString);
                        ///////////////////// MODIFY BY PEACH FOR PASSPARAMETER
                    
                    $.post(o.ajax, tempParameter, function(r) {
                        spy.parse(e, r);
                    });
                }
			    timestamp = o.timestamp.call();
			}
            if($("#"+spy[0].id+"SpyStop").val()==1){
                //console.log("STOP SPY "+spy[0].id);
                clearInterval(spy.ajaxTimer);
                $("#"+spy[0].id+"_spyTmp").empty();
                return false;
            }
		}, o.timeout);
	});


};

$.fn.fadeEachDown = function() {
	var s = this.size();
	return this.each(function(i) {
		var o = 1 - (s == 1 ? 0.5 : 0.85/s*(i+1));
		var e = this.style;
		if (window.ActiveXObject)
			e.filter = "alpha(opacity=" + o*100 + ")";
		e.opacity = o;
	});
};

function PauseSpy(id){
    $("#"+id+"SpyRunning").val(0);
    return false;
}
function PlaySpy(id){
    $("#"+id+"SpyRunning").val(1);
    return false;
}
function ExitSpy(id){
    $("#"+id+"SpyStop").val(1);
    return false;
}