﻿window.onload = (function()
{
    processMenus();
});

function showNewsLetterSignup()
{
    YUI().use('node', function(Y) { 
        var contentNode = Y.Node.get('#NewsLetter'); 
        contentNode.setStyle('display','block');
    });
}

function sendEmailAddress()
{
    var data = '';
    
    data += 'email=' + document.getElementById('email').value;
    
    postAjaxMessage('addNewsLetterEmail',data,function(xmlhttp){});
    
    YUI().use('node', function(Y) { 
        var contentNode = Y.Node.get('#NewsLetter'); 
        contentNode.setStyle('display','none');
    });
}

function sendContactForm()
{
    var data = '';
    
    data += 'name=' + document.getElementById('Name').value + '&email=' + document.getElementById('email').value + '&telno=' + document.getElementById('telno').value + '&enquiry=' + document.getElementById('enquiry').value;
    
    postAjaxMessage('addContactInfo',data,function(xmlhttp){
        YUI().use('node', 'dd', function(Y) { 
            var contentNode = Y.Node.get('#FeedBackForm');
            var createNode = Y.Node.create('<p>Thank you for your enquiry</p>');
            
            contentNode.replaceChild(createNode, contentNode.get('children').item(0));
        });
    });
}

function getLayout()
{
    var theData;
    var begin;

    begin = location.search.indexOf("page=");
    if (begin >0 )
    {
        theData = location.search.substring(begin+5,location.search. length);
        theData = unescape(theData);
    }
    
    //alert('getting layout');
    
    sendAjaxMessage("getPage","&Page="+theData,processAjaxResponse);
}

function processAjaxResponse(xmlhttp)
{
    var response = xmlhttp.responseText;
    
    //alert(response);

    var type = response.substring(response.indexOf('<type>')+6,response.indexOf('</type>'));
    
    content = response.substring(response.indexOf('<content>')+9,response.indexOf('</content>'));
    
    //alert('content ' + content);
    
    if(type == "LayoutEdit")
    {
        YUI().use('node', 'anim', function(Y) { 
            var contentNode = Y.Node.get('#ContentArea'); 
            var createNode = Y.Node.create('<div>'+content+'</div>');
            
            contentNode.appendChild(createNode);
            
            //alert(contentNode.get('text'));
            
            var loadingNode = Y.Node.get('#Loading');
            loadingNode.setStyle('display','none');
        });
        //alert('content' + content);
        
        addEditorControls();
        addMainCMSInterface();
        addEditBoxInterface();
        addPageMenuInterface();
        
        processMenus();
    }
    else if(type == "Layout")
    {
        YUI().use('node', 'anim', function(Y) { 
            var contentNode = Y.Node.get('#ContentArea'); 
            var createNode = Y.Node.create('<div>'+content+'</div>');
            
            contentNode.appendChild(createNode);
            
           // alert(contentNode.get('text'));
            
            var loadingNode = Y.Node.get('#Loading');
            loadingNode.setStyle('display','none');
        });
        
        processMenus();
    }
}

function sendAjaxMessage(message,query, callback)
{
    var xmlhttp = setupAjax();
    
    var randomnumber = Math.floor(Math.random()*999999);
    
    xmlhttp.onreadystatechange = getCallbackFunction(xmlhttp,callback);
    
    xmlhttp.open("GET","processAjaxRequest.aspx?msg="+message+query+"&rand="+randomnumber,true);
    xmlhttp.send(null);
}   

function postAjaxMessage(message, query, callback)
{
    var xmlhttp = setupAjax();
    
    xmlhttp.onreadystatechange = getCallbackFunction(xmlhttp,callback);
    
    xmlhttp.open("POST","processAjaxRequest.aspx?msg="+message,true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send(query);
}

function getCallbackFunction(xmlhttp,callback)
{
    return function()
    {
        if(xmlhttp.readyState == 4)
        {
            callback(xmlhttp);
        }
    }
}

function setupAjax()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        alert("Your browser does not support XMLHTTP!");
    }
    
    return xmlhttp;
}

function processMenus()
{
    YUI().use('node','anim', function(Y) {    
        var MenuRootNodes = Y.all('.ChildContents');
        if(MenuRootNodes != null)
        {
            MenuRootNodes.each(function(ContentNode)
            {
                var ParentNode = ContentNode.ancestor('.ParentMenuItem');
                ContentNode.setStyle('display','none');
                //ParentNode.addClass('MenuClosed');
                //ParentNode.removeClass('MenuOpen');
                
                if(ParentNode.hasClass('MenuOpen'))
                {
                    ContentNode.setStyle('display','block');
                }
                
                ParentNode.get('children').item(0).on('click',function(thisNode)
                {
                    if(ParentNode.hasClass('MenuClosed'))
                    {
                        ParentNode.addClass('MenuOpen');
                        ParentNode.removeClass('MenuClosed');
                        ContentNode.setStyle('display','block');
                    }
                    else
                    {
                        ContentNode.setStyle('display','none');
                        ParentNode.addClass('MenuClosed');
                    }
                });
            });
        }
    });
}