var PostNewItem   = new Object();
//=====================================================//
//** extending the master Util class
var Util = $.extend(Util, {
    showSimpleMessageInDialog: function(msg) {

        Util.initDialog();
        $('#dialog').html(msg);
        
        var xButtons = {};
        
        xButtons.close = function() {
            $(this).dialog('close');
            $(this).dialog('destroy');
        };
        
        var x_dialog = $('#dialog').dialog(
            $.extend(Util.dialogDefaults, {
                buttons: xButtons
            })
        );
    }

});

//=====================================================//
var Validate = $.extend(Validate, {

});

var Content = {
    setupNewsletterForm: function(e) {
        var options = {
            success: function(json) {
                if (json.status == 'error') {
                    alert(json.errorMsg);
                    return;
                }

                var msg = "<div class='sysMessage'>" + json.html + "</div>";
                $('#newsLetterForm').html(msg);
                Util.hideProgress();
            },
            beforeSubmit: function() {
                Util.showProgress();
            },
            dataType: 'json'
        };
        $('#newsLetterForm').ajaxForm(options);
    }
}
//=====================================================//
var Common = {
    setTableColors: function() {
        $('table tr.row:even').css({'background-color': '#f3f3f3'});
        $('table tr.row:odd').css({'background-color': '#fafafa'});

    }
}
//----------------------------------------------------------------//
Basket = {
    addToBasket: function(e) {
        e.preventDefault();
        var url = "/index.php?_room=basket&_spAction=addToBasket&room=&showHTML=0" ;
        var product_id = $(this).attr('product_id');
        var data = {'product_id': product_id}
        $.post(url, data, function (json) {
            if (json.status == 'error') {
                Util.alert(json.errorMsg);
                return;
            }
            //Util.alert(json.html);
            var count = parseInt(json.count) + 0; //convert to number
            var countText = count + ' ITEM';
            if (count > 1) {
                countText = count + ' ITEMS';
            }

            $("a span.shoppingBagCount").html("(" + countText + ")");

            //---------------------//
            $("#miniCart").html(json.miniCart);
            Basket.showMiniCart.call($('a.miniCartLink'));
            //------------------//
            setTimeout(function() {
                $('#miniCart').slideUp();
            }, 4000);
            
        }, 'json');
    },

    setupShippingForm: function() {
        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj);
                Util.hideProgressInd();
            },
            beforeSubmit: function() {
                Util.showProgressInd();
            },
            dataType: 'json'
        };
        $('#shippingForm').ajaxForm(options);

    }
}

//**********************************************************************************//
Basket.setBillingDetails = function (isChecked)
{
	with (window.document.billingDetailsForm) {
		if (isChecked) {
			shipping_first_name.value      = cust_first_name.value;            
			shipping_last_name.value       = cust_last_name.value;            
			shipping_email.value           = cust_email.value;           
			shipping_phone.value           = cust_phone.value;           
			shipping_address_flat.value    = cust_address_flat.value;  
			shipping_address_street.value  = cust_address_street.value;  
			shipping_address_town.value    = cust_address_town.value;    
			shipping_address_country.value = cust_address_country.value; 
		}
		else{
			shipping_first_name.value      = "";            
			shipping_last_name.value       = "";            
			shipping_email.value           = "";           
			shipping_phone.value           = "";           
			shipping_address_flat.value    = "";  
			shipping_address_street.value  = "";  
			shipping_address_town.value    = "";    
			shipping_address_country.value = ""; 
		}
		   
	}
}

//=====================================================//
Basket.showMiniCart = function(){
    pos = $(this).offset();
    var liWidth = $(this).parent().width();
    var cartWidth = $('#miniCart').width();

    $('#miniCart')
    .css('left', pos.left - (cartWidth - liWidth))
    .css('top', $('#main').offset().top)
    .slideDown();
}
//------------------------------------------------//
var Product = {
    setupImageSlider: function() {
        var paused = false;
        var pausedByHover = false;
        setSlideshowControl();
        //$(document).everyTime(3000, function() {
        //    slideSwitch();
        //});

        $('#slideshow img').each(function(i){
            var imgUrl = $(this).attr('src');
            imgUrl = imgUrl.replace(/normal\//, "large/");
            $(this).parent().attr('href', imgUrl);
        });

        // $('#slideshow').hover(
        //     function(){
        //         if (!paused) {
        //             pauseSlideshow();
        //             pausedByHover = true;
        //         }
        //     },
        //     function(){
        //         if (pausedByHover) {
        //             playSlideshow(false);
        //         }
        //         pausedByHover = false;
        //     }
        // );

        //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();
            // $(document).everyTime(3000, function(i) {
            //     slideSwitch(i);
            // });
        }


    }
}
//=====================================================//
var Member = {
    setupRegisterForm: function(e) {
        /*var extraPar = {
            callback: function() {
                var msg =
                confirm(Lang.data.registerThanksText);
                "<div>" +
                "Thank you for registering with IMI. An email confirmation of your account has been sent to you.<br/><br/>" +

                "Please click continue shopping to go back to the product pages.<br/><br/>" +

                "The IMI team" +
                "</div>" + 

                "<div style='margin-top:110px; text-align:center;'>" +
                "<a href='/products/'><img src='/images/continue_shopping.jpg' /></a>" +
                "</div>"; 
                $('#registerForm').html(msg);
            }
        }*/
        var extraPar = {
            callback: function() {
                var url = '/index.php?_room=member&_spAction=newMemberThanks&showHTML=0';
                $.get(url, function (data) {
                    $('#registerForm').html(data);
                })
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
                Util.hideProgressInd();
            },
            beforeSubmit: function() {
                Util.showProgressInd();
            },
            dataType: 'json'
        };
        $('#registerForm').ajaxForm(options);
    },
    
    setupEditProfileFormForm: 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);
    }
}
//------------------------------------------------//
var Dialog = {

    setUpForm: function(formName) {
        $('#' + formName).livequery(function() {

            /****************************************************/
            var extraPar = {
                callback: function(json) {
                    if (json.returnText != ''){
                        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.submit = function() {
                $('#' + formName).submit();
            };

            xButtons.cancel = function() {
                $(this).dialog('close');
                $(this).dialog('destroy');
            };

            var x_dialog = $('#dialog').dialog(
                $.extend(Util.dialogDefaults, {
                    width: 450,
                    height: 410,
                    title: dialogTitle,
                    buttons: xButtons
                })
            );
            Util.hideProgressInd();
        });
    }

}
var EmailFriend = {
    emailToFriendForm: function(e) {
        var extraPar = {
            callback: function() {
                var msg = "<div class='sysMessage'>Your message has been sent successfully.</div>";
                $('#emailToFriendForm').html(msg);
            }
        }

        var options = {
            success: function(json, statusText, jqFormObj) {
                Validate.validateFormData(json, statusText, jqFormObj, extraPar);
            },
            beforeSubmit: function() {},
            dataType: 'json'
        };
        $('#emailToFriendForm').ajaxForm(options);
    }
}

//------------------------------------------------//

