Event.observe(document, 'dom:loaded', _ous_init);
Event.observe(document, 'dom:loaded', _popups_init);

function _ous_init() {
    try {
        replaceContent(window.external.GetGameTitle(), $$('.gameTitle'));
    } catch(err) {
        console.log(err);
    }
    try {
        $('continue').writeAttribute('href', window.external.GetOusUrl());
        $('buyPrice').writeAttribute('href', window.external.GetOusUrl());
        $('connectNow').writeAttribute('href', window.external.GetOusUrl());
    } catch(err) {
        console.log(err);
    }
    try {
        if (window.external.CheckOnlineStatus() == 0) {
            showObj($$('.offline'));
            showObj($('alert'));
        }
    } catch(err) {
        console.log(err);
    }
    try {
        if (window.external.GetTrialTimeLeft() <= 0) {
            showObj($('expired'));
            showObj($('alert'));
        } else if (window.external.GetTrialTimeLeft() > 0) {
            showObj($('expiring'));
            showObj($('alert'));
        }
    } catch(err) {
        console.log(err);
    }
    replaceContent(GetGamePriceDollars(), $$('.gamePrice'));

    // Set default values for plan info
    if (jsObj = $('plan-cost')) jsObj.innerHTML = '$7.99';
    if (jsObj = $('num-of-games')) jsObj.innerHTML = 'six';

    if (jsObj = $$('a')) {
        jsObj.each(function(e) {
            e.observe('click',
            function(event) {
                if (e.readAttribute('href').length > 1) {
                    var noIcoins = $('noicoins');
                    if (!noIcoins.hasClassName('on')) {
                        showObj('overlay');
                        showObj('loading');
                        window.location = e.readAttribute('href');
                    }
                } else {
                    Event.stop(event);
                }
            });
        })
    }

    $$('#plans label').each(function(planLabels) {
        $(planLabels).observe('click',
        function(event) {
            plans(planLabels);
        })
    })

    /* for checkbox */
    $('none').observe('click',
    function(event) {
        plans($('none'));
    })
    $('none').checked = false; // if user selected this before, browser save it as default, so removing it
    if (jsObj = $('continue')) {
        jsObj.observe('click',
        function(event) {
            var noIcoins = $('noicoins');
            if (noIcoins && noIcoins.hasClassName('on')) {
                showObj('overlay');
                showObj('mypopup');
                Event.stop(event);
            }
            Event.stop(event);
        });
    }

    if (jsObj = $('buyNow')) {
        jsObj.observe('click',
        function(event) {
            purchaseItem();
            Event.stop(event);
        });
    }

};

function plans(planLabel) {
    var planLabels = $$('#plans label');
    var planLabel = $(planLabel);
    var none = $('none');
    if (planLabels) {
        planLabels.each(function(planLabel) {
            planLabel.removeClassName('on');
        }); // Remove visual selection from all plans
        if (planLabel != none) {
            planLabel.previous('input').checked = true; // Select the radio (hidden) (This is needed for IE, which for some reason doesn't accept the label)
        }
        if (planLabel != none) {
            planLabel.addClassName('on'); // Mark the clicked plan as active
        } else {
            $('noicoins').addClassName('on');
        }
        if (planLabel.previous('input') == $('plan1')) {
            if (jsObj = $('plan-cost')) jsObj.innerHTML = '$6.99';
            if (jsObj = $('num-of-games')) jsObj.innerHTML = 'twelve';
            $$('#plan-description p').invoke('removeClassName', 'hideObj');
        }
        else if (planLabel.previous('input') == $('plan2')) {
            if (jsObj = $('plan-cost')) jsObj.innerHTML = '$7.99';
            if (jsObj = $('num-of-games')) jsObj.innerHTML = 'six';
            $$('#plan-description p').invoke('removeClassName', 'hideObj');
        }
        else if (planLabel.previous('input') == $('plan3')) {
            if (jsObj = $('plan-cost')) jsObj.innerHTML = '$9.99';
            $$('#plan-description p').invoke('removeClassName', 'hideObj');
            if (jsObj = $$('#plan-description p')[0]) jsObj.addClassName('hideObj');
        }
        else if (planLabel == none || planLabel == $('noicoins')) {
            $$('#plan-description p').invoke('addClassName', 'hideObj');
        }
    }
};

function _popups_init() {
    var jsObj = $('tooltip');
    if (jsObj) {
        jsObj.observe('click',
        function(event) {
            togglePopup('mytooltip');
            Event.stop(event);
        });
    }

    if ((jsObj = $$('#mypopup a')).length > 0) {
        jsObj[0].observe('click',
        function(event) {
            hideObj('overlay');
            hideObj('mypopup');
            var plan1 = $('plan1');
            if (plan1) {
                plan1.checked = true;
                $('noicoins').removeClassName('on');
                $('plan1Label').addClassName('on');
                $('plan-cost').innerHTML = '$7.99';
                $('num-of-games').innerHTML = 'six';
                $$('#plan-description p').invoke('removeClassName', 'hideObj');
            }
            Event.stop(event);
        });
    }

    if (jsObj = $('close')) {
        jsObj.observe('click',
        function(event) {
            hideObj('mytooltip');
            Event.stop(event);
        });
    }
    $(document).observe('click',
    function() {
        hideObj('mytooltip');
    });
}

function showObj(elem) {
    if (typeof(elem) === 'string') {
        var jsObj = $(elem);
    } else {
        var jsObj = elem;
    }
    if (jsObj) {
        if (jsObj.length > 1) {
            jsObj.each(function(s) {
                s.removeClassName('hideObj');
            });
        } else {
            jsObj.removeClassName('hideObj');
        }
    }
}

function hideObj(elem) {
    var jsObj = $(elem);
    if (jsObj) {
        jsObj.addClassName('hideObj');
    }
};

function togglePopup(elem) {
    if ($(elem).hasClassName('hideObj')) {
        showObj(elem);
    }
    else {
        hideObj(elem);
    };
};

function replaceContent(content, element) {
    if (content) {
        try {
            if (element) {
                if (element.length > 1) {
                    element.each(function(s) {
                        s.innerHTML = content;
                    });
                } else {
                    element.innerHTML = content;
                }
            }
        } catch(err) {
            console.log(err);
        }
    }
}

function GetGamePriceDollars() {
    try {
        var cents = window.external.GetGamePriceCents();
        var dollars = new Number(cents / 100);
        return dollars.toFixed(2);
    } catch(err) {
        console.log(err);
    }
}