var oscWidget = '<div id="oscMain"><span id="oscStandby">Please activate Javascript.</span><script type="text/javascript">var oscBaseURL = "http://195.85.222.91/osc"; var uKey = "b22d0c6bc44774e6436f85278f6f18ea";</script><script src="http://195.85.222.91/osc/res/osc/js/storageCalculator.js" type="text/javascript"></script></div>';
var oscPromoCodeFormSendIsBound = false;
var sessionId = false;

function showPromotionDialog()
{
    resetPromotionDialog();

    $('#oscPromotionDialog').dialog({
            modal: true, 
            draggable: false, 
            resizable: false, 
            width: 200,
            height: 100,
            dialogClass: 'oscPromoDialog',
            beforeClose: resetPromotionDialog
    });

    $('.ui-widget-overlay').css({opacity: 0.9});

    var pos = $('.oscPromoDialog').position();
    var newLeft = pos.left - 85;
    var newTop = pos.top - 72;

    $('#oscPromotionDialog').css({display: 'none'});
    $('#oscPromotionDialog').css({height: 'auto'});
    $('.oscPromoDialog').animate({height: 245, top: newTop}, 500)
                                            .animate({width: 370, left: newLeft}, 300, function(){
                                                    $('#oscPromotionDialog').fadeIn();
                                            });

    if(!oscPromoCodeFormSendIsBound) {
            $('#oscPromotionCodeForm').submit(sendPromotionCode);
            oscPromoCodeFormSendIsBound = true;
    }

    $(window).resize(function() {
            recenterPromotionDialog();
    });
}

function sendPromotionCode(event)
{
    event.preventDefault();
    $('#oscPromotionDialog .oscError').remove();

    var requestUrl = $('#oscPromotionCodeForm').attr('action');

    jQuery.ajax({
        dataType: 'jsonp',
        jsonp: 'jsonp_callback',
        url: requestUrl,
        data: $('#oscPromotionCodeForm').serialize(),
        success: function(data){
            if('success' == data.status)
            {
                $('#oscPromotionDialogContent').hide();
                $('#oscPromotionDialog').append(oscWidget);

                if('undefined' != typeof data.sessionId)
                {
                        sessionId = data.sessionId;
                }

                $("#oscPromotionDialog").dialog("option", "height", "720");
                $(".oscPromoDialog").css({width: 370, height: 750});
                recenterPromotionDialog();
            }
            else
            {
                $('#oscPromotionCodeForm').after('<span class="oscError">'+data.message+'</span>');
            }
        }
    });
}

function resetPromotionDialog()
{
    $('#oscAccessCode').val('');
    $('#oscPromotionDialog .oscError').remove();
    $("#oscPromotionDialog").dialog("option", "height", "220");
    $('#oscPromotionDialogContent').show();
    $('#oscMain').remove();
}

function recenterPromotionDialog()
{
    $("#oscPromotionDialog").dialog("option", "position", "center");
}
