/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * advertisement.js
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 1.0 of the OpenFlyers
 * license that is available through the world-wide-web at the following
 * URI: http://www.openflyers.com/license/semifreelicense1_0.txt. If you did not receive a
 * copy of the OpenFlyers License and are unable to obtain it through the
 * web, please send a note to contact@openflyers.com so we can mail you
 * a copy immediately.
 *
 * @category    Javascript
 * @package     OpenFlyers
 * @author      Stéphane Goossens
 * @copyright   2008 OPENFLYERS S.A.R.L. <contact@openflyers.com>
 * @license     http://www.openflyers.com/license/semifreelicense1_0.txt  OpenFlyers License
 * @link        http://www.openflyers.com
 * @since       Mon Fev 16 2015
 */

/**
 * launchAds
 * 
 * @return void
 */
function launchAds() {
    if (!hasAdvertisement) {
        return false;
    }
    setInterval(updateAds, adInterval);
}

/**
 * updateAds
 * 
 * Reloading ads without refreshing the page
 * 
 * @return void
 */
function updateAds() {
    if (!document.getElementById('adBox')) {
        return;
    }
    // Prepare request
    var action     = 'getAdAgencyTag.php',
        parameters = 'structureName=' + structureName + '&userId=' + userId + '&actionName=' + actionName + '&IPAddress=' + IPAddress;
    // Request handler
    var getAdAgencyTagOnSuccess = function(response) {
        // Reloading ads without refreshing the page
        if (response == '') {
            return false;
        }
        var adBox = document.getElementById('adBox');
        /*
        // Prepare Google Adsense Ads
        if (response.search('google_ad') !== -1) {
            // @link http://stackoverflow.com/questions/6197768/dynamic-adsense-insertion-with-javascript
            var legacyWrite = document.write;
            document.write  = function (content) {
                adBox.innerHTML = content;
                document.write  = legacyWrite;
            };
        }
        */
        // Loading Ads
        $(adBox).html(response);
        // Open the ad in a new window or tab
        var adLinks = adBox.getElementsByTagName('a');
        for (var i = 0; i < adLinks.length; i++) {
            if (adLinks[i].getAttribute('rel') === 'external') {
                adLinks[i].target = '_blank';
            }
        }
    };
    // Init request
    var request = null;
    // IE < 10
    if (window.XDomainRequest) {
        request = new XDomainRequest();
        // Request handler
        request.onload = function() {
            getAdAgencyTagOnSuccess(request.responseText)
        };
    }
    else {
        request = new XMLHttpRequest();
        // Request handler
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                getAdAgencyTagOnSuccess(request.responseText)
            }
        };
    }
    // Launch request
    request.open('GET', action + '?' + parameters, true);
    request.send();
}

YAHOO.util.Event.onDOMReady(launchAds);