(function (){var _root=tangane;var _package=_root.package.create('mediatis.tracking.omniture.codeOperation',{file:'codeOperation.js',authors:['bj'],files:{},needed:['omniture.js','request.js']});
    //// add code here ////
    var omniture = _root.package.get('mediatis.tracking.omniture',_package);
    var request = _root.package.get('utils.request',_package);
    
	_package.CodeOperation = function(){
        var self = this;
        self.values={};
    };
	_package.CodeOperation.prototype.params ={
        ori:{sep:'1',length:3,urlIn:'ori',urlOut:'ori'},
        sup:{sep:'2',length:3,urlIn:'sup',urlOut:'sup'},
        cmp:{sep:'3',length:3,urlIn:'cmp',urlOut:'cmp'},
        pro:{sep:'4',length:3,urlIn:'pro',urlOut:'pro'},
        ofr:{sep:'5',length:3,urlIn:'ofr',urlOut:'ofr'},
        rec:{sep:'6',length:5,urlIn:'co'},
        dat:{sep:'7',length:6}
    };
    _package.CodeOperation.prototype.cookieName = 'tangane.mediatis.tracking.omniture.codeOperation';

    _package.CodeOperation.prototype.writeCookie=function(){
        var self=this;
        window.document.cookie=_package.CodeOperation.prototype.cookieName+'='+self.getCode()+';path=/;';
    };
    _package.CodeOperation.prototype.setCode=function(code){
        var self=this;
        if(code){
            var start, end;
            var param, value;
            for(var name in _package.CodeOperation.prototype.params){
                param = _package.CodeOperation.prototype.params[name];
                start = code.indexOf(param.sep);
                if(start!=-1){
                    end = start+param.sep.length+param.length;
                    self.values[name] = code.slice(start,end);
                    code=code.substring(end);
                };
            };
        };
        return self.getCode();           
    };        

    _package.CodeOperation.prototype.getCode=function(){
        var self=this;
        var code='', value;
        for(name in _package.CodeOperation.prototype.params){
            value = self.values[name];
            if(value){
                code+=value;
            };
        };
        return code;
    }; 
        
    _package.CodeOperation.prototype.loadFromUrl=function(){
        var self=this;
        var param, value, start;
        for(var name in _package.CodeOperation.prototype.params){
            param = _package.CodeOperation.prototype.params[name];
            if(param.urlIn){
                value = _root.trim(_root.request.getParameter(param.urlIn));
                if(value){
                    start = value.indexOf(param.sep);
                    if(start==0){
                        value = value.substring(start+param.sep.length);                 
                    };
                    if(value.length==param.length){
                        self.values[name]= param.sep+value;
                    };
                };
            };
        };
        return self.getCode();           
    };

    _package.CodeOperation.prototype.getUrlParams=function(){
        var self=this;
        var param, name, value;
        var request='';
        for(var name in _package.CodeOperation.prototype.params){
            param = _package.CodeOperation.prototype.params[name];
            if(param.urlOut){
                value = self.values[name];
                if(value){
                    request+='&'+param.urlOut+'='+value;
                };
            };
        };
        return request;
    };
    
    _package.CodeOperation.prototype.setDate=function(time){
        var self=this;
        var name = 'dat';
        var month;
        var date=new Date();
        if(time){
            date.setTime(time);
        };
        month=String(date.getMonth()+1);
        self.values[name]=_package.CodeOperation.prototype.params.dat.sep+date.getFullYear()+(month.length<2?'0'+month:month);
    };
    
    _package.CodeOperation.prototype.readCookie=function(){
        var self=this;
        var start = window.document.cookie.indexOf(_package.CodeOperation.prototype.cookieName+'=');
        if(start!=-1){
            start+=_package.CodeOperation.prototype.cookieName.length+1;
            self.setCode(window.document.cookie.slice(start,window.document.cookie.indexOf(';',start)));
        };
        return self.getCode();           
    };
    
    //////////////////////////////
    // see codeOperation.js.jsp //
    _package.CodeOperation.prototype.ID_ANY = '_ANY_';
    _package.CodeOperation.prototype.ID_NONE = '_NONE_';
    _package.CodeOperation.prototype.CODE_NOREFERER = '_NOREFERER_';
    _package.CodeOperation.prototype.refererToCode = {};
    _package.CodeOperation.prototype.refererToKey=new RegExp(':\\/\\/.*?(\.[^.]*?\.[^.]*?)(?:\\/|\\?|:)','i');
    _package.CodeOperation.prototype.loadFromReferer=function(referer){
        var self = this;
        if(self.refererToCode){
            var code;
            if(referer){
                var domain = self.refererToKey.exec(referer);
                if(domain && domain.length>1){
                   domain = domain[1];
                   code = self.refererToCode[domain];
                   if(!code){
                        code = self.refererToCode[self.ID_ANY];
                   };
                };
             };
             if(!code){
                code = self.refererToCode[self.ID_NONE];
             };
             if(code && code!=self.CODE_NOREFERER){
                self.setCode(code);
             };
        };
        return self.getCode();           
    };
})();