/**
 * If you modify this, make sure to update stat.js.min too (with YUICompressor).
 * No, this is NOT handled by the minifying framework (especially while it's not implemented)
 *   - nzoli
 */
(function () {

    var url         = new String(document.location.href),
        referrer     = document.referrer,
        title       = '',
        keywords    = '',
        description = '',
        cAccept     = false,
        af          = 0,
        topic       = '';

    var _stripSlashes = function (str) {
        str=str.replace(/\\'/g,'\'');
        str=str.replace(/\\"/g,'"');
        str=str.replace(/\\0/g,'\0');
        str=str.replace(/\\\\/g,'\\');
        return str;
    };
    var haveZonecode = function () {
        var sites = document.getElementsByTagName('script');
        for (i in sites) {
            if (sites[i].src != undefined && sites[i].src.match('http://staging.adserve.adflan.com/impression/TT.php'))
                return 1;
        }
        return 0;
    };
    var _initData = function () {
        title = document.title;
        //title = _stripSlashes(title);
        af = haveZonecode();
        var metaAray = document.getElementsByTagName('meta');
        for ( var i = 0; i < metaAray.length; i++ ) {
            switch ( metaAray[i].name.toLowerCase() ) {
                case 'description':
                    description = metaAray[i].content;
                    //description = _stripSlashes(description);
                    break;
                case 'keywords':
                    keywords = metaAray[i].content;
                    //keywords = _stripSlashes(keywords);
                    break;
                case 'category':
                    topic = metaAray[i].content;
                    break;
            }
        }
    };
    var getHost = function() {
        var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
        return url.match(re)[1].toString();
    };
    var isValidUrl = function() {
        var filter = {"host":{"visualminds.hu":["visualminds.hu/en/tag/data-visualisation","visualminds.hu/en/portfolio/16/sourcebinder"]},"general":["index.php","index.html","index.aspx","index.jsp"]};
        var hostName = getHost();
        if (hostName.substring(0, 4) == 'www.'){
            hostName = hostName.substring(4);
        }
        var i, urls = [];
        if ( filter.host != undefined && filter.host[hostName] != undefined ) {
            urls = urls.concat(filter.host[hostName]);
        }
        if ( filter.general != undefined ) {
            for (i in filter.general) {
                urls.push( hostName + '/' + filter.general[i]);
            }
        }

        var prefixes = ['', 'www.', 'http://', 'https://', 'http://www.', 'https://www.'];
        var postfixes = ['', '/'];

      for (var i_url in urls) {
        for (var i_pre in prefixes) {
          for (var i_post in postfixes) {
            var test = prefixes[i_pre] + urls[i_url] + postfixes[i_post];
            if (test == url) return false;
          }
        }
      }
      return true;
    };
    var getCookie = function (name) {
	var nameEQ = name + "=";
	var cArr = document.cookie.split(';');
	for(var i = 0; i < cArr.length; i++) {
		var cPart = cArr[i];
		while ( cPart.charAt(0) == ' ' ) {
                    cPart = cPart.substring(1,cPart.length);
                }
		if (cPart.indexOf(nameEQ) == 0) {
                    return cPart.substring(nameEQ.length, cPart.length);
                }
	}
	return null;
    };
    var cookieCheck = function () {
        document.cookie = 'accept_cookies=1; path=/';
        if ( getCookie('accept_cookies') == 1 ) {
            return true;
        }
        return false;
    };
    var sendRequest = function () {
        var i = new Image(1,1);
        var src = "http://fs1.fruitflan.com/stat.php?id=sst-100068-1201&cookies=" + cAccept
              + "&ref=" + encodeURIComponent(referrer)
              + "&url=" + encodeURIComponent(url)
              + "&title=" + encodeURIComponent(title)
              + "&description=" + encodeURIComponent(description)
              + "&keywords=" + encodeURIComponent(keywords)
              + "&af=" + af;

        if (topic != ''){
            src += "&topic=" + topic;
        }

        if (typeof af_vid != 'undefined')
            src += "&af_vid=" + af_vid + "";

        i.src = src;
        i.onload = function () {
            i.onload = null;
        }
    };

    try {
        _initData();
        if ( isValidUrl() ) {
            sendRequest();
        }
    } catch(err) { }
})();

