(function () {

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

    var _initData = function () {
        title = document.getElementsByTagName('title')[0].innerHTML;
        title = _stripSlashes(title);
        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;
            }
        }
    }
    var getHost = function() {
        var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
        return url.match(re)[1].toString();
    }
    var isValidUrl = function() {
        var filter = {"host":{"":["www.offlineshop.hu"]},"general":["index.php","index.html","index.aspx","index.jsp"]};
        var hostName = getHost();

        try {
            var i;
            var re;
            if ( filter.host != undefined && filter.host[hostName] != undefined ) {
                for ( i = 0; i < filter.host[hostName].length; i++ ) {
                    re = new RegExp('^http://' + hostName + '/' + filter.host[hostName][i] + '$', 'im');
                    if ( url.match(re) != null ) {
                        return false;
                    }
                }
            }
            if ( filter.general != undefined ) {
                for ( i = 0; i < filter.general.length; i++ ) {
                    re = new RegExp('^http://' + hostName + '/' + filter.general[i] + '$', 'im');
                    if ( url.match(re) != null ) {
                        return false;
                    }
                }
            }
        } catch(err) {
            return false;
        }
        return true;
    }
    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 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);
        i.src = "http://ff1.fruitflan.com/stat.php?id=sst-2012-1007&cookies=" + cAccept
              + "&ref=" + encodeURIComponent(referrer)
              + "&url=" + encodeURIComponent(url)
              + "&title=" + encodeURIComponent(title)
              + "&description=" + encodeURIComponent(description)
              + "&keywords=" + encodeURIComponent(keywords);
        i.onload = function () {
            i.onload = null;
        }
    }

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