/*
    XXX: REQUIREMENTS
        -- OOP.js
*/


var BaseAttachment = {
    /* 
     XXX: base class

       Represents the dictionary to be sent to FB when posting an product. It
       contains all pertinent/necessary info about the wall post (all visible
       copy, links, the product thumb, and the flash show (if applicable) ).

       XXX: Any method can be overridden in child classes for more specific 
       functionality/data.

        e.g.:

        {
            'name'   : // first line that appears next to thumb
            'caption': // appears below 'name'
            'href'   : // URL to view product on our site
            'properties' : // contains any extra copy (see _makeProperties() )
                    { 'section1':
                        { 'href': 'http://foo.com'
                          'text': 'blah blah'
                        },
                      'section2':
                        { 'href': 'http://bar.com'
                          'text': 'blar blar'
                        },
                    }
            'media'  : // all info about the thumb/flash (see _makeMedia() )
                    [{ 'attr': 'value',
                       'attr': 'value',
                       'attr': 'value',
                       ...
                       }]
        }
    */

    init: function(info) {
        this.info = info;
        this.finalProduct = {};

        // session
        this.ahost = info.ahost;
        this.khost = info.khost;
        this.source = info.source;

        // flash show
        this.facebookPlayable = info.facebookPlayable.toLowerCase() == 'true';
        this.expandedHeight = info.expandedHeight;
        this.expandedWidth = info.expandedWidth;
        this.scaledMovieURL = info.scaledMovieURL;

        // product
        this.prodNum = info.prodNum;
        this.thumbURL = info.thumbURL;
        this.prodTitle = info.prodTitle;
        this.pickupURL = undefined;
        this.media = undefined;
        this.pickupURL =undefined;

        // wall copy
        this.mainCaption = undefined;
        this.caption = undefined;
        this.description = undefined;
        this.properties = undefined;

        // miscellaneous
        this.fanURL = info.fanURL;
        this.productType = info.productType || 'eCard' // all sites handle 
                                                       // this word differently
    },

    make: function() {
        /* Do everything necessary to create the attachment (a dictionary)
           that will be passed to Facebook when posting a product
        */
        this.pickupURL = this._makePickupURL();
        var fp = this.finalProduct;

        fp.href = this.pickupURL;
        fp.name = this._makeMainCaption();
        fp.properties = this._makeProperties();
        fp.media = this._makeMedia();
        fp.caption = this.info.caption || 'Shared by {*actor*}';
        fp.description = this._makeDescription();

        return fp;
    },

    _makeMainCaption: function() {
        /* First line of copy (link) that appears to right of thumb
        */
        return this.info.mainCaption || '';
    },

    _makeDescription: function() {
        return this.info.description || '';
    },

    _makePickupURL: function() {
        /* The URL that the user will/can click on to view the product full size
           on our site (user leaves Facebook).
        */
        return this.info.pickupURL || '';
    },

    _makeProperties: function() {
        /* All sub-sections that appear in the 'properties' section
           will appear next to the posted product on a user's FB wall.
        */
        return this.info.properties || {};
    },

    _makeMedia: function() {
        /* A dictionary that holds all meta info about the product:
                - pickup url
                - thumb url
                - media type
                - flash show URL and dimensions
        */

        // postcard or not playable
        if (!this.facebookPlayable) {
            return [{
                'href': this.pickupURL,
                'src': this.thumbURL,
                'type': 'image'
            }]
        }
        // playable on FB wall (flash)
        else {
            return [{
                'href': this.pickupURL,
                'type': 'flash', 
                'swfsrc': this.scaledMovieURL,
                'imgsrc': this.thumbURL,
                'width': '100', 
                'height':'80', 
                'expanded_width': this.expandedWidth,
                'expanded_height': this.expandedHeight
            }]
        }
    }
};


