document.write("<div id='CyNContent' style='position:fixed;bottom:0px;right:0px;margin-right: 15px;border-left:solid 1px gray;border-top:solid 1px gray;border-right:solid 1px gray;font-family: tahoma, arial;font-size: 8pt;background-color: #f7f7f7;padding: 2px;vertical-align:center; text-align: center;opacity: .75;cursor:hand;display:none;width:600px;height:400px'></div>");

CyJ.Count=1;
function CyJ(fullUrl) 
{
    this.onreadystatechange=function(){if(this.readyState == 'complete'){document.getElementsByTagName("head").item(0).removeChild(this);}}; 
    this.onload=function(){document.getElementsByTagName("head").item(0).removeChild(this)}; 
    this.scriptObj = document.createElement("script"); 
    this.scriptObj.setAttribute("type", "text/javascript"); 
    this.scriptObj.setAttribute("charset", "utf-8"); 
    this.scriptObj.setAttribute("src", fullUrl + '&nc=' + (new Date()).getTime()); 
    this.scriptObj.setAttribute("id", 'JSONScriptId'+CyJ.Count); 
    this.scriptObj.onreadystatechange=this.onreadystatechange;
    this.scriptObj.onload=this.onload;
    document.getElementsByTagName("head").item(0).appendChild(this.scriptObj);
    CyJ.Count++;
    this.StartTime = new Date();
}

var sSession;
var s = new Date();
if (navigator.userAgent.indexOf('Mac') >= 0 && s.getTimezoneOffset() >= 720)
s.setTime (s.getTime() - 1440*60*1000);

var sCookies = document.cookie.toString();

if(sCookies.indexOf("_cyId") == -1){
	sSession = parseInt( Math.random()*100000000 ) + "_" + s.getTime();
	document.cookie = "_cyId=" + sSession + ";expires=Mon, 31-Dec-2010 00:00:00 GMT;path=/;";
}
sCookies = document.cookie.toString();
if(sCookies.indexOf('_cyId') == -1){
	sSession = "";
}else{
	if(sCookies.indexOf(';') == -1) sCookies = sCookies.replace(/_cy/g, ';_cy');
	var start = sCookies.indexOf("_cyId=") + 7;
	var end = sCookies.indexOf(";",start);
	if (end == -1) end = sCookies.length;
	sSession = unescape(sCookies.substring(start,end));
}

var basePage = "http://" + ((location.host.indexOf("dev")!=-1)?"dev.":"") + "stats.curchy.com";





CyStat.prototype.Init = function (){
        this.Update();
    };
    
CyStat.prototype.Update = function(action, param){
        var url = basePage + "/" + sSession + "/CyJSON.axd?callback=_cyN.Callback";
        if(action)  url += "&action=" + action;
        if(param)   url += "&" + param;
        _updateRequest = new CyJ(url);
    };
    
function CyStat(){
        var _updateRequest;
    };
    
    
    
    

CyC.prototype.Update = function (){
        this._updateRequest = new CyJ(this._updateUrl + this._updateParams);
    };

CyC.prototype.Callback = function (o){
        this._updateParams = "&u=1";
        
        if(o.InstantMessage)
        {
            var h = "";
            
            h+= "You received a message from <b>" + o.InstantMessage.User + "</b> on " + o.InstantMessage.MessageDate + "<br/>";
            h+= "<br/><b>" + o.InstantMessage.Message + "</b>";
            var o = this.cyN.GetAndReset(500,60,h);
            o.style.textAlign="";
        }
        
        setTimeout("_cyN.CU.Update();", 5000);
    };

function CyC(cyN){
        var _updateRequest;
        var _updateUrl;
        var _updateParams;
        
        this.cyN=cyN;
        
        this._updateUrl = basePage + "/" + sSession + "/Cy.axd?callback=_cyN.CU.Callback";
        this._updateParams = "&url="+escape(window.location) + 
                    "&ref=" + escape(document.referrer) +
                    "&lng=" + ((!document.all || navigator.userAgent.match('Opera')) ? navigator.language : navigator.userLanguage) + 
                    "&tz=" + (Math.round(new Date('dec 1, 2002').getTimezoneOffset()/60)*-1);
        if(screen) this._updateParams += "&scr=" + escape(screen.width + "x" + screen.height + "x" + screen.colorDepth);
    };






