/**
 * @author Touchnote
 *  - requires jQuery +1.3 + low Pro for jQuery
 *  - This is a behavior for popup previews of postcards
 *  TODO:: currently these only work on elements that have an id
 */		
 
/**
 * Playback Events for Click Tale
 */
var ClickTalePlayback = $.klass({
	
	// Fires on each element when initialised
	initialize: function() { },
  
  	CTIsRecording: function() {
  		return ((typeof ClickTaleExec == "function") && this.ClickTaleIsIn("recording"));
  	},
  
	CTIsPlayback: function() {
		try { return parent && parent.WebPlayer; }
		catch(e) { return false; }
	},
	
	
	ClickTaleIsIn: function(testFor) {
	    var topLocation = top.location;
	
	    if(testFor == "recording" && window.location == topLocation) {
	        return true;
	    } else if(testFor == "recording" || window.location == topLocation) {
	        return false;
	    }
	
	    switch(testFor.toLowerCase()) {
	    case "report":
	        var fn = arguments.callee;
	        return fn("scroll-heatmap") || fn("click-heatmap") || fn("form-analytics");
	    case "scroll-heatmap":
	        var regex = new RegExp("Heatmap.aspx\?", "i");
	        return regex.test(topLocation);
	    case "click-heatmap":
	        var regex = new RegExp("ClickHeatMap.aspx\?", "i");
	        return regex.test(topLocation);
	    case "form-analytics":
	        var regex = new RegExp("FormAnalytics.aspx\?", "i");
	        return regex.test(topLocation);
	    case "playback":
	        var regex = new RegExp("Player.aspx\?", "i");
	        return regex.test(topLocation);
	    }
	},
	
	CtExec: function (fn){
		if (this.CTIsRecording()){ // we are / could be recordingClickTaleExec(fn);
			ClickTaleExec(fn);
		}else if(this.ClickTaleIsIn("playback")){ // executes in PLAYBACK MODE
			//alert('we are in playback');
		}
	},
	
	getBubbledElement: function (firstBubbledElement){
			while(firstBubbledElement.attr('id') == ""){
				firstBubbledElement = $(firstBubbledElement).parent(); // keep going up till we find an ID (sim bubbling)
			}
			return firstBubbledElement;
	},
  
  	// Re-fire the event in the clicktale player
	onclick: function(el) { 
	
		// executes in RECORD MODE
		if (this.CTIsRecording()){ // we are / could be recording
			ClickTaleExec("$('#" + this.getBubbledElement(this.element).attr('id') + "').click()");
		}else if(this.ClickTaleIsIn("playback")){ // executes in PLAYBACK MODE
			//alert('we are in playback');
		}
	}
});