var lang = 'EN';

var xmlDoc;
var isIE = false;
var firstTime = true;

var _container;

function loadContent(url, container)
{
    if(xmlDoc != null)
    {
        xmlDoc.onreadystatechange = function() {};
        xmlDoc.abort();
    }
    
    _container = container;
    
    var path = '/HTML/' + lang + '/' + url;
    
    if(window.location.href.toLowerCase().indexOf('/movies/') > 0)
        path = '/Movies' + path;
    
    _container.innerHTML = '<div class="Text" style="height:' + (_container.offsetHeight - 6) + 'px;">Loading...</div>';
    
    loadUrl(path);
}

function viewContent(url, container)
{
    if(xmlDoc != null)
    {
        xmlDoc.onreadystatechange = function() {};
        xmlDoc.abort();
    }
    
    _container = document.getElementById(container);
    
    var path = '/HTML/' + lang + '/' + url;
    
    if(window.location.href.toLowerCase().indexOf('/movies/') > 0)
        path = '/Movies' + path;
    
    _container.innerHTML = '<div class="Text" style="height:' + (_container.offsetHeight - 6) + 'px;">Loading...</div>';
    
    loadUrl(path);
}

function viewContent2(url, container)
{
    if(xmlDoc != null)
    {
        xmlDoc.onreadystatechange = function() {};
        xmlDoc.abort();
    }
    
    _container = document.getElementById(container);
    
    var path = '/HTML/' + lang + '/' + url;
    
    if(window.location.href.toLowerCase().indexOf('/movies/') > 0)
        path = '/Movies' + path;
    
    _container.innerHTML = '<div class="Text" style="background-color:#efefef;">Loading...</div>';
    
    loadUrl(path);
}

function showNews(mode, params)
{
    if(xmlDoc != null)
    {
        xmlDoc.onreadystatechange = function() {};
        xmlDoc.abort();
    }
    
    _container = document.getElementById('contentToShow');
    
    var path = '/newslist.aspx?MODE=' + mode;
    
    if(params != '')
        path += '&PARAMS=' + params;
    
    _container.innerHTML = '<div class="Text" style="height:' + (_container.offsetHeight - 6) + 'px;">Loading...</div>';
    
    loadUrl(path);
}

function loadUrl(path)
{
    try
    {
        xmlDoc = new XMLHttpRequest();
        xmlDoc.onreadystatechange = processReqChange;
        xmlDoc.open("GET", path, true);
        xmlDoc.send(null);
    }
    catch(ex)
    {
        isIE = true;
        xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlDoc) {
            xmlDoc.onreadystatechange = processReqChange;
            xmlDoc.open("GET", path, true);
            xmlDoc.send();
        }
    }
}

function processReqChange()
{
    if(xmlDoc.readyState == 4)
    {
        if (xmlDoc.status == 200)
        {
            var response = xmlDoc.responseText;
            
            _container.innerHTML = getResponseBody(response);
        }
        else alert(xmlDoc.status);
    }
    
    if(firstTime && location.hash != '')
    {
        setTimeout('goToAnchor("' + location.hash.slice(1) + '")', 200);
        
        firstTime = false;
    }
}

function getResponseBody(content) 
{
   var test = content.toLowerCase();
   var x = test.indexOf("<body");
   if(x == -1) return "";

   x = test.indexOf(">", x);
   if(x == -1) return "";

   var y = test.lastIndexOf("</body>");
   if(y == -1) y = test.lastIndexOf("</html>");
   if(y == -1) y = content.length;

   return content.slice(x + 1, y);   
} 

function goToAnchor(anchor)
{
    var obj = document.getElementsByName(anchor);
    
    if(isIE)
    {
        try
        {
            obj[0].scrollIntoView();
        }
        catch(ex)
        {
            setTimeout('goToAnchor("' + anchor + '")', 200);
        }
    }
    else
    {
        obj[0].click();
    }
}