var EcardAttachment = {
    /*
       XXX: Must extend BaseAttachment

       Specialized ecard attachment for CNP. 
       See docstring for BaseAttachment.
    */

    init: function(info) {
        this.parent(info);

        // XXX: pickup -- if pickupURL passed in, these aren't needed
        this.pickupPD = info.pickupPD;
        this.site = info.site;
        this.level50URL = info.level50URL || '';
        this.trimmedMemberNumber = info.trimmedMemberNumber;
        this.usageNumber = undefined;
    },

    _makePickupURL: function() {
        /* The URL to view the product full size on our site.
                -- use url passed in, or create.
        */
        if (this.info.pickupURL) {
            return this.info.pickupURL;
        }

        return this.level50URL + this.ahost + this.pickupPD + 
            '?p=' + this.prodNum + '&m=' + this.trimmedMemberNumber +
            '&i=' + this.usageNumber + '&source=' + this.source;
    },

    _makeProperties: function() {
        /* Extra copy to appear to right of thumb
                -- use dictionary passed in, or create.
        */
        if (this.info.properties) {
            return this.info.properties;
        }

        var ecardKey = 'Your ' + this.productType
        var properties = {}

        properties[ecardKey] = {
            'text': this.prodTitle,
            'href': this.pickupURL
        };

        if (this.fanURL) {
            properties['Become a fan'] = {
                'text': this.site.toLowerCase(),
                'href': this.fanURL 
            };
        }

        return properties;
    }
};


var CNPcardAttachment = {
    /* 
       Specialized ecard attachment for CNP. 
       See docstring for EcardAttachment.
    */
    init: function(info) {
        this.finalProduct = {};
        this.swf = info.swf;
        this.thumb_url = info.thumb_url;
        this.base_path = info.base_path;
        this.project_id = info.project_id;
        this.product_id = info.product_id;
        this.product_host = info.product_host;
        this.project_host = info.project_host;
        this.photo_host = info.photo_host;
        this.productType = info.product_type;
        this.site = info.site;
        this.properties = info.properties || undefined;
        this.fanURL = info.fanURL;
        this.usageURL = info.usageURL;
        this.homeURL = info.homeURL;
    },

    make: function() {
        /* Do everything necessary to create the attachment (a dictionary)
           that will be passed to Facebook when posting an ecard
        */
        var fp = this.finalProduct;
        fp.caption = 'Shared by {*actor*}';
        fp.name = this._makeName();
        fp.properties = this._makeProperties();
        fp.media = this._makeMedia();
        return fp;
    },

    _makeName: function() {
        /* First line of copy (link) that appears to right of thumb
        */
        return 'I posted a ' + this.productType + ' from ' + this.site + ' for you';
    },

    _makeProperties: function() {
        /* Extra copy to appear to right of thumb
                -- use dictionary passed in, or create.
        */
            
        if (this.properties != undefined) {
            return this.properties;
        }

        var ecardKey = 'Your ' + this.productType
        var properties = {}

        if (this.fanURL) {
            properties['Become a fan'] = {
                'text': this.site.toLowerCase(),
                'href': this.fanURL 
            };
        }

        return properties;
    },

    _makeMedia: function() {
        return [{
                    'type': 'flash', 
                    'fanURL': this.fanURL,
                    'swfsrc': this._buildShell(),
                    'imgsrc': this.thumb_url,
                    'width': '167', 
                    'height': '148', 
                    'expanded_width': '460', 
                    'expanded_height': '460'}
                ]
    },

    _random: function() {
        return Math.floor(Math.random()*(1+500-1))+1;
    },

    _buildShell: function() {
        return this.base_path + this.swf
               + "?&projectID="  +  escape(this.project_id)
               + "&productID="   +  escape(this.product_id)
               + "&productHost=" +  escape(this.product_host)
               + "&projectHost=" +  escape(this.project_host)
               + "&basePath="    +  escape(this.base_path)
               + "&photoHost="   +  escape(this.photo_host)
               + "&cache=" + this._random();
    }
}

var JLEcardAttachment = {
    /* 
       XXX: Must extend EcardAttachment

       Specialized ecard attachment for JL. 
       See docstring for EcardAttachment.
    */

    init: function(info) {
        this.parent(info);
    },

    _makePickupURL: function() {
        return this.ahost + this.pickupPD + 
            '?code=' + this.usageNumber + this.trimmedMemberNumber +
            '&source=' + this.source + '&cont=2';
    }
};

