﻿var Page = Class.create({

    defaultText: {
        name: 'Full Name',
        email: 'Email Address',
        mobile: 'Mobile Number'
    },

    initialize: function() {
        document.observe("dom:loaded", this.domLoaded.bindAsEventListener(this));
    },

    domLoaded: function() {
        this.injectFlash();
        this.initializeDefaultText();
        this.hijackLinks();
        this.initializeCelebrityPreview();
        this.showThankyou();
    },

    injectFlash: function() {

        var userId = $F($('infoUserId'));

        var flashVars = {
            serviceURL: '/Api/GetUserAnswers/' + userId,
            swfsDirectory: '/Flash/'
        };

        var flashParams = {
            wmode: "opaque",
            AllowScriptAccess: "always",
            AllowFullScreen: "true",
            AllowNetworking: "allownetworking"
        };

        var objectAttributes = {
            id: "helix"
        };

        swfobject.embedSWF(
                '/Flash/Main.swf',
                'noflash',
                600,
                400,
                '9',
                false,
                flashVars,
                flashParams,
                objectAttributes);

        swfobject.embedSWF(
                '/Flash/prizeImage_small.swf',
                'prizePic',
                91,
                91,
                '9',
                false,
                {},
                { wmode: 'transparent' },
                {});

        swfobject.embedSWF(
                '/Flash/videoIllustration.swf',
                'videoIllustration',
                89,
                54,
                '9',
                false,
                {},
                { wmode: 'transparent' },
                {});
    },

    initializeDefaultText: function() {

        new Bauer.Controls.DefaultText({ element: $('fullName'), defaultText: this.defaultText.name });
        new Bauer.Controls.DefaultText({ element: $('email'), defaultText: this.defaultText.email });
        new Bauer.Controls.DefaultText({ element: $('mobile'), defaultText: this.defaultText.mobile });
    },

    hijackLinks: function() {

        var competition = $('competition');

        if (competition) {
            competition.down('button').observe('click', this.handleCompetitionClick.bindAsEventListener(this));
        }

        var share = $('shareSocial');

        if (share) {
            share.down('a').observe('click', this.handleSocialClick.bindAsEventListener(this));
        }

        var offer = $('offerLink');

        if (offer) {
            offer.down('a').observe('click', this.handleOfferClick.bindAsEventListener(this));
        }
    },

    initializeCelebrityPreview: function() {

        new Bauer.Controls.PreviewBox({

            previewItems: '#celebs img',

            getCallbackUrl: function(item) {
                return '/Ajax/CelebrityPreview';
            },

            getCallbackParams: function(item) {

                return {
                    celebrityId: $F(item.up('li').down('input'))
                };
            }
        });
    },

    handleCompetitionClick: function(event) {

        event.stop();

        var params = {
            name: $F($('fullName')),
            email: $F($('email')),
            mobile: $F($('mobile'))
        };

        if (params.name == this.defaultText.name) {
            params.name = '';
        }

        if (params.email == this.defaultText.email) {
            params.email = '';
        }

        if (params.mobile == this.defaultText.mobile) {
            params.mobile = '';
        }

        new Ajax.Request('/Ajax/EnterCompetition', {
            method: 'post',
            parameters: params,
            onSuccess: function(transport) {

                var result = transport.responseText.evalJSON();
                var html = '<h4>' + result.Heading + '</h4><p>' + result.MainText + '</p>';

                if (result.Tracker) {
                    html += result.Tracker;
                }

                new Bauer.Controls.Dialog({
                    parentElement: $('mainCol'),
                    html: html
                });

                if (result.Success) {
                    var form = $('competition').down('form');
                    form.insert({ after: '<p><strong>You have been entered into the competition</strong></p>' });
                    form.hide();
                }
            }
        });
    },

    handleSocialClick: function(event) {

        event.stop();

        new Ajax.Request('/Ajax/SocialNetwork', {
            method: 'get',
            parameters: {},
            onSuccess: function(transport) {

                new Bauer.Controls.Dialog({
                    parentElement: $('mainCol'),
                    html: transport.responseText
                });
            }
        });
    },

    handleOfferClick: function(event) {

        event.stop();

        var link = event.element().up('a');
        var href = link.getAttribute('href');

        window.open(href);
    },

    showThankyou: function() {

        var thanks = $F($('thankyou'));

        if (thanks == 'true') {

            new Ajax.Request('/Ajax/Thankyou', {
                method: 'get',
                parameters: {},
                onSuccess: function(transport) {

                    new Bauer.Controls.Dialog({
                        parentElement: $('mainCol'),
                        html: transport.responseText
                    });
                }
            });            
        }
    }
});

new Page();
