if (!TrekkSoft) {
    var TrekkSoft = {};
}

TrekkSoft.getBaseUrl = function() {
    return 'http://newpraguetours.trekksoft.com/en';
};


// ---


TrekkSoft.Embed = {};

TrekkSoft.Embed.registerOnLoadCallback = function(func)
{
    if (window.addEventListener) {
        window.addEventListener('load', func, false);
    } else if (window.attachEvent) {
        window.attachEvent('onload', func);
    } else {
        window.onload = func;
    }
};


// ---


TrekkSoft.Embed.Iframe = function(userAttribs)
{
    this.rendered = false;

    this.attribs = {
        src         : TrekkSoft.getBaseUrl(),
        entryPoint  : 'home',
        tourId      : null,
        url         : null,
        referral    : null,
        width       : '700px',
        height      : '500px',
        frameborder : 0,
        scrolling   : 0
    };

    if (typeof userAttribs == 'object') {
        for (var key in this.attribs) {
            if (!userAttribs[key]) {
                continue;
            }
            
            this.setAttrib(key, userAttribs[key]);
        }
    }
};

TrekkSoft.Embed.Iframe.prototype.setAttrib = function(key, value)
{
    this.attribs[key] = value;

    // Backward compatibility
    if (key == 'tourId') {
        this.setAttrib('entryPoint', 'tour');
    }
    
    return this;
};

TrekkSoft.Embed.Iframe.prototype.render = function(elementId)
{
    var thiz = this;

    TrekkSoft.Embed.registerOnLoadCallback(function() {
        if (thiz.rendered) {
            return;
        }

        var e = document.getElementById(elementId.replace('#', ''));
        if (!e) {
            return;
        }

        if ('tour' == thiz.attribs.entryPoint) {
            thiz.attribs.src = TrekkSoft.getBaseUrl() + '/order/step1/tourId/' + thiz.attribs.tourId;
        } else if ('url' == thiz.attribs.entryPoint) {
            thiz.attribs.src = thiz.attribs.url;
        } else if ('shop' == thiz.attribs.entryPoint) {
            thiz.attribs.src = TrekkSoft.getBaseUrl() + '/shop';
        }

        thiz.attribs.src += '?_embed=1';

        if (thiz.attribs.referral) {
            thiz.attribs.src += '&agentReferral=' + thiz.attribs.referral;
        }

        var n = document.createElement('iframe');
        n.src         = thiz.attribs.src;
        n.width       = thiz.attribs.width;
        n.height      = thiz.attribs.height;
        n.frameBorder = thiz.attribs.frameborder;
        //n.scrolling   = thiz.attribs.scrolling;

        while (e.hasChildNodes()){
            e.removeChild(holder.lastChild);
        }
        e.appendChild(n);

        thiz.rendered = true;
    });
};


// ---


TrekkSoft.Embed.Button = function(attribs)
{
    this.rendered = false;
    
    this.attribs = {
        url         : TrekkSoft.getBaseUrl(),
        entryPoint  : 'home',
        tourId      : null,
        referral    : null,
        width       : 700,
        height      : 500
    };

    if (typeof attribs == 'object') {
        for (var key in this.attribs) {
            if (!attribs[key]) {
                continue;
            }

            this.setAttrib(key, attribs[key]);
        }
    }
};

TrekkSoft.Embed.Button.prototype.setAttrib = function(key, value)
{
    this.attribs[key] = value;

    // Backward compatibility
    if (key == 'tourId') {
        this.setAttrib('entryPoint', 'tour');
    }
    
    return this;
};

TrekkSoft.Embed.Button.prototype.registerOnClick = function(elementId, type)
{
    var e = document.getElementById(elementId.replace('#', ''));
    if (!e) {
        return;
    }

    var thiz = this;

    TrekkSoft.Embed.registerOnLoadCallback(function() {
        if (thiz.rendered) {
            return;
        }

        var func = function(evt) {
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false;
            }
            
            thiz.open(type);
        };

        if (e.addEventListener) {
            e.addEventListener('click', func, false);
        } else if (e.attachEvent) {
            e.attachEvent('onclick', func);
        } else {
            window.onload = func;
        }
        
        thiz.rendered = true;
    });

    return this;
};

TrekkSoft.Embed.Button.prototype.open = function()
{
    var url = this.attribs.url;

    if ('tour' == this.attribs.entryPoint) {
        url = TrekkSoft.getBaseUrl() + '/order/step1/tourId/' + this.attribs.tourId;
    } else if ('shop' == this.attribs.entryPoint) {
        url = TrekkSoft.getBaseUrl() + '/shop';
    }

    url += '?_embed=1';

    if (this.attribs.referral) {
        url += '&agentReferral=' + this.attribs.referral;
    }
    
    switch (this.attribs.target)
    {
        case 'popup':
            var opts = 'width=' + parseInt(this.attribs.width) + ',height=' + parseInt(this.attribs.height) + ',menubar=no,toolbar=no,location=no,scrollbars=yes,status=no';
            return window.open(url, 'trekksoftpopup', opts);

        case 'self':
            return window.location = url;

        default:
            return window.open(url);
    }
    

};
