﻿(function() {

    window.ad = {

        COOKIE_NAME : 'live-tracker.prevent-ad-n',

        newAvailsLink : null,

        updateNewAvailsLink : function() {
            var self = this;

            $('a[target=_blank]').each(function() {
                var
                link = $(this),
                href = link.attr('href');
                
                if (!href || (href.indexOf('click.newavails.ru') == -1)) return;
                self.newAvailsLink = link.attr('href');
            });
        },

        getNewAvailsLink : function() {
            if (this.newAvailsLink == null) {
                this.updateNewAvailsLink();                            
            }
        
            return this.newAvailsLink;
        },
        
        show : function() {
            if (window.ad.cookie.get(this.COOKIE_NAME)) return;
            
            var adOpened = false;
            
            if (this.getNewAvailsLink() != null) {
                window.open(this.getNewAvailsLink());
                adOpened = true;
            }

            if (adOpened) window.ad.cookie.set(this.COOKIE_NAME);
        }
    
    };
    
    window.ad.ip = {
    
        get : function() {
            return $('.user-ip').text();
        }
    
    };
    
    window.ad.cookie = {
    
        EXPIRES_AFTER_IN_MILLISECONS : 3 * 60 * 60 * 1000,

        get : function(cookieName) {
            var cookiesString = document.cookie, cookiesMap = { };
            
            if ((typeof cookiesString != 'string') || (cookiesString.length == 0)) return null;
            
            var cookiesArray = cookiesString.split(/;\s/g);
            
            for (var i = 0; i < cookiesArray.length; ++i) {
                var currentCookie = cookiesArray[i].split('=');
                
                if (currentCookie.length == 2) {
                    cookiesMap[decodeURIComponent(currentCookie[0])] = decodeURIComponent(currentCookie[1]);
                }
            }
            
            var
            storedId = cookiesMap[cookieName],
            cookieExists = (typeof storedId != 'undefined');

            if (!cookieExists) return false;
            
            return (storedId == window.ad.ip.get());
        },

        set : function(cookieName) {
            var
            expires = new Date((new Date()).getTime() + this.EXPIRES_AFTER_IN_MILLISECONS),
            cookieString = encodeURIComponent(cookieName) + "=" + encodeURIComponent(window.ad.ip.get())
                + "; expires=" + expires.toGMTString() + "; path=/; domain=.live-tracker.ru";

            document.cookie = cookieString;
        }
    
    };
    
    window.ad.google = {
    
        COOKIE_NAME : 'live-tracker.prevent-ad-g',

        _showPopupElement : function(id) {
            var newFrame = null;
        
            $('iframe').each(function() {
                if ((this.name == 'google_ads_frame') && (this.width == 728)) {
                    newFrame = $(this).clone();
                }
            });

            if (newFrame == null) return false;
            
            newFrame.css('position', 'relative');
            
            var background = $('.modal-window-background');
            var adContent = $('.ad-content');
            
            adContent.children().remove();
            adContent.append(newFrame);

            var self = this;

            newFrame.load(function() {
                adContent.prepend('<div style="color: white; font-size: 2.5em; margin-bottom: 1em;">Щелкните на рекламе для продолжения</div>');
                adContent.show();

                setTimeout(function() {
                    window.ad.cookie.set(self.COOKIE_NAME);
                    showspoiler(id);
            
                    adContent.hide();
                    background.hide();
                    newFrame.remove();
                }, 10000);
            });
            
            background.show();
        },

        run : function(id) {
            if (window.ad.cookie.get(this.COOKIE_NAME)) {
                return;
            }
            
            if (this._showPopupElement(id) === false) {
                return;
            }

            return true;
        }
    
    };
    
})();
