﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, cc_resx_TR_Payment_Success, cc_resx_TR_Bad_or_missing_product_code, 
cc_resx_TR_AUTH_AJAX_ERROR_RETRY, cc_resx_TR_AUTH_AJAX_ERROR_REFRESH, cc_resx_TR_Redirecting, 
cc_resx_TR_AUTH_AJAX_ERROR
 */

//  
//  Author: Kevin Burkitt
//  Date:   2010-04-13
//  
//  Star Overlay Management
//
cc.stars = function() {

    return {
        buyInProgress: false,
        buyInProcess: false,
        buyError: false,
        signupRedirect: false,

        init: function(target) {

            jQuery('.cc-award-token-platinum-girloftheweek', target).unbind('click').click(function() {
                cc.stars.showGirlOfTheWeekStarsOverlay('1');
                return false;
            });
            
            jQuery('.cc-award-token-gold-girloftheweek', target).unbind('click').click(function() {
                cc.stars.showGirlOfTheWeekStarsOverlay('3');
                return false;
            });


            jQuery('.cc-livechat-buy-overlay', target).unbind('click').click(function() {
            cc.stars.showGirlOfTheWeekStarsOverlay('3');
            return false;
            });


            jQuery('.cc-award-token-platinum', target).unbind('click').click(function() {
                cc.stars.showStarsOverlay('1');
                return false;
            });
            jQuery('.cc-award-token-gold', target).unbind('click').click(function() {
                cc.stars.showStarsOverlay('3');
                return false;
            });
            jQuery('.cc-award-token-silver', target).unbind('click').click(function() {
                cc.stars.showStarsOverlay('5');
                return false;
            });

            jQuery('.cc-buy-tokens', target).unbind('click').click(function() {
                cc.stars.showBuyStarsOverlay();
                return false;
            });
        },

        showStarsOverlay: function(type) {
            var mediaid = jQuery('#wr-media-id').val();
            cc.ui.colorbox("/stars/award-overlay/" + type + "/" + mediaid, function() {
                // Reload Stars... 
                cc.stars.reloadStarsPanel();
            });
        },

        reloadStarsPanel: function() {

            cc.ajax.load('#wr-award-stars', '/watch/stars/show/', null, false, true);
        },

        showBuyStarsOverlay: function() {
            cc.ui.colorbox("/stars/buy-overlay/", function() {
                jQuery('.cc-payments', '#stars-buy-overlay').unbind('click').click(function() {
                    var product = jQuery(this).attr("cc:product");

                    cc.stars.showOverlayPleaseWait();

                    var result = cc.stars.buy(product);
                });
            });
        },




        showGirlOfTheWeekStarsOverlay: function(type, mediaid) {
            //mediaid = 'F6AAF3F1-440D-4306-86C7-011CB229CA18';
            jQuery('#wr-media-id').val(mediaid);
            cc.ui.colorbox("/stars/award-overlay/" + type + "/" + mediaid, function() {
                // Reload Stars...
                cc.stars.reloadGirlOfTheWeekStarsPanel();
            });
        },

        reloadGirlOfTheWeekStarsPanel: function() {

            cc.ajax.load('#cc-winner-topcontent', '/girl-of-the-week-topcontent/');
        },




        buy: function(product) {

            if (typeof cc.stars.buyInProgress !== 'undefined' && cc.stars.buyInProgress === true) {
                return false;
            }
            cc.stars.buyInProgress = true;

            var p = product;
            var page = cc.helpers.encodeQsParam(document.location.href);
            var url = '/payments/bt-easy-collect/buy/' + p + '/?u=' + page;

            // cc.log('Attempting to buy \'' + p + '\' on page: ' + page + '');

            cc.stars.updateOverlayResponseMsg('');

            cc.ajax.getJSON(url, {}, function(resp, textStatus) {
                try {
                    if (resp.success !== true) {
                        // Some Server Failure or Bad Id Encountered In Processing (bad; shouldn't happen)
                        cc.stars.updateOverlayResponseMsg(resp.message);
                        // Hide Please Wait Overlay
                        cc.stars.hideOverlayPleaseWait();
                    }
                    else {
                        // Processed ok
                        if (typeof resp.submitForm !== 'undefined' && resp.submitForm !== '') {
                            cc.stars.updateOverlayResponseMsg(cc_resx_TR_Redirecting);

                            cc.stars.buyInProcess = false;
                            cc.stars.buyError = false;
                            cc.stars.epochOverlayTransactionStart(jQuery(resp.submitForm));
                        }
                        else {
                            if (resp.redirect !== '') {
                                // redirect user to C&B for authorisation
                                // cc.stars.hideOverlayPleaseWait();
                                cc.stars.updateOverlayResponseMsg(cc_resx_TR_Redirecting);

                                location.href = resp.redirect;

                                return;
                            }
                            else {
                                // Here = user already authorised & valid C&B user:

                                // Payment Has Been Attempted! 

                                // if success, reload will be probably be set 
                                if (resp.reload === true) {
                                    jQuery('.sob-wait p').html(cc_resx_TR_Payment_Success);

                                    if (typeof resp.ecTransactionId !== 'undefined') {
                                        cc.tracking.trackEcommerce(
                                            resp.ecTransactionId,
                                            resp.ecProductType,
                                            resp.ecProductDescription,
                                            resp.ecProductCategory,
                                            resp.ecShop,
                                            resp.ecProductValue,
                                            resp.ecCity,
                                            resp.ecState,
                                            resp.ecCountry,
                                            function() {
                                                location.reload(true);
                                            }
                                        );

                                    } else {
                                        location.reload(true);
                                    }

                                }
                                else {
                                    // Or just display resp.message 
                                    cc.stars.updateOverlayResponseMsg(resp.message);
                                    // Hide Please Wait Overlay
                                    cc.stars.hideOverlayPleaseWait();
                                }
                            }
                        }
                    }
                }
                catch (e) {
                    cc.stars.updateOverlayResponseMsg(cc_resx_TR_AUTH_AJAX_ERROR);

                    // Hide Please Wait Overlay
                    cc.stars.hideOverlayPleaseWait();
                }

            },
            function() {
                cc.stars.buyInProgress = false;
            });
        },

        /*
        buy: function(product) {

            if (typeof cc.stars.buyInProgress != 'undefined' && cc.stars.buyInProgress == true) {
        return false;
        }
        cc.stars.buyInProgress = true;

            var p = product;
        var page = cc.helpers.encodeQsParam(document.location.href);
        var url = '/payments/bt-easy-collect/buy/' + p + '/?u=' + page;

            cc.log('Attempting to buy \'' + p + '\' on page: ' + page + '');

            cc.stars.updateOverlayResponseMsg('');

            cc.ajax.getJSON(url, {}, function(resp, textStatus) {
        try {
        if (resp.success !== true) {
        // Some Server Failure or Bad Id Encountered In Processing (bad; shouldn't happen)
        cc.stars.updateOverlayResponseMsg(resp.message);
        // Hide Please Wait Overlay
        cc.stars.hideOverlayPleaseWait();
        }
        else {
        // Processed ok
        if (resp.redirect !== '') {
        // redirect user to C&B for authorisation
        // cc.stars.hideOverlayPleaseWait();
        cc.stars.updateOverlayResponseMsg(cc_resx_TR_Redirecting);
        location.href = resp.redirect;
        return;
        }
        else {
        // Here = user already authorised & valid C&B user:

                            // Payment Has Been Attempted! 

                            // if success, reload will be probably be set 
        if (resp.reload === true) {
        jQuery('.sob-wait p').html(cc_resx_TR_Payment_Success);
        location.reload(true);
        }
        else {
        // Or just display resp.message 
        cc.stars.updateOverlayResponseMsg(resp.message);
        // Hide Please Wait Overlay
        cc.stars.hideOverlayPleaseWait();
        }
        }
        }
        }
        catch (e) {
        cc.stars.updateOverlayResponseMsg(cc_resx_TR_AUTH_AJAX_ERROR);

                    // Hide Please Wait Overlay
        cc.stars.hideOverlayPleaseWait();
        }

            },
        function() {
        cc.stars.buyInProgress = false;
        });
        },
        */

        showOverlayPleaseWait: function() {
            jQuery('.sob-wait-bg, .sob-wait').show();
        },

        hideOverlayPleaseWait: function() {
            jQuery('.sob-wait-bg, .sob-wait').hide();
        },

        updateOverlayResponseMsg: function(msg) {
            jQuery('#stars-buy-overlay .buy-error').html(msg);
        },

        epochOverlayTransactionStart: function(form) {
            if (typeof cc.stars.buyInProcess !== 'undefined' && cc.stars.buyInProcess === true) {
                return false;
            }
            cc.stars.buyInProcess = true;
            cc.stars.buyError = false;

            try {

                var f = form;
                var transactionId = jQuery('.x_transactionid', f).val();
                var pi_code = jQuery('.pi_code', f).val();

                if (typeof pi_code === 'undefined' || pi_code === '') {
                    cc.stars.updateOverlayResponseMsg(cc_resx_TR_Bad_or_missing_product_code);
                    return false;
                }

                if (typeof transactionId !== 'undefined' && transactionId !== '') {
                    return true;

                } else {

                    var page = cc.helpers.encodeQsParam(document.location.href);
                    var url = '/payments/epoch/start/' + pi_code + '/';

                    // cc.log('Attempting to buy \'' + pi_code + '\' on page: ' + page + '');

                    // Redirecting you to our Payment Provider
                    cc.stars.showOverlayPleaseWait();

                    cc.ajax.getJSON(url, {}, function(resp, textStatus) {
                        try {
                            if (resp.success !== true) {
                                // Some Server Failure or Bad Id Encountered In Processing (bad; shouldn't happen)
                                cc.stars.updateOverlayResponseMsg(resp.message);
                                cc.stars.buyError = true;
                            }
                            else {
                                // Processed ok
                                if (resp.message !== '') {
                                    // We have transaction id:

                                    jQuery('.x_transactionid', f).val(resp.message);
                                    jQuery('.x_url', f).val(location.href);
                                    cc.stars.showOverlayPleaseWait();
                                    cc.stars.buyInProcess = false;
                                    cc.stars.signupRedirect = true;
                                    jQuery(f).submit();
                                }
                                else {
                                    // Or just display resp.message
                                    cc.stars.updateOverlayResponseMsg(cc_resx_TR_AUTH_AJAX_ERROR_RETRY);
                                    cc.stars.buyError = true;
                                }
                            }

                            return false;
                        }
                        catch (e) {
                            cc.stars.updateOverlayResponseMsg(cc_resx_TR_AUTH_AJAX_ERROR);
                            cc.stars.buyError = true;
                        }
                    },
                    function() {
                        cc.stars.buyInProcess = false;

                        if (cc.stars.buyError === false && cc.stars.signupRedirect === false) {
                            // Hide Please Wait Overlay
                            cc.stars.hideOverlayPleaseWait();
                            cc.stars.buyError = false;
                        }
                    });
                }
            }
            catch (e) {
                cc.stars.updateOverlayResponseMsg(cc_resx_TR_AUTH_AJAX_ERROR_REFRESH);
                cc.stars.buyInProcess = false;
            }

            return false;
        }

    };

} ();

