var statistic = {
    req : null,
    which : null,
    statlock : false,
    contextPath : "/",
    openNewWindow : false,

    initialize : function(contextPath, openNewWindow) {
        this.contextPath = contextPath;
        if(openNewWindow) {
            this.openNewWindow = openNewWindow;
        }
    },

    registerCarClicks : function (statisticType, carId){
        if (! this.statlock){
            this.statlock = true;
            this.retrieveURL(this.contextPath + '/statcount.do?m=click&stt=' + statisticType + '&cId='+carId);
            this.statlock = false;
        }
    },

    retrieveURL : function(url) {
        //Do the Ajax call
        if (window.XMLHttpRequest) { // Non-IE browsers
            this.req = new XMLHttpRequest();
            //  req.onreadystatechange = processStateChange;
            try {
                this.req.open("GET", url, true); //was get
                this.req.send(null);
            } catch (e) {
                //    alert("Problem Communicating with Server\n"+e);
            }
        } else if (window.ActiveXObject) { // IE
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
            if (this.req) {
                //    req.onreadystatechange = processStateChange;
                try {
                    this.req.open("GET", url, true);
                    this.req.send();
                } catch (e) {

                }
            }
        }
    },

    clickKaitoriBanner : function(htmlId) {
        $("#" + htmlId).trigger("click");
        if(this.openNewWindow) {
            var kaitoriWindow = window.open("http://www.buy-car.jp/kaitori.html",
                "kaitoriWindow", "scrollbars=1,resizable=yes,status=yes,toolbar=yes,location=yes,menubar=yes,fullscreen");
            kaitoriWindow.focus();
        }
        else {
            window.location.href = "http://www.buy-car.jp/kaitori.html";
        }
    },

    dwrCounter : function(obj) {
        var result = function(a){};
		var handler = {
		        callback: result,
		        exceptionHandler: function(answer, exception) {},
		        errorHandler: function(message, exception) {}
	         	};
        CounterService.count(obj.id, handler);
        return true;
    }
}

function clickKaitoriBanner() {
    statistic.clickKaitoriBanner("top-right-banner");
}