/**
 * popup hompy window
 */
var hompywindow = '', fullwindow = '';
var isIE_var = '';
function isIE(){
    var browser=navigator.appName;
    if( browser=="Microsoft Internet Explorer")
	return '1';
    return '-1';
}

function popup(url, width, height, name, params_override) {
    var name = name || 'GodGame',
        width = width || 1000,
        height = height || 665,
        params_override = params_override || {},
        params = {
            menubar: 'no', scrollbars: 'no', resizable: 'yes',
            width: width, height: height,
            'top': (screen.availHeight-height)/2, left: (screen.availWidth-width)/2
        },
        l = [], k, v;
    
    for( var k in params ){
        v = (typeof params_override[k] !== 'undefined') ? params_override[k] : params[k];
        l.push( k + '=' + v );
    }

    if (hompywindow && !hompywindow.closed && hompywindow.location) {
        hompywindow.location.href = url;
    }
    else {
        hompywindow = window.open(url, name, l.join(','));
        try {
            if (!hompywindow.opener) {
                hompywindow.opener = self;
            }
        } catch(e) {}
    }

    if (window.focus && hompywindow) {
        hompywindow.focus();
    }
    return false;
}

function popupFull(url) {
    var name = "GodGameFull";
    if (!fullwindow.closed && fullwindow.location) {
        fullwindow.location.href = url;
    } else {
        fullwindow = window.open(url,name);
        if (!fullwindow.opener) fullwindow.opener = self;
    }
    if (window.focus) { fullwindow.focus(); }
    return false;
}

function genericAJAX(method, url, poststr, callback) {
    var req;
    if (window.XMLHttpRequest)
        req = new XMLHttpRequest();
    else if (window.ActiveXObject)
        req = new ActiveXObject("Microsoft.XMLHTTP");
    else
	return;

    req.open(method, url);
    req.onreadystatechange = function() {
	if( req.readyState == 4 && req.status == 200 ) {
	    callback(req.responseText);
	}
    };
    if( method == "POST" ) {
	req.setRequestHeader('Content-Type',
			     'application/x-www-form-urlencoded');
	req.send(poststr);
    } else {
	req.send(null);
    }
}


window['showTime'] = (function () {
    var _js_src = 'http://gmy.mwsrv.com/.mass/gamelist.showTime.js',
        _current_ctime = null,
        _ctime_offset = (-480 - (new Date()).getTimezoneOffset()) * 60,
        _timer = {
            fetch: null,
            sys: null
        },
        _clock = null,
        _d_head = null;

    function _loadScript() {
        if (_d_head === null) {
            _d_head = document.getElementsByTagName('head')[0];
        }
        var js = document.createElement('script'); js.async = true;
        js.src = _js_src + '?' + (new Date()).getTime();
        _d_head.appendChild(js);
    }

    return function showTime(set_ctime) {
            if (typeof set_ctime === 'undefined') {   // first run
                _loadScript();
                _timer.sys = setInterval(function () {
                        var now = (new Date(_current_ctime)).toString(),
                            t = now.split(' ');

                        _current_ctime += 1e3;

                        if (_clock === null) {
                            _clock = document.getElementById('clock');
                        }

                        if (isIE_var == '') {
                            isIE_var = isIE();
                        }
                        if (isIE_var == '-1') {
                            _clock.innerHTML = t[3]+" "+t[1]+" "+t[2]+" "+t[4];
                        }
                        else if (isIE_var == '1') {
                            _clock.innerHTML = t[5]+" "+t[1]+" "+t[2]+" "+t[3];
                        }
                    }, 1e3);
                _timer.fetch = setInterval(function () {
                        _loadScript();
                    }, 10e3);
            }
            else {  // sync ctime
                _current_ctime = (set_ctime - _ctime_offset) * 1e3;
            }
        };
}());

function shareFacebook(u,t){
 window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
}

function sharePlurk(u,t){
 window.open('http://www.plurk.com/?qualifier=shares&status=' .concat(encodeURIComponent(u)) .concat(' ') .concat('(') .concat(encodeURIComponent(t)) .concat(')'));
}

function shareTwitter(u,t){
 window.open('http://twitter.com/home/?status='.concat(encodeURIComponent(t)) .concat(' ') .concat(encodeURIComponent(u)));
}

function logout() {
    window.location = "/logout.php?origurl=" + escape(window.location);
}

