/*
 *  _a_cast.js - copyright(c) activecore,Inc. 2005-2011
 * 
 *  This software is licensed by activecore, Inc.
 *  You CAN NOT use this file except in compliance with the License.
 *
 *  Product  : ad insight
 *  Version  : 1.4 
 *  Rev      : 1.1
 *  Last Update: 2010/08/10
 */
var _c_host = 'tracer02.a-cast.jp';
var _c_creative_js = "/creative_click.js?cid="; 
function _adGetCookie(cn) {
   var get_data = document.cookie;
   var cv = new Array();
   var gd = get_data.split(";");
   var i;
   for (i = 0; i < gd.length; i++) { 
      var a = gd[i].split("=");
      a[0] = a[0].replace(" ","");
      cv[a[0]] = a[1];
   }
   if (cv[cn]) return cv[cn];
   else return "";
}
function _adSetCookie(cn,val,exp) {
   document.cookie = cn + "=" + val + "; path=/; expires=" + exp + ";";
}
function writeFlash(tags) {
    document.write(tags); 
}
function _ACast(_target, _alt, _alt_link, _width, _height, _location_id, _alt_div) {
    this.ref=escape(document.referrer);
    try {
        if( parent != self ){
            this.ref=escape(top.document.referrer);
        }
    } catch(e) {
        // this.ref = escape(document.referrer);
    }
    this.url=escape(window.location);
    this.target = _target;
    this.alt = _alt;
    this.alt_link = _alt_link;
    this.timerId = null;
    this.timeout = 8 * 1000;
    this.width = _width;
    this.height = _height;
    this.location_id = _location_id;
    this.alt_div = _alt_div;
}
_ACast.prototype.timeoutHandler = function() {
    clearInterval(this.timerId);
    var _id = document.getElementById("ID" + this.location_id);
    if (_id == null) {
        // var _alt_id = document.getElementById(this.location_id);
        // Rev1.1
        var _alt_id = document.getElementById(this.location_id) != null ? document.getElementById(this.location_id) : document.getElementById('adi' + this.location_id);
        if (_alt_id != null && this.alt != '') {
            if (_alt_id.innerHTML) {
                _alt_id.innerHTML = '<a href="' + this.alt_link + '">' 
                + '<img src="' + this.alt + '" border="0" alt="" width="' + this.width + '" height="' + this.height + '">' + '</a>';
            }
        } else {
            if (_alt_id != null && this.alt_div != undefined) {
                var _alt_div = document.getElementById(this.alt_div);
                if (_alt_div) {
                    _alt_id.innerHTML = _alt_div.innerHTML;
                }
            }
        }
    }   
}
_ACast.prototype.request = function() {
    var _now = new Date();
    this.timerId = setInterval( "_aCast_" + this.location_id + ".timeoutHandler()" , this.timeout);
    var _ac_ck = _adGetCookie('ac'); 
    var _adsvr_ck = _adGetCookie('ac_adsvr');
    document.write('<script language=\"JavaScript\" src=\"' + this.target + 'url=' + this.url + ';' + 'referer=' + this.ref + ';rand=' + Math.round(Math.random() * _now.getTime()) + ';' + location.protocol 
    + ';ac_del=&' + 'ac_ck=' + _ac_ck + '&adsvr_ck=' + _adsvr_ck +  '\"><\/script>');
}
function _void() { 
    return; 
}
function ac_SetStatus(_cid, _status, _url) {
    if (_status == "") return;
    if (_cid == "") return;
    var _now = new Date();
    var _proto=location.protocol;
    if (_proto != "http:" && _proto != "https:") {
        return;
    }
    var _path=_proto + '//' + _url + '?' + "st_" + _cid + "=" + _status;
    document.write('<script language=\"JavaScript\" src=\"' + _path + '&' + 'rand=' + Math.round(Math.random() * _now.getTime()) + '\"><\/script>');
    
}

function _aCastNoRedirectClick(_redirectURL, _c_location_id, _c_creative_id, _c_cid, _adsvr_ck, _ac_ck) {
    var _c_proto = window.location.protocol;
    var _now = new Date();
    if (_c_proto == "http:" || _c_proto == "https:") {
        var _path = _c_proto 
        + '//' + _c_host + _c_creative_js + _c_cid + '&location_id=' + _c_location_id 
        + '&creative_id=' + _c_creative_id
        + '&rand=' + Math.round(Math.random() * _now.getTime()) 
        + ';ac_del=&ac_ck=' + _ac_ck 
        + '&adsvr_ck=' + _adsvr_ck 
        ;
        var _img=new Image(1,1);
        _img.src=_path;
        _img.onload=function() {_void();}
    }
    location.href=_redirectURL;
    return;
}

// V1.2
function fillZero(s) {
    var digits = 5;
    if (s == null || s == undefined) {
        return s;
    }
    return Array((digits + 1) - s.toString().split('').length).join('0') + s;
}
function _adGetOnClick(_cid, _loc_id, _ck_id, _isDir) { 
    var _c_cid = _cid;
    var _c_location_id = _loc_id;
    var _ac_ck = '';
    var _adsvr_ck = '';
    if ('function' === typeof _adGetCookie) {
        _ac_ck = _adGetCookie('ac');
        _adsvr_ck = _adGetCookie('ac_adsvr');
    }
    var _dir = '';
    if (_isDir != undefined) {
        if (_isDir == 'y') {
            _dir = '/c' + fillZero(_cid);
        }
    }
    var _c_proto = window.location.protocol;
    var _now = new Date();
    var _url = escape(window.location);
    var _path = _c_proto 
        + '//' + _c_host + _dir + _c_creative_js + _c_cid + '&location_id=' + _c_location_id 
        + '&ck_id=' + _ck_id
        + '&rand=' + Math.round(Math.random() * _now.getTime()) 
        + '&ck_url=' + _url
        + ';ac_del=&ac_ck=' + _ac_ck 
        + '&adsvr_ck=' + _adsvr_ck
        ;
    try {   
        var _img=new Image(1,1);
        _img.src=_path;
        _img.onload=function() {_void();}
    } catch (e) {
        // alert(e);
    }

    if (navigator.userAgent.indexOf("MSIE") < 0) {
        var sec = 1;
        var start = new Date;
        while (_img.complete == false) {
            var cur = new Date;
            if (sec * 1000 <= cur.getTime() - start.getTime()) {
                break;
            }
        }
    }
    
}