CyN.prototype.Init = function(){
        this.Stat.Init();
        this.CU.Update();
    }
    
CyN.prototype.Show = function (){
        var o = document.getElementById('CyNContent');
        o.style.textAlign='center';
        o.style.width="100px";
        o.style.height="17px";
        o.innerHTML = "Loading...";
        
        this.Stat.Update();
    };
    
CyN.prototype.Callback=function(r)
    {
        _currentStats = r;
        
        var width = 30;
        var h="";
        
        if(_currentStats.CountryFlag){
            h += "<img src='"+basePage+"/CyResource.axd?r=images.worldflags."+_currentStats.CountryFlag+".gif' border='0'/> ";
            width += 20;
        }

        if(_currentStats.Comment){
            h += "<a href='#' onclick='_cyN.ShowComment();return false;' title='Send a Comment'>Comment</a> | ";
            width += 40;
        }

        if(_currentStats.Contact){
            h += "<a href='#' onclick='_cyN.ShowContact();return false;' title='Contact'>Contact</a> | ";
            width += 40;
        }
            
        if(_currentStats.Feedback){
            h += "<a href='#' onclick='_cyN.ShowFeedback();return false;' title='Feedback'>Send Feedback</a> | ";
            width += 40;
        }

        if(_currentStats.OnlineUsers)
        {
            width += 100;
            h += _currentStats.OnlineUsers + " user" + ((_currentStats.OnlineUsers>1)?"s":"") + " online | ";
        }
        
        h = h.substring(0, h.length-2);
        
        var o = document.getElementById('CyNContent');
        if(!_currentStats.Contact && !_currentStats.Feedback && !_currentStats.Contact)
            o.onmouseover=this.MouseOver;
        else
            h += "<a href='#' onclick='_cyN.MouseOver();return false;' style='border:0px;'><img src='"+basePage+"/CyResource.axd?r=images.max.gif' style='margin:0px;padding:0px;border:0px;'/></a>";
            
        o.innerHTML = "<span style='color:black;'>" + h + "</span>";
        o.style.textAlign="center";
        o.style.width = width + "px";
        o.style.height="17px";
        o.style.display='inline';
        
        if(h.length==0) o.style.display="none";
        else o.style.display="inline";
    };
    
    
CyN.prototype.MouseOver = function (){
        if(this._notificationTimeOut) 
            clearTimeout(this._notificationTimeOut);
            
        var o = document.getElementById('CyNContent');
        o.onmouseover="";
        o.onmouseout= function (){
        function isChildOf(parent, child)
        {
            if(child != null)
                while(child.parentNode)
                    if((child = child.parentNode) == parent)
                        return true;
            return false;
        }
        
        var o = document.getElementById('CyNContent');
        var cmt = null;
        if(event.toElement) cmt= event.toElement;
        else if(event.relatedTarget) cmt = event.relatedTarget;
        if( !isChildOf(o, cmt) && o != cmt )
	        _notificationTimeOut = setTimeout("_cyN.Show();", 3000);
    };
        o.style.width="150px";
        o.style.height="130px";
        o.style.cursor = "";
        o.innerHTML = "<span style='color:black;'>" +
            "<a style='border: 0px;' target='_outline' href='http://outline.curchy.com'><img src='" + basePage + "/CyResource.axd?r=images.outlinesm.png' style='border:0px;'/></a><br/><br/>" +
            "<b>" + _currentStats.OnlineUsers + "</b> user" + ((_currentStats.OnlineUsers>1)?"s":"") + " online<br/>" +
            "The farthest user is at <b>" + Math.round(_currentStats.FarthestUser) + "</b> km from me.<br/>" + 
            "<br/>" +
            "<a href='#' onclick='_cyN.ShowMap();return false;'>Live Map</a> | " + 
            "<a href='#' onclick='_cyN.Show();return false;'>Close</a></span>";
    };
    
