var PostNewItem   = new Object();
var Functions   = new Object();

//----------------------------------------------------------------//
//** extending the master Util class
var BasketLocal = {

};

var Basket = $.extend(Basket, BasketLocal);

//=============================================================//
//** extending the master Util class
var Util = $.extend(Util, {
    // alert: function(text) {
    //     Util.initDialog();
    //     $('#dialog').html(text);

    //     var xButtons = {};
    //     xButtons['OK'] = function() {
    //         $(this).dialog('close');
    //         $(this).dialog('destroy');
    //     };

    //     $('#dialog').dialog(
    //         $.extend(Util.dialogDefaults, {
    //             height: 100,
    //             width: 350,
    //             buttons: xButtons
    //         })
    //     );
    // },
    
    setUpForm: function(formName, extraPar){
        $(function() {
            var options = {
                success: function(json, statusText, jqFormObj) {
                    Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                    Util.hideProgressInd();
                },
                beforeSubmit: function() {
                    Util.showProgressInd();
                },
                dataType: 'json'
            };
            $('#' + formName).ajaxForm(options);
        });
    }

});

//=====================================================//
var Product = {
    emailToFriendForm: function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if (url == '' || url == 'javascript:void(0)'){
            url = $(this).attr('link');
        }

        Util.showProgressInd();
        var xButtons = {};

        xButtons['Send'] = function() {
            $('#emailToFriendForm').submit();
        };

        xButtons['Cancel'] = function() {
            $(this).dialog('close').dialog('destroy');
        };

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 500,
                    height: 440,
                    title: 'Forward to Friend',
                    buttons: xButtons
                })
            );
            $('#dialog').html(data);
            Util.hideProgressInd();
        });
    },
    
    emailToFriendCallback: function(json) {
        Util.alert(json.returnText, function() {
            $('#dialog').dialog('close');
            $('#dialog').dialog('destroy');
            Util.hideProgressInd();
        });
    },

    setupJqZoom: function() {
        $('.product .detailCont .pictureContainer img').each(function(i){
            var imgUrl = $(this).attr('src');
            imgUrl = imgUrl.replace(/normal\//, "large/");
            $(this).parent().attr('href', imgUrl);
        });

        var options = {
            zoomWidth: 250,
            zoomHeight: 250,
            xOffset: 10,
            yOffset: 0,
            position: 'left',
            title: false
        };
        $('.product .detailCont .pictureContainer a.zoomImage').jqzoom(options);
    },

    setupImageSlider: function() {
        var paused = false;
        var pausedByHover = false;
        setSlideshowControl();

        $('#slideshow img').each(function(i){
            var imgUrl = $(this).attr('src');
            imgUrl = imgUrl.replace(/normal\//, "large/");
            $(this).parent().attr('href', imgUrl);
        });

        //set the slideshow controls handlers
        $('#slideshowControl img.pause')
        .click(function() {
            pauseSlideshow();
        });
        $('#slideshowControl img.play')
        .click(function() {
            playSlideshow();
        });
        $('#slideshowControl img.forward')
        .click(function() {
            slideSwitch();
        });

        function slideSwitch() {
            var $active = $('#slideshow div.active');
            if ($active.length == 0) {
                $active = $('#slideshow div:last');
            }

            var $next = $active.next().length ? $active.next() : $('#slideshow div:first');
            $active.addClass('last-active');

            $next
            .css({opacity: 0.0})
            .addClass('active')
            .animate({opacity: 1.0}, 1000, function() {
                $active.removeClass('active last-active');
            });
        }

        function setSlideshowControl() {
            $('#slideshowControl img').hide();
            if (paused) {
                $('#slideshowControl img.play').show();
                $('#slideshowControl img.forward').show();
            } else {
                $('#slideshowControl img.pause').show();
            }
        }

        function pauseSlideshow(setControls) {
            setControls = setControls == undefined ? true : setControls;
            paused = true;
            if (setControls) {
                setSlideshowControl();
            }
            $(document).stopTime();

        }

        function playSlideshow() {
            paused = false;
            setSlideshowControl();
        }


    }
}

//=====================================================//
var Member = {
    copyBillingToDeliveryAddress: function(e) {
        $('#fld_shipping_address_flat').val($('#fld_address_flat').val());
        $('#fld_shipping_address_street').val($('#fld_address_street').val());
        $('#fld_shipping_address_town').val($('#fld_address_town').val());
        $('#fld_shipping_address_state').val($('#fld_address_state').val());
        $('#fld_shipping_address_country').val($('#fld_address_country').val());
        $('#fld_shipping_address_po_code').val($('#fld_address_po_code').val());
        $('#fld_shipping_address_country').change();
    },

    setupRegisterForm: function(e) {
        var extraPar = {
            callback: function(json, statusText, jqFormObj, extraParamObj) {
                var msg = json.returnText;
                $('#registerForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                Util.hideProgressInd();
            },
            beforeSubmit: function() {
                Util.showProgressInd();
            },
            dataType: 'json'
        };
        $('#registerForm').ajaxForm(options);
    },
    
    setupEditProfileForm: function(e) {
        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Your profile has been updated.</div>";
                $('#editProfileForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#editProfileForm').ajaxForm(options);
    },

    onchangeAddressCountry: function() {
        var country = $(this).val();
        if (Util.isCountryUS(country)) {
            $('#fld_address_state').parent('div').slideDown();
        } else {
            $('#fld_address_state').parent('div')
            .slideUp(function() {
                $('#fld_address_state').val('');
            });
        }
    },

    onchangeAddressCountryShipping: function() {
        var country = $(this).val();
        if (Util.isCountryUS(country)) {
            $('#fld_shipping_address_state').parent('div').slideDown();
        } else {
            $('#fld_shipping_address_state').parent('div')
            .slideUp(function() {
                $('#fld_shipping_address_state').val('');
            });
        }
    }
}

var Dialog = {

    setUpForm: function(formName) {
        $('#' + formName).livequery(function() {

            /****************************************************/
            var extraPar = {
                callback: function(json) {
                    if (json.returnText != ''){
                    	$('#dialog').dialog('destroy');
                        Util.showSimpleMessageInDialog(json.returnText);
                    }
                }
            }

            var options = {
                success: function(json, statusText, jqFormObj) {
                    Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                    Util.hideProgressInd();
                },
                beforeSubmit: function(frmData) {
                    Util.showProgressInd();
                },
                dataType: 'json'
            };

            $('#' + formName).ajaxForm(options);

        });
    },

    openDialog: function(formName, dialogTitle) {

        url = $(this).attr('href');

        Util.showProgressInd();

        $.get(url, function(data){
            Util.initDialog();
            $('#dialog').html(data);

            var xButtons = {};

            xButtons[Lang.data.submit] = function() {
                $('#' + formName).submit();
            };

            xButtons[Lang.data.cancel] = function() {
                $(this).dialog('close');
                $(this).dialog('destroy');
            };

            var x_dialog = $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 400,
                    height: 250,
                    title: dialogTitle,
                    buttons: xButtons
                })
            );
            Util.hideProgressInd();
        });
    }

}

