window.sabavisionZones = window.sabavisionZones || [];
//Fire when new push
sabavisionZones.push = function(){
    for(var zoneI in arguments){
        if(arguments[zoneI]['source']){

            var zoneId = arguments[zoneI];
            var zoneTag = document.getElementById('sabavision_zone_' + zoneId.toString() );

            if(zoneTag){
                sabavisionPoxJs.renderAds(arguments[zoneI]);
            }else{
                sabavisionPoxJs.queue.push(arguments[zoneI]);
            }
        }
    }

    return Array.prototype.push.apply(this,arguments);
};

var sabavisionPoxJs = (function () {

    var queue = [];
    var zones = (window['sabavisionZones'])? window.sabavisionZones:[];
    var baseUrl = 'api.sabavision.com/pox/?';
    var forceProtocol = 'https:';//Value> https: , http:

    var renderAds = function (zoneData) {
        var zoneId = zoneData.id;
        var zoneTag = document.getElementById('sabavision_zone_' + zoneId.toString() );

        var zoneComponent = adComponents[zoneData.type](zoneData);
        if(zoneTag){
            zoneTag.innerHTML = zoneComponent;
        }else{
            console.log('Can not found zonde Tag');
        }

    };

    var adComponents = {
        'banner': function (zoneData) {
            var protocol = (forceProtocol)?forceProtocol:location.protocol;
            var url = protocol + '//' + baseUrl + 'id=' + zoneData['zoneId'] + '&w=' + zoneData['w'] + '&h=' + zoneData['h'];

            // var style = 'max-width:'+zoneData['w']+'px;' +
            //             'max-width:'+zoneData['h']+'px;';
            return '<style>' +
                '#sabavision_zone_'+zoneData['id']+'{ width:'+zoneData['w']+'px; max-width:100%;}' +
                '.h_iframe-sabavision_responsive_ads_'+zoneData['w']+'x'+zoneData['h']+'{position:relative; } .h_iframe-sabavision_responsive_ads_'+zoneData['w']+'x'+zoneData['h']+' iframe {transform:none;position:absolute;top:0;left:0;width:100%; height:100%;} ' +
                '</style>' +
                '<div class="h_iframe-sabavision_responsive_ads_'+zoneData['w']+'x'+zoneData['h']+'">' +
                '<span style="display: block;padding-top:'+(zoneData['h']/zoneData['w']*100)+'%"></span>' +
                '<iframe src="'+url+'" frameborder="0" scrolling="No">Ads Error</iframe>' +
                '</div>';

            // return '<iframe style="'+style+'" src="'+url+'">Ads Error</iframe>';
        },
        'mobile_view_banner': function (zoneData) {
            if((navigator.userAgent.match(/Android/i)||navigator.userAgent.match(/webOS/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/BlackBerry/i)||navigator.userAgent.match(/Windows Phone/i))){
                var protocol = (forceProtocol)?forceProtocol:location.protocol;
                var url = protocol + '//' + baseUrl + 'id=' + zoneData['zoneId'] + '&w=' + zoneData['w'] + '&h=' + zoneData['h'];
                return '<style>' +
                    '#sabavision_zone_'+zoneData['id']+'{ position:fixed;bottom:0px;left:0px;width:100%;z-index: 999999999999999; }' +
                    '.h_iframe-sabavision_responsive_ads_'+zoneData['w']+'x'+zoneData['h']+'{position:relative; } .h_iframe-sabavision_responsive_ads_'+zoneData['w']+'x'+zoneData['h']+' iframe {transform:none;position:absolute;top:0;left:0;width:100%; height:100%;} ' +
                    '</style>' +
                    '<div class="h_iframe-sabavision_responsive_ads_'+zoneData['w']+'x'+zoneData['h']+'">' +
                        '<div onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);" style="background-color: red;z-index: 9999;position: absolute;font-size: 21px;right: 0px;top: -16px;border-radius: 100%;width: 27px;height: 21px;text-align: center;color: #fff;padding-bottom: 5px;font-family: Tahoma;">x</div>' +
                        '<span style="display: block;padding-top:'+(zoneData['h']/zoneData['w']*100)+'%"></span>' +
                        '<iframe src="'+url+'" scrolling="No">Ads Error</iframe>' +
                    '</div>';

            }else{
                return '';
            }
        },
        'pre-roll': function (zoneData) {
            var protocol = (forceProtocol)?forceProtocol:location.protocol;
            var url = protocol + '//' + baseUrl + 'id=' + zoneData['zoneId'] + '&w=' + zoneData['w'] + '&h=' + zoneData['h'] + '&type=pre-roll';

            var style = 'width:'+zoneData['w']+'px;' +
                'height:'+zoneData['h']+'px;';
            return '<iframe style="'+style+'" src="'+url+'">Ads Error</iframe>';
        }
    };


//   initialize
    for(var zoneI in zones){
        if(zones[zoneI]['source']){
            renderAds(zones[zoneI]);
        }
    }


//    Return
    return {
        renderAds: renderAds,
        queue: queue
    };

})();


window.sabavisionQueueInterval = setInterval(function(){
    var length = sabavisionPoxJs.queue.length;
    if(length){
        for(var i = 0; i<length; i++){
            var zone = sabavisionPoxJs.queue.splice(0,1);
            sabavisionPoxJs.renderAds(zone[0]);
        }
    }else{
        clearInterval(window.sabavisionQueueInterval);
    }
}, 500);