CyN.prototype.GetAndReset = function(width, height, content){
        if(this._notificationTimeOut) 
            clearTimeout(this._notificationTimeOut);

        var o = document.getElementById('CyNContent');
        o.style.display='none';
        o.style.textAlign='center';
        o.style.width=width+"px";
        o.style.height=height+"px";
        o.onmouseover=null;
        o.onmouseout=null;
        o.onclick=null;

        var h = "<div style='text-align:center;position:fixed;bottom:"+(height-45)+"px;right:0px;margin-right: 20px; z-index:100;background-color:#f7f7f7; border: solid 1px gray; padding: 3px;color: black;z-index:100;'>";
        h += "<a style='border: 0px;' target='_outline' href='http://outline.curchy.com'><img src='" + basePage + "/CyResource.axd?r=images.outlinesm.png' style='border:0px;'/></a><br/>"
        h += "<a style='border: 0px;' href='#' onclick='_cyN.Show();return false;'>Close</a>";
        h += "</div>";
        o.innerHTML = h + content;
        o.style.display='inline';
        
        return o;
    };
    
CyN.prototype.ShowContact = function (msg){

        if(!msg) msg="";
    
        var h="";
        
        h += "<table align='left'>";
        h += "<tr><td>Your Name</td><td><input id='CyName' type='text' width='50' style='border:solid black 1px;'/></td></tr>";
        h += "<tr><td>Your Email</td><td><input id='CyEmail' type='text' width='50' style='border: solid black 1px;'/></td></tr>";
        h += "<tr><td>Message</td><td><textarea id='CyMessage' type='text' cols='50' rows='10' style='border:solid black 1px;'>" + msg + "</textarea></td></tr>";
        h += "<tr><td></td><td align='right'><input type='button' value='Send' onclick='_cyN.ContactSend();return false;' style='border:solid black 1px;'/></td></tr>";
        h += "</table>";

        var o = this.GetAndReset(500,260, h);
        o.style.textAlign="";
    };
    
CyN.prototype.ContactSend = function (){
        var url = "";
        url += "name=" + escape(document.getElementById("CyName").value) + "&";
        url += "email=" + escape(document.getElementById("CyEmail").value) + "&";
        url += "comment=" + escape(document.getElementById("CyMessage").value) + "&";
        this.Stat.Update("Contact", url);
    };
    
CyN.prototype.ShowComment = function (msg){
        
        if(!msg) msg="";
        
        var h="";
        
        h += "<table align='left'>";
        h += "<tr><td>Message</td><td colspan='2'><textarea id='CyMessage' type='text' style='width:350px; height: 100px;border:solid black 1px;'>" + msg + "</textarea></td></tr>";
        h += "<tr><td>Your Name</td><td colspan='2'><input id='CyName' type='text' width='50' style='border:solid black 1px;'/></td></tr>";
        h += "<tr><td>Your Email</td><td><input id='CyEmail' type='text' width='50' style='border: solid black 1px;'/></td>";
        h += "<td align='right'><input type='button' value='Send' onclick='_cyN.ContactSend();return false;' style='border:solid black 1px;'/></td></tr>";
        h += "</table>";

        var o = this.GetAndReset(500,170,h);
        o.style.textAlign="";
    };
    
CyN.prototype.CommentSend = function (){
        var url = "";
        url += "name=" + escape(document.getElementById("CyName").value) + "&";
        url += "email=" + escape(document.getElementById("CyEmail").value) + "&";
        url += "comment=" + escape(document.getElementById("CyMessage").value) + "&";
        this.Stat.Update("Contact", url);
    };
    
CyN.prototype.ShowMap = function (){
        var h = "<iframe src='" + basePage + "/live/map.aspx?rnd=" + (new Date()).getTime() + "' width='100%' height='100%' frameborder='0' />";
        var o = this.GetAndReset(600,400,h);
    };
    
function CyN()
{
    var _notificationTimeOut;
    var _currentStats=null;
    
    this.Stat = new CyStat();
    this.CU = new CyC(this);
}

var _cyN = new CyN();

if(window.attachEvent) window.attachEvent("onload", new function(){_cyN.Init()});
else if(window.addEventListener) window.addEventListener("load", new function(){_cyN.Init()});
else if(document.addEventListener) document.addEventListener("onload", new function(){_cyN.Init()}, false);