// JavaScript Document

//jwplayer events for hot spot store data

function postData() {
    if(startedPlaying != -1) {
        //e.preventDefault();
        endPlaying = parseInt(jwplayer().getPosition());
		totalVideo = parseInt(jwplayer().getDuration());
		//alert('endPlaying '+endPlaying+' totalVideo '+totalVideo);								
        $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
        startedPlaying = -1;
        var i = 0;
        while(i<5000)
        {
            i++;
        }
     } 
}

jwplayer('preview').onPlay( function() {
    startedPlaying =  parseInt(jwplayer().getPosition(), 10);
    if(startedPlaying == totalVideo)
        startedPlaying = 0;
});
                            
jwplayer('preview').onPause( function(){
    endPlaying = parseInt(jwplayer().getPosition());
	totalVideo = parseInt(jwplayer().getDuration());
    $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
    startedPlaying = -1;
});
                            
/*jwplayer('preview').onSeek( function(event){
    endPlaying = parseInt(event.position, 10);
    if(startedPlaying != -1) {
		totalVideo = parseInt(jwplayer().getDuration());
        $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
        startedPlaying = parseInt(event.offset);
    }
    else
        startedPlaying = endPlaying;
});*/
                            
jwplayer('preview').onComplete( function() {
    endPlaying = totalVideo = parseInt(jwplayer().getDuration());
	if(startedPlaying == -1)
	    startedPlaying = 0;
    $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
    startedPlaying = -1;
    endPlaying = -1;
});

jwplayer('preview').onTime( function() {
    var currentTime = parseInt(jwplayer().getPosition(), 10);
    if((endPlaying +1) != currentTime && endPlaying != currentTime ) {
        //alert("seekVideo viewerStatisticID :"+viewerId+", totalVideoLength : "+totalVideo+", startedPlayingTime : "+startedPlaying+", endPlayingTime : "+endPlaying);
        totalVideo = parseInt(jwplayer().getDuration(), 10);
        //Seek is happened
        if(startedPlaying != -1 && startedPlaying != endPlaying) {
            $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
        }
        startedPlaying = currentTime;
    }
    endPlaying = parseInt(jwplayer().getPosition(), 10);
});
                            
/*jwplayer('preview').onBuffer( function() {
    if(startedPlaying != -1)
    {
        endPlaying = parseInt(jwplayer().getPosition());
		totalVideo = parseInt(jwplayer().getDuration());
        $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
        startedPlaying = -1;
    }
});*/

/* Following events are for iPhone Events & android events.
    iPhone uses <Video> Tag to play video.
    new_templates/Video_slice_common.php, Contain Video tag to display iPhone 
*/

    function iPhonePlayVideo() {
        //alert("play");
        //alert("play \nCurrent Time" + iPhoneVideo.currentTime+"\nDuration :"+iPhoneVideo.duration);
        startedPlaying =  parseInt(iPhoneVideo.currentTime, 10);
        //alert("play startedPlaying "+startedPlaying);
        if(startedPlaying == totalVideo)
            startedPlaying = 0;
    }
    
    function iPhoneTimeUpdateVideo() {
        var currentTime = parseInt(iPhoneVideo.currentTime, 10);
        if((endPlaying +1) != currentTime && endPlaying != currentTime ) {
            totalVideo = parseInt(iPhoneVideo.duration, 10);
            //Seek is happened
            if(startedPlaying != -1 && startedPlaying != endPlaying) {
                $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
            }
            startedPlaying = currentTime;
        }
        endPlaying = parseInt(iPhoneVideo.currentTime, 10);
    }
    
    function iPhonePauseVideo() {
        //alert("pauseVideo Current Time" + iPhoneVideo.currentTime+"\nDuration :"+iPhoneVideo.duration);
        endPlaying = parseInt(iPhoneVideo.currentTime, 10);
    	totalVideo = parseInt(iPhoneVideo.duration, 10);
    	//alert("pauseVideo viewerStatisticID :"+viewerId+", totalVideoLength : "+totalVideo+", startedPlayingTime : "+startedPlaying+", endPlayingTime : "+endPlaying);
        $.post("Video_Hotspot_StoreData.php", {"viewerStatisticID": ""+viewerId, "totalVideoLength": ""+totalVideo, "startedPlayingTime": ""+startedPlaying, "endPlayingTime": ""+endPlaying}, "json");
        startedPlaying = -1;
    }
