﻿
function invokeJSONService(serviceURL, containerId, jsonParameters)
{
    var containerHtml = $(containerId).get('html').trim();   
    if (containerHtml == '') {
    
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');
                
                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode(jsonParameters));        
     }
}


function injectControlHtmlFromWebService(serviceURL, controlLocation, containerId, title) 
{
    invokeJSONService(serviceURL,containerId, 
    { 'controlLocation': controlLocation, 'title': title });
  
    /*

    var containerHtml = $(containerId).get('html').trim();
    if (containerHtml == '') {
    
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');
                
                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'controlLocation': controlLocation, 'title': title }));
        
    }
    */
}

function injectHtmlFromWebService(serviceURL, controlLocation, containerId) 
{

    invokeJSONService(serviceURL,containerId, 
    { 'controlLocation': controlLocation });

/*
    var containerHtml = $(containerId).get('html').trim();
    if (containerHtml == '') {
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');
                
                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'controlLocation': controlLocation }));
    }
   */
}

function injectChannelControlFromWebService(serviceURL, controlLocation, channel, containerId) 
{
    invokeJSONService(serviceURL,containerId, 
    { 'controlLocation': controlLocation, 'channel': channel });
/*
    var containerHtml = $(containerId).get('html').trim();
    if (containerHtml == '') {
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');
                
                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'controlLocation': controlLocation, 'channel': channel }));
    }
 */
}

function injectStoryControlFromWebService(serviceURL, controlLocation, category, channel, containerId) 
{
    invokeJSONService(serviceURL,containerId, 
    { 'controlLocation': controlLocation, 'category': category, 'channel': channel });
/*
    if ($(containerId).get('html') == '') {
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');
                
                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'controlLocation': controlLocation, 'category': category, 'channel': channel }));
    }
 */
}

function injectCompanyControlFromWebService(serviceURL, controlLocation, ticker, containerId) 
{
    invokeJSONService(serviceURL,containerId, 
    { 'controlLocation': controlLocation, 'ticker': ticker });
    /*
    if ($(containerId).get('html') == '') {
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');

                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'controlLocation': controlLocation, 'ticker': ticker }));
    }
    */
}

function injectArticleControlFromWebService(serviceURL, controlLocation, articleId, containerId) 
{
    invokeJSONService(serviceURL,containerId, 
    { 'controlLocation': controlLocation, 'articleId': articleId });
    /*
    if ($(containerId).get('html') == '') {
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');

                $(containerId).set('html', html);
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'controlLocation': controlLocation, 'articleId': articleId }));
    }
    */
}

function injectAdTagFromWebService(serviceURL, dimensions, posNo, kwPair, containerId) 
{
    invokeJSONService(serviceURL,containerId, 
    { 'adSize': dimensions, 'position': posNo, 'keywordPair': kwPair});
    /*
    if ($(containerId).get('html') == '') {
        var request = new Request.JSON({
            url: serviceURL,
            onSuccess: function(html) {
                if ($(containerId).hasClass('loading'))
                    $(containerId).removeClass('loading');
                else if ($(containerId).hasClass('loading_container'))
                    $(containerId).removeClass('loading_container');
                    // Not working
                $(containerId).set('html', html);
                //alert($(containerId).get('html'));
                eval($(containerId).get('html'));
            },
            onFailure: function(error) { alert(error); },
            urlEncoded: false,
            headers: { "Content-type": "application/json" }
        });
        request.send(JSON.encode({ 'adSize': dimensions, 'position': posNo, 'keywordPair': kwPair}));
    }
    */
}