$(document).ready(    
    function(){     
        //print label
        $('.print_label').live('click', function(){
            var id = Math.floor(Math.random()*1000)
            var io = $('<iframe id="' + id + '" name="' + id + '" />');

            io.attr('src', 'index/get-printable-label');
            io.css('position', 'absolute');
            io.css('top', '-1000px');
            io.css('left', '-1000px');
            io.appendTo(document.body);
            return false;
        });

        //validate shipping form on slide 3
        $('#shipping').submit(function(){                
                $.post("customer/ajax-save-shipping-address",
                        {
                                'street': $('#shipping input[name = "street"]').val(),
                                'city': $('#shipping input[name = "city"]').val(),
                                'state': $('#shipping select[name = "state"] option:selected').val(),
                                'post_code': $('#shipping input[name = "post_code"]').val()
                        },
                        function(data) {
                                if (!data.success){
                                        $.each(data.errors, function(k,v){
                                                //display errors
                                                $('<em>'+v+'</em>').insertAfter($('#shipping [name="'+k+'"]'));
                                        });
                                }else{
                                    //populate fields in the shipping box on 4th slide
                                    var customer = data.customer;
                                    $('#shipping-box input[name="address"]').val(customer['street']);
                                    $('#shipping-box input[name="town"]').val(customer['city']);
                                    $('#shipping-box select[name="state"] option:selected').removeAttr('selected');
                                    $('#shipping-box select[name="state"] option[value="' + customer['state']+'"]').attr('selected', 'selected');
                                    $('#shipping-box input[name="post_code"]').val(customer['post_code']);
                                    $('#shipping-box input[name="comments"]').val('');
                                    __createDialog('#dialog_shipping_address_saved',{
                                    modal:true,
                                    draggable:true,
                                    resizable:false,
                                    width:460,
                                    buttons:{
                                            Ok:function(){
                                                    __closeDialog('#dialog_shipping_address_saved');
                                            }
                                    }
                                });
                                __openDialog('#dialog_shipping_address_saved');
                                }
                        },'json'
                );           
                return false;
        });

        function sendAjaxMessage(val){
            $.post('customer/ajax-save-message',{message : val},
                function(data){
                        console.log(data);
                        //add new message
                        $('.message-board .loading-container').append(data);                        
                        //refresh Cufon
                        Cufon.refresh();                        
                        //reset text input
                        jQuery('input[name="message"]').val('');
                        jQuery('input[name="message"]').blur();
                        //hide send button
                        var slide3Scrollbar = $('#fix_scrollbar');
                        slide3Scrollbar.tinyscrollbar_update('bottom');
                        var slide4Scrollbar = $('#pay_scrollbar');
                        slide4Scrollbar.tinyscrollbar_update('bottom');
                },'text');            
        }
        var keywodsText;
        $("input.before-focus").live("focus", function(){
                keywordsText = jQuery(this).val();
                jQuery(this).val("");
                jQuery(this).removeClass('before-focus');
        });
        $('input.before-focus').blur(function(){
                var val = jQuery(this).val();
                if (val == ''){
                        jQuery(this).val(keywordsText);
                        jQuery(this).addClass('before-focus');
                }
        });
        $('input[name="message"]').keyup(function(event){
            if (jQuery(this).val().length > 0){
                    //show button
                    jQuery(this).parent().find('#send').fadeIn(500);
            }else{
                    //hide button
                    jQuery(this).parent().find('#send').fadeOut(500);
            }
            if (event.keyCode == '13') {
                    //ENTER is pressed
                    event.preventDefault();
                    var val = jQuery(this).val();
                    jQuery(this).blur();
                    if (jQuery(this).hasClass('before-focus') == false){
                            //send the message
                            sendAjaxMessage(val);
                            jQuery(this).parent().find('#send').fadeOut(500);
                    }
            }
        });
        $('#send').live('click', function(){
            sendAjaxMessage(jQuery(this).parent().find('input[name="message"]').val());
            jQuery(this).fadeOut(500);
            return false;
        });
        $('#btn_submit_customer').live('click',function(){
            A25Core.__validate('#form_customer', 'customer/ajax-check-customer',function(resp){                
                if (resp.success){
                    if (resp.customerExists){                        
                        __createDialog('#dialog_update_customer',{
                                modal:true,
                                draggable:true,
                                resizable:false,
                                width:460,
                                buttons:{
                                      Yes:function(){
                                            $.post('customer/ajax-update-basic-information', {id: resp.customerId,
                                                        name:$('input[name="first_name"]').val(),
                                                        email: $(' input[name="email"]').val(),
                                                        phone: $(' input[name="phone"]').val(),
                                                        message: jQuery('input[name="comment"]').val()
                                                    },
                                                    function(data){
                                                        if (!data.success){
                                                                //customer updated unsuccessfully
                                                                jQuery.each(data.errors, function(k,v){
                                                                        //display errors
                                                                        jQuery('#dialog_update_customer [name="'+k+'"]').parent().next('em').html(v);
                                                                });
                                                        }else{
                                                           __closeDialog('#dialog_update_customer');
                                                           window.location.href = "repair/before-repair";
                                                        }        
                                             },'json');
                                      },
                                      No:function(){
                                            $.post("customer/ajax-simple-login", {
                                                    email: $('input[name="email"]').val(),
                                                    message: jQuery('input[name="comment"]').val()
                                                },function(aslData) {
                                                    if (!aslData.success){
                                                        //customer updated unsuccessfully
                                                        jQuery.each(aslData.errors, function(k,v){
                                                                //display errors
                                                                jQuery('#dialog_update_customer [name="'+k+'"]').parent().next('em').html(v);
                                                        });
                                                    }else{
                                                        //hide the login link below the logo
                                                        __closeDialog('#dialog_update_customer');
                                                        window.location.href = "repair/before-repair";
                                                    }
                                            },'json');
                                      }
                                }
                        });
                        __openDialog('#dialog_update_customer');
                    } else {
                        $.post("customer/ajax-add-basic-info", {
                                name: jQuery('input[name="first_name"]').val(),
                                email: jQuery('input[name="email"]').val(),
                                phone: jQuery('input[name="phone"]').val(),
                                message: jQuery('input[name="comment"]').val()
                        },
                        function(data) {
                            if (!data.success){
                                //customer added unsuccessfully
                                jQuery.each(data.errors, function(k,v){
                                    //display errors
                                    jQuery(dialogId+' [name="'+k+'"]').parent().next('em').html(v);
                                });
                            }else{
                                window.location.href = "repair/before-repair";
                            }
                        },'json');
                    }
                }
            });
        });
    }
);



