function ajax( url, parameters, callback, method ) {
    var req = new XMLHttpRequest();
    if (typeof (method) == 'undefined') {
        method = 'GET';
    }
    var paramString;
    if ( typeof(parameters) != 'undefined' ) { 
        var paramArray = [ ];
        for ( var key in parameters ) {
            paramArray[ paramArray.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(parameters[key]);
        }
        paramString = paramArray.join("&").replace(/%20/g, "+");
    }
    req.onreadystatechange = function( ) {
        if (req.readyState == 4) {
            callback.call(this, req.responseText, req.status);
            setLinkUrls();
            setFormsSubmit( );
        }
    }

    if ( paramString ) {
        url += '?' + paramString;
    }
    req.open( method, url, true );
    req.send();
}

function loadContent ( url, parameters, useHash ) {
    if ( typeof( useHash ) == 'undefined' ) {
        useHash = false;
    }
    if ( typeof( parameters ) == 'undefined' ) {
        parameters = '';
    }
    if ( useHash ) {
        var hash = document.location.hash;
        hash = hash.replace('#','');
        if ( hash !== '' ) {
            url = hash;
        }
    }
    var contentElement = document.getElementById( 'container' );
    var loading = document.getElementById( 'loading' );
    loading.className = 'show';
    if( url.indexOf("login") == -1 ) {
        document.location.hash = url;
    }

    _gaq.push(['_trackPageview', url ]);
    
    ajax( url, parameters, function ( response, status ) {
        if (status == 200) {
            contentElement.innerHTML = response;
        }
        loading.className = 'hide';
    } );
}

function setShowStatus ( element, id ) {
    var checkbox = document.getElementById( 'cb' + id );
    var checked = 0;
    if (checkbox.innerHTML == '') {
        checkbox.innerHTML = '<span></span>';
        checked = 1;
    }
    else {
        checkbox.innerHTML = '';
        checked = 0;
    }
    
    var loading = document.getElementById( 'loading' );
    loading.className = 'show';
    ajax( '/check', { "watched" : id, "remove" : (checked != 1) }, function ( response ) {
        if ( response == '1' ) {
            // some feedback?
        }
        loading.className = 'hide';
    } );
    return false;
}

function setFormsSubmit ( ) {
    var forms = document.querySelectorAll( 'form' );
    for (var i = 0; i < forms.length; i ++ ) {
        if ( forms[i].className.indexOf('ready') == -1 ) {

            forms[i].onsubmit = function ( ) {
                function encode(inputs) {
                    for (var j = 0; j < inputs.length; ++ j ) {
                        if (inputs[j].name)
                            args[inputs[j].name] = inputs[j].value;
                    }
                }
                
                var args = {};
                encode( this.getElementsByTagName("input"));
                encode(this.getElementsByTagName("textarea"));
                encode(this.getElementsByTagName("select"));
                encode(this.getElementsByTagName("button"));    
                loadContent( this.action, args );

                return false;
            }

        }
    }
}

function setLinkUrls ( ) {
    var host = 'http://' + document.location.host;
    var links = document.querySelectorAll( 'a' );
    for ( i = 0; i < links.length; i ++ ) {
        if (links[i].className.indexOf('ready') == -1 && links[i].onclick == null) {
            var href = links[i].href;
            href = href.replace ( host, '' );
            links[i].href = '#'+href;
            links[i].onclick = function( ) {
                var hash = this.hash;
                hash = hash.replace( '#', '' );
                loadContent( hash );
                return false;
            };
        }
    }
}

loadContent( isLogged ? '/unwatched/' : '/login', [], true );

