var request;

function handleLinkClick(evt, url, objectId)
{
    _cancelEvent(evt);
    
    // Ajax stuff... (needs to return null if not a file (or something!))
    doAjaxRequest('/Ajax/InternalLinkHandler.ashx',
     'url=' + url + '&id=' + objectId, 
     linkClickAjaxHandler );
}

function linkClickReturnHandler(isFile, newUrl, vanityName)
{
    if (isFile)
    {
        // call google-analytics
        pageTracker._trackPageview(vanityName);
    }
    
    document.location = newUrl;
}

function _cancelEvent (evt) {
    if(window.event) {
        window.event.returnValue = false;
    } else if(evt) {
        evt.preventDefault();
    }
}

doAjaxRequest = function(url, params, completeHandler) {
    var req = request = getXmlHttpRequest();
    req.onreadystatechange = completeHandler;
	req.open("POST", url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(params);
}

releaseXmlRequest = function() {
    this.req = null;
}

function linkClickAjaxHandler() {
    var xhr = request;
	if(xhr.readyState == 4) {
	    try {
		    eval(xhr.responseText);
		    releaseXmlRequest();
		} catch(e) {
		    alert("Something went wrong with the AJAX request \n" + e.message);
		}
	}
}

// AJAX helpers
function getXmlHttpRequest() {
	var xhr = null;
	
	try {
		xhr = new ActiveXObject("MSXML2.XMLHTTP");
	} catch(e) { 
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e) {
			xhr = false;
		}
	}
	
	if(!xhr && typeof XMLHttpRequest != 'undefined') {
		xhr = new XMLHttpRequest();
	}
	
	return xhr;
};

var pd4me = {};
pd4me.activity = function(contentId, activityId){
   
        $.ajax({
            type: "POST",
            url: "/activity.ashx",
            data: ({ id: contentId, activityId: activityId })
        });
};

var activityPlugin = function() { 
 
    // the "this" variable is a pointer to the current Player instance 
    var player = this; 
    
    var contentId = this.id().match(/\d+$/)[0];
    
    // myPlugin does something when the player is loaded 
    this.onLoad(function() { 
 
        console.log('onload flowplayer');
       
    }); 
    
    this.onSeek(function() {
        
        console.log('onSeek flowplayer');
         pd4me.activity(contentId,3);
    });
    
    this.onStart(function() {
        console.log('onStart flowplayer');
         
         pd4me.activity(contentId,2);
    });
    
    this.onPause(function() {
        console.log('onPause flowplayer');
         pd4me.activity(contentId,5);
    });
    
    this.onResume(function() {
        console.log('onResume flowplayer');
        pd4me.activity(contentId,2);
    });
    
    this.onStop(function() {
        console.log('onStop flowPlayer');
         pd4me.activity(contentId,4);
    });    
   
   this.onLastSecond(function() {
    console.log('onLastSecond flowplayer');
   });
   
   this.onFinish(function() {
    console.log('onFinish flowplayer');
    pd4me.activity(contentId, 6);
    });
 
    // the player instance should be returned to enable plugin and method chaining 
    return this; 
 
};


$(document).ready(function() {

    $('#recentCommentsWrapper').click(function(e){
        
        if ($(e.target).is('.removeComment'))
        {
             e.preventDefault();
            $.ajax({
                type: "POST",
                url:"/ajax/CommentReadHandler.ashx",
                data:({ cirId: e.target.id }),
                success: function(comments) {
                    $('#recentComments').replaceWith(comments); /// could be empty            
                },
                error: function(xhr, textStatus, errorThrown) {
                    $('#message').text('an error has occurred and the comment was not removed').show().addClass('errorMessage');
                }
                
            })
        }
    });
    
});