var _devictio_use_title_as_name = 0;
var _devictio_install_tracker = 1;
var _devictio_tracker_pause = 500;
var _devictio_download_extensions = "7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip";

// Beginning script
function _devictio_plug_normal(_devictio_pl) {
	if (_devictio_tm.indexOf(_devictio_pl) != -1 && (navigator.mimeTypes[_devictio_pl].enabledPlugin != null)) 
		return '1';
	return '0';
}

function _devictio_plug_ie(_devictio_pl)
{
	pk_found = false;
	document.write('<SCR' + 'IPT LANGUAGE=VBScript>\n on error resume next \n pk_found = IsObject(CreateObject("' + _devictio_pl + '")) </SCR' + 'IPT>\n');
	if (pk_found) return '1';
	return '0';
}

var _devictio_jav = '0'; if(navigator.javaEnabled()) _devictio_jav='1';
var _devictio_agent = navigator.userAgent.toLowerCase();
var _devictio_moz = (navigator.appName.indexOf("Netscape") != -1);
var _devictio_ie = (_devictio_agent.indexOf("msie") != -1);
var _devictio_win = ((_devictio_agent.indexOf("win") != -1) || (_devictio_agent.indexOf("32bit") != -1));
var _devictio_cookie = (navigator.cookieEnabled)? '1' : '0';
if((typeof (navigator.cookieEnabled) == "undefined") && (_devictio_cookie == '0')) {
	document.cookie="_devictio_testcookie"
	_devictio_cookie=(document.cookie.indexOf("_devictio_testcookie")!=-1)? '1' : '0';
}

var _devictio_dir='0',_devictio_fla='0',_devictio_pdf='0',_devictio_qt = '0',_devictio_rea = '0',_devictio_wma='0'; 
if (_devictio_win && _devictio_ie){
	_devictio_dir = _devictio_plug_ie("SWCtl.SWCtl.1");
	_devictio_fla = _devictio_plug_ie("ShockwaveFlash.ShockwaveFlash.1");
	if (_devictio_plug_ie("PDF.PdfCtrl.1") == '1' || _devictio_plug_ie('PDF.PdfCtrl.5') == '1' || _devictio_plug_ie('PDF.PdfCtrl.6') == '1') _devictio_pdf = '1';
	_devictio_qt = _devictio_plug_ie("Quicktime.Quicktime"); // Old : "QuickTimeCheckObject.QuickTimeCheck.1"
	_devictio_rea = _devictio_plug_ie("rmocx.RealPlayer G2 Control.1");
	_devictio_wma = _devictio_plug_ie("wmplayer.ocx"); // Old : "MediaPlayer.MediaPlayer.1"
} else {
	var _devictio_tm = '';
	for (var i=0; i < navigator.mimeTypes.length; i++)
		_devictio_tm += navigator.mimeTypes[i].type.toLowerCase();
	_devictio_dir = _devictio_plug_normal("application/x-director");
	_devictio_fla = _devictio_plug_normal("application/x-shockwave-flash");
	_devictio_pdf = _devictio_plug_normal("application/pdf");
	_devictio_qt  = _devictio_plug_normal("video/quicktime");
	_devictio_rea = _devictio_plug_normal("audio/x-pn-realaudio-plugin");
	_devictio_wma = _devictio_plug_normal("application/x-mplayer2");
}
	
var _devictio_rtu = '';
try {
	_devictio_rtu = top.document.referrer;
} catch(e1) {
	if(parent){ 
		try{ _devictio_rtu = parent.document.referrer; } catch(e2) { _devictio_rtu=''; }
	}
}
if(_devictio_rtu == '') {
	_devictio_rtu = document.referrer;
}

function _devictio_escape(_devictio_str){
	if(typeof(encodeURIComponent) == 'function') {
		return encodeURIComponent(_devictio_str);
	} else {
		return escape(_devictio_str);
	}
}
var _devictio_title = '';
if (document.title && document.title!="") _devictio_title = _devictio_escape(document.title);

var _devictio_called;

function _devictio_getUrlLog( _devictio_action_name, _devictio_site, _devictio_devictiourl, _devictio_custom_vars )
{
	var _devictio_custom_vars_str = '';
	if(typeof _devictio_custom_vars == "undefined"){
		_devictio_custom_vars = false;
	}
	if (_devictio_custom_vars) {
		for (var i in _devictio_custom_vars){
			if (!Array.prototype[i]){
				_devictio_custom_vars_str = _devictio_custom_vars_str + '&vars['+ escape(i) + ']' + "=" + escape(_devictio_custom_vars[i]);
			}
		}
	}
	
	var _devictio_url = document.location.href;
	var _devictio_da = new Date();
	var _devictio_src = _devictio_devictiourl
		+'?url='+_devictio_escape(document.location.href)
		+'&action_name='+_devictio_escape(_devictio_action_name)
		+'&idsite='+_devictio_site
		+'&res='+screen.width+'x'+screen.height	+'&col='+screen.colorDepth
		+'&h='+_devictio_da.getHours()+'&m='+_devictio_da.getMinutes()+'&s='+_devictio_da.getSeconds()
		+'&fla='+_devictio_fla+'&dir='+_devictio_dir+'&qt='+_devictio_qt+'&realp='+_devictio_rea+'&pdf='+_devictio_pdf
		+'&wma='+_devictio_wma+'&java='+_devictio_jav+'&cookie='+_devictio_cookie
		+'&title='+_devictio_title
		+'&urlref='+_devictio_escape(_devictio_rtu)
		+_devictio_custom_vars_str;
	return _devictio_src;
}

function devictio_log( _devictio_action_name, _devictio_site, _devictio_devictiourl, _devictio_custom_vars )
{
	if(_devictio_called && (!_devictio_action_name || _devictio_action_name=="")) return;
	var _devictio_src = _devictio_getUrlLog(_devictio_action_name, _devictio_site, _devictio_devictiourl, _devictio_custom_vars );
	document.writeln('<img src="'+_devictio_src+'" alt="Devictio interface pro" style="border:0" />');
	if(!_devictio_action_name || _devictio_action_name=="") _devictio_called=1;
	
  _devictio_init_tracker(_devictio_site, _devictio_devictiourl);
}

function _devictio_add_event(elm, evType, fn, useCapture) 
{
	if (elm.addEventListener) { 
		elm.addEventListener(evType, fn, useCapture); 
		return true; 
	} else if (elm.attachEvent) { 
		var r = elm.attachEvent('on' + evType, fn); 
		return r; 
	} else {
		elm['on' + evType] = fn;
	}
}

var _devictio_tracker_site, _devictio_tracker_url;

function _devictio_init_tracker(_devictio_site, _devictio_devictiourl) 
{
	if( typeof(piwik_install_tracker) != "undefined" )
		_devictio_install_tracker = piwik_install_tracker;
	if( typeof(piwik_tracker_pause) != "undefined" )
		_devictio_tracker_pause = piwik_tracker_pause;
	if( typeof(piwik_download_extensions) != "undefined" )
		_devictio_download_extensions = piwik_download_extensions;

	_devictio_hosts_alias = ( typeof(piwik_hosts_alias) != "undefined" ? piwik_hosts_alias : new Array())
	_devictio_hosts_alias.push(window.location.hostname);

	if( !_devictio_install_tracker )
		return;

	_devictio_tracker_site = _devictio_site;
	_devictio_tracker_url = _devictio_devictiourl;

	if (document.getElementsByTagName) {
		linksElements = document.getElementsByTagName('a')
		for (var i = 0; i < linksElements.length; i++) {
		if( linksElements[i].className != 'piwik_ignore' )
			_devictio_add_event(linksElements[i], 'mousedown', _devictio_click, false);
		}
	}
}

function _devictio_dummy() { return true; }

function _devictio_pause(_devictio_time_msec) {
	var _devictio_now = new Date();
	var _devictio_expire = _devictio_now.getTime() + _devictio_time_msec;
	while(_devictio_now.getTime() < _devictio_expire)
		_devictio_now = new Date();
}

// _devictio_type only 'download' and 'link' types supported
function devictio_track(url, _devictio_site, _devictio_url, _devictio_type) 
{
	var _devictio_image = new Image();
	_devictio_image.onLoad = function() { _devictio_dummy(); };
	_devictio_image.src = _devictio_url + '?idsite=' + _devictio_site + '&' + _devictio_type + '=' + escape(url) + '&rand=' + Math.random() + '&redirect=0';
	_devictio_pause(_devictio_tracker_pause);
}

function _devictio_is_site_hostname(_devictio_hostname) {
	for(i = 0; i < _devictio_hosts_alias.length; i++)
		if( _devictio_hostname == _devictio_hosts_alias[i] ) 
			return true;
	return false;
}

function _devictio_click(e)
{
	var source;

	if (typeof e == 'undefined')
		var e = window.event;

	if (typeof e.target != 'undefined') 
		source = e.target;
	else if (typeof e.srcElement != 'undefined')
		source = e.srcElement;
	else return true;

	while( source.tagName != "A" )
		source = source.parentNode;

	if( typeof source.href == 'undefined' )
		return true;

	var _devictio_download = new RegExp('\\.(' + _devictio_download_extensions + ')$', 'i');
	var _devictio_link_type;
	var _devictio_not_site_hostname = !_devictio_is_site_hostname(source.hostname);

	if( source.className == "piwik_download" )
		_devictio_link_type = 'download';
	else if( source.className == "piwik_link" ) {
		_devictio_link_type = 'link';
		_devictio_not_site_hostname = 1;
	}
	else _devictio_link_type = (_devictio_download.test(source.href) ? 'download' : 'link');

	if( _devictio_not_site_hostname || _devictio_link_type == 'download' ) 
		devictio_track(source.href, _devictio_tracker_site, _devictio_tracker_url, _devictio_link_type);

	return true;
}
