var _gaq = _gaq || [];

Event.observe(window, "load", function(){

    var isForwardingToDifferentDomain = function (fullUrl, element) {
        var ignore = null;

        if(element){
            ignore = element.getAttribute("xc_ignore");
        }

        if(ignore != null){
            return false;
        }

        if(xc_uDomain == xc_cDomain){
            return false;
        }

        var destHost = fullUrl.replace(/.*\/\/([^/]+)\/?.*/, "$1");

        var firstDir = fullUrl.replace(/.*\/\/[^/]+\/([^/]+)\/?.*/, "$1");

        if((location.host == xc_uDomain && destHost == xc_cDomain && firstDir == xc_url) ||
                (location.host == xc_cDomain && destHost == xc_uDomain)){
            return true;
        }

        return false;
    }

    $A($$('form')).each(function(form){
        var actionName = form.getAttribute("action");

        if(actionName == null){
            return;
        }

        if(isForwardingToDifferentDomain(actionName, form)){
            Event.observe(form, "submit", function(){
                _gaq.push(['_linkByPost', form]);
            }, false);
        }
    });

    $A($$('a')).each(function(anchor){
        var href = anchor.getAttribute("href");

        if(href == null){
            return;
        }

        if(isForwardingToDifferentDomain(href, anchor)){
            Event.observe(anchor, "click", function (event){
                Event.stop(event)
                _gaq.push(['_link', href]);
                return false;
            }, false);
        }
    });


    ECCART.common.Function.onClickLocation = function (href){
        if(isForwardingToDifferentDomain(href, null)){
            _gaq.push(['_link', href]);
        } else {
            location.href = href;
        }
        return false;

    }

});
