/**
* This file finds out all desired information about the visitor's browser and outputs an image tag calling usagehandler.php
* @package Modules
* @subpackage Usage
* @author Eventus Media
* @version 1.0
*/

// This function checks if the requested plugin is found in the browser
function UsagePlugin(plugin) {
    if (MimeTypes.indexOf(plugin) != -1 && (navigator.mimeTypes[plugin].enabledPlugin != null))
        return '1';
    return '0';
}

// This function checks if the requested plugin is found in the internet explorer browser
function UsagePluginIe(plugin)
{
    found = false;
    document.write('<SCR' + 'IPT LANGUAGE=VBScript>\n on error resume next \n found = IsObject(CreateObject("' + plugin + '")) </SCR' + 'IPT>\n');
    if (found) return '1';
    return '0';
}

// Checks if javascript is enabled
var javascript = '0';
if(navigator.javaEnabled())
    javascript='1';
var useragent = navigator.userAgent.toLowerCase();
// Check for Internet Explorer
var ie = (useragent.indexOf("msie") != -1);
// Check for Microsoft Windows
var windows = ((useragent.indexOf("win") != -1) || (useragent.indexOf("32bit") != -1));
// Check for cookies
var cookies = (navigator.cookieEnabled)? '1' : '0';
// Check for cookies by setting a test cookie
if((typeof (navigator.cookieEnabled) == "undefined") && (cookies == '0')) {
    document.cookie="usage_testcookie"
    cookies=(document.cookie.indexOf("usage_testcookie")!=-1)? '1' : '0';
}
// Initialize plugin variables
var director='0',flash='0',pdf='0',quicktime = '0',realplayer = '0',windowsmedia='0';
// Check for plugins in Internet Explorer in Windows
if (windows && ie){
    director = UsagePluginIe("SWCtl.SWCtl.1");
    flash = UsagePluginIe("ShockwaveFlash.ShockwaveFlash.1");
    if (UsagePluginIe("PDF.PdfCtrl.1") == '1' || UsagePluginIe('PDF.PdfCtrl.5') == '1' || UsagePluginIe('PDF.PdfCtrl.6') == '1')        pdf = '1';
    quicktime = UsagePluginIe("Quicktime.Quicktime");
    realplayer = UsagePluginIe("rmocx.RealPlayer G2 Control.1");
    windowsmedia = UsagePluginIe("wmplayer.ocx");
// Check for plugins in some other browser or operating system
} else {
    var MimeTypes = '';
    // Get all Mime Types in a string
    for (var i=0; i < navigator.mimeTypes.length; i++)
        MimeTypes += navigator.mimeTypes[i].type.toLowerCase();
    director = UsagePlugin("application/x-director");
    flash = UsagePlugin("application/x-shockwave-flash");
    pdf = UsagePlugin("application/pdf");
    quicktime  = UsagePlugin("video/quicktime");
    realplayer = UsagePlugin("audio/x-pn-realaudio-plugin");
    windowsmedia = UsagePlugin("application/x-mplayer2");
}

var referer = '';
// Get the referer url
try {
    referer = top.document.referrer;
} catch(e1) {
    if(parent){
        try{ referer = parent.document.referrer; } catch(e2) { referer=''; }
    }
}
if(referer == '') {
    referer = document.referrer;
}

// Encodes a string
function EscapeStr(string){
    if(typeof(encodeURIComponent) == 'function') {
        return encodeURIComponent(string);
    } else {
        return escape(string);
    }
}

// This function creates the url with all required information for usagehandler.php
function CreateImageUrl(SiteId, PageId, UsageHandlerUrl)
{
	PageId = parseInt(PageId);

    var date = new Date();
    var url = UsageHandlerUrl
        +'?url='+EscapeStr(document.location.href)
        +'&res='+screen.width+'x'+screen.height    +'&col='+screen.colorDepth
        +'&h='+date.getHours()+'&m='+date.getMinutes()+'&s='+date.getSeconds()
        +'&fla='+flash+'&dir='+director+'&qt='+quicktime+'&realp='+realplayer+'&pdf='+pdf
        +'&wma='+windowsmedia+'&java='+javascript+'&cookie='+cookies
        +'&urlref='+EscapeStr(referer)
        +'&siteid='+SiteId
        +'&pageid='+PageId
    // Finally output the image tag code
    document.writeln('<img src="'+url+'" alt="Usage" style="border:0" />');
}
