﻿jQuery(document).ready(function(){  
    //Slide show
    jQuery.each(jQuery('.anh-slideshow-image'),function(i,img){
        jQuery(img).mouseover(function(){
            jQuery('#anh-slideshow-image').attr('src',jQuery(img).attr('alt'));        
        });        
    });
    //End Slide show
    // SmallImage
    jQuery.each(jQuery('.hangHoaDetailsImgSmall'),function(i){
        jQuery('.hangHoaDetailsImgSmall:eq(' + i + ')').mouseenter(function(){                
            jQuery('#hangHoaDetailsImg').attr('src',this.title);
            this.className='hangHoaDetailsImgSmallHover';
        });
        jQuery('.hangHoaDetailsImgSmall:eq(' + i + ')').mouseleave(function(){                               
            this.className='hangHoaDetailsImgSmall';
        });
   }); 
   jQuery('.anh-slideshow-prev').click(function(){
        if (jQuery('.anh-slideshow-main').scrollLeft() >= 0)
        {
            jQuery('.anh-slideshow-main').animate({"scrollLeft": "-=250px"}, 500);          
        }                
   });    
   jQuery('.anh-slideshow-next').click(function(){        
        jQuery('.anh-slideshow-main').animate({"scrollLeft": "+=250px"}, 500);
   });    
    //End SmallImage    
      
    // Mua hang
    jQuery('#btn_muahang').click(function(){
        jQuery('#popAdd').load('lib/aspx/gioHang_add.htm',function(){
                // dialog
                jQuery('#popAdd').dialog({
                    close: function(){
                        jQuery(this).dialog('destroy');                                    
                    },
                    open: function(){  
                        jQuery('#HH_SoLuong').val('1');
                        jQuery('#HH_SoLuong').keyup(function(){
                            jQuery('#HH_SoLuong').val(formatNumber(jQuery('#HH_SoLuong').val()));                            
                        });
                    },
                    height : 100,
                    width: 400,   
                    show: 'slide',                     
                    title: 'Thêm vào giỏ hàng',                                            
                    buttons: {                                        
                        'Thêm vào giỏ hàng': function() {		                    		                    
                            var HH_Ten = jQuery('#btn_muahang_ten').html();
                            var HH_Gia = jQuery('#btn_muahang_gia').html().replace(',','');
                            var HH_Anh = jQuery('#btn_muahang_img').attr('src');
                            var HH_ID = jQuery('#btn_muahang').attr('title');
                            var HH_SoLuong = jQuery('#HH_SoLuong').val();
                            if (HH_SoLuong == '')
                            {
                                HH_SoLuong = '1';                                
                            }
                            jQuery.getJSON('lib/aspx/gioHang.aspx',{
                                'action' : 'add',
                                'HH_ID' : HH_ID,
                                'HH_Ten' : HH_Ten,
                                'HH_Gia' : HH_Gia,
                                'HH_Anh' : HH_Anh,
                                'HH_SoLuong' : HH_SoLuong
                            },function(data){                                                                
                                loadCart();
                            });
                            jQuery(this).dialog('destroy');
                            loadStatus();
                        },
                        'Đóng lại': function() {		                    		                    
                            jQuery(this).dialog('destroy');			                
                        }
                    }		                
                    });             
                 // end dialog
            });   
    });
    // End mua hang
    // Lien he
    jQuery('#lienHeLoad').click(function(){
        jQuery('#pop').load('lib/aspx/lienHe.htm',function(){
            // dialog
            jQuery('#pop').dialog({
                    close: function(){
                        jQuery(this).dialog('destroy');                                    
                    },
                    open: function(){                                                                             
                    },
                    height : 350,
                    width: 400,   
                    show: 'slide',                     
                    title: 'Liên hệ',                                            
                    buttons: {                                        
                        'Gửi liên hệ': function() {		                    		                                                
                            var KH_Ten = jQuery('#KH_Ten').val();
                            var KH_DiaChi = jQuery('#KH_DiaChi').val();
                            var KH_Email = jQuery('#KH_Email').val();
                            var KH_Telephone = jQuery('#KH_Telephone').val();
                            var KH_Mobile = jQuery('#KH_Mobile').val();
                            var KH_TieuDe = jQuery('#KH_TieuDe').val();
                            var loi = '';
                            if (KH_Ten == null || KH_Ten == '')
                            {		     
                                loi += ' - Nhập tên\n';                   
                            }
                            if (KH_DiaChi == null || KH_DiaChi == '')
                            {		     
                                loi += ' - Nhập địa chỉ\n';                   
                            }
                            if (KH_Email == null || KH_Email == '')
                            {		     
                                loi += ' - Nhập e-mail\n';                   
                            }
                            if (KH_Telephone == null || KH_Telephone == '')
                            {		     
                                loi += ' - Nhập telephone\n';                   
                            }
                            if (KH_Mobile == null || KH_Mobile == '')
                            {		     
                                loi += ' - Nhập mobile\n';                   
                            }
                            if (KH_TieuDe == null || KH_TieuDe == '')
                            {		     
                                loi += ' - Nhập Tiêu đề\n';                   
                            }
                            if (loi != '')
                            {
                                alert('Lỗi\n' + loi + '- - - - - - -');
                                return false;
                            }
                            jQuery.getJSON('lib/aspx/lienHe.aspx',{
                               'KH_Ten' : KH_Ten,
                               'KH_DiaChi' : KH_DiaChi,
                               'KH_Email' : KH_Email,
                               'KH_Telephone' : KH_Telephone,
                               'KH_Mobile' : KH_Mobile,
                               'KH_TieuDe' : KH_TieuDe
                            },function(data){                                                                
                                alert(data);
                            });
                            jQuery(this).dialog('destroy');
                        },
                        'Đóng lại': function() {		                    		                    
                            jQuery(this).dialog('destroy');			                
                        }
                    }		                
                    });                                
            // end dialog
        });
    });
    // End lien he               
   jQuery.each(jQuery('.Box_SPS'),function(i,hangHoa){
       jQuery('.Box_SPS:eq(' + i + ') > .btn_muahang').click(function(){
       // Load Page
       jQuery('#popAdd').load('lib/aspx/gioHang_add.htm',function(){
                // dialog
                jQuery('#popAdd').dialog({
                    close: function(){
                        jQuery(this).dialog('destroy');                                    
                    },
                    open: function(){  
                        jQuery('#HH_SoLuong').val('1');
                        jQuery('#HH_SoLuong').keyup(function(){
                            jQuery('#HH_SoLuong').val(formatNumber(jQuery('#HH_SoLuong').val()));                            
                        });
                    },
                    height : 100,
                    width: 400,   
                    show: 'slide',                     
                    title: 'Thêm vào giỏ hàng',                                            
                    buttons: {                                        
                        'Thêm vào giỏ hàng': function() {		                    		                    
                            var HH_Ten = jQuery('.Box_SPS:eq(' + i + ') > .BOX_SPS_Ten').html();
                            var HH_Gia = jQuery('.Box_SPS:eq(' + i + ') > .BOX_SPS_gia').html().replace(',','');
                            var HH_Anh = jQuery('.Box_SPS:eq(' + i + ') > .BOX_SPS_Anh > img').attr('src');
                            var HH_ID = jQuery('.Box_SPS:eq(' + i + ') > .btn_muahang').attr('title');
                            var HH_SoLuong = jQuery('#HH_SoLuong').val();
                            if (HH_SoLuong == '')
                            {
                                HH_SoLuong = '1';                                
                            }
                            jQuery.getJSON('lib/aspx/gioHang.aspx',{
                                'action' : 'add',
                                'HH_ID' : HH_ID,
                                'HH_Ten' : HH_Ten,
                                'HH_Gia' : HH_Gia,
                                'HH_Anh' : HH_Anh,
                                'HH_SoLuong' : HH_SoLuong
                            },function(data){                                                                
                                loadCart();                                
                            });
                            jQuery(this).dialog('destroy');
                        },
                        'Đóng lại': function() {		                    		                    
                            jQuery(this).dialog('destroy');			                
                        }
                    }		                
                    });                                 
                 // end dialog
            }); 
            // End Load Page       
       });
   });   
   jQuery('#gioHangDetails').mouseover(function(){
        loadCart();             
   });   
   jQuery('#hotro-lienhe').mouseover(function(){
        //lienHe.htm
          jQuery('#pop').load('lib/aspx/lienHe.htm',function(){
            // dialog
            jQuery('#pop').dialog({
                    close: function(){
                        jQuery(this).dialog('destroy');                                    
                    },
                    open: function(){                                                                             
                    },
                    height : 500,
                    width: 500,   
                    show: 'slide',                     
                    title: 'Liên hệ',                                            
                    buttons: {                                        
                        'Gửi liên hệ': function() {		                    		                                                
                            var KH_Ten = jQuery('#KH_Ten').val();
                            var KH_DiaChi = jQuery('#KH_DiaChi').val();
                            var KH_Email = jQuery('#KH_Email').val();
                            var KH_Telephone = jQuery('#KH_Telephone').val();
                            var KH_Mobile = jQuery('#KH_Mobile').val();
                            var KH_TieuDe = jQuery('#KH_TieuDe').val();
                            var loi = '';
                            if (KH_Ten == null || KH_Ten == '')
                            {		     
                                loi += ' - Nhập tên\n';                   
                            }
                            if (KH_DiaChi == null || KH_DiaChi == '')
                            {		     
                                loi += ' - Nhập địa chỉ\n';                   
                            }
                            if (KH_Email == null || KH_Email == '')
                            {		     
                                loi += ' - Nhập e-mail\n';                   
                            }
                            if (KH_Telephone == null || KH_Telephone == '')
                            {		     
                                loi += ' - Nhập telephone\n';                   
                            }
                            if (KH_Mobile == null || KH_Mobile == '')
                            {		     
                                loi += ' - Nhập mobile\n';                   
                            }
                            if (KH_TieuDe == null || KH_TieuDe == '')
                            {		     
                                loi += ' - Nhập Tiêu đề\n';                   
                            }
                            if (loi != '')
                            {
                                alert('Lỗi\n' + loi + '- - - - - - -');
                                return false;
                            }
                            alert('Cám ơn quý khách đã góp ý\Chúng tôi sẽ liên lạc lại với bạn trong thời gian sớm nhất');
                            jQuery.getJSON('lib/aspx/lienHe.aspx',{
                               'KH_Ten' : KH_Ten,
                               'KH_DiaChi' : KH_DiaChi,
                               'KH_Email' : KH_Email,
                               'KH_Telephone' : KH_Telephone,
                               'KH_Mobile' : KH_Mobile,
                               'KH_TieuDe' : KH_TieuDe
                            },function(data){                                                                                                
                            });
                            jQuery(this).dialog('destroy');
                        },
                        'Đóng lại': function() {		                    		                    
                            jQuery(this).dialog('destroy');			                
                        }
                    }		                
                    });                                
            // end dialog
        });
   });    
 });  
 function loadCart()
 {
     jQuery('#pop').load('lib/aspx/gioHang_show.htm',function(){
        // dialog
        jQuery('#pop').dialog({
            close: function(){
                jQuery(this).dialog('destroy');                                    
            },            
            open: function(){                
                jQuery.getJSON('lib/aspx/gioHang.aspx',{'ref' : Math.random()},function(data){
                    var l = '';                        
                    jQuery.each(data.GioHangList,function(i,hang){                        
                        l += '<li>';
                        
                        l += '<img src=\"' + hang.Anh + '\" />';
                        l += '<a href=\"javascript:;\">Xóa<a/>';
                        
                        l += '<b>' + hang.Ten + '</b><br/>' + hang.Gia + '<br/>';                        
                        l += hang.SoLuong                                                
                        
                        l += '</li>';                                                
                    });
                    l += '<li class=\"gioHangShow-total\">';
                    l += 'Tổng số tiền: <span>' +  data.Total + '</span>';
                    l += '</li>';
                    jQuery('#gioHangShow').append(l);
                });
            },
            height : 400,
            width: 600,   
            show: 'slide',         
            title: 'Giỏ hàng',                        
            modal: true,
            buttons: {                
                'Đóng lại': function() {		                    		                    
                    jQuery(this).dialog('destroy');
                    loadStatus();			                
                },
                'Thanh toán': function() {		                    		                    
                    jQuery(this).dialog('destroy');	
                    checkOut();		                
                },
                'Xóa giỏ hàng': function() {		                    		                    
                    jQuery(this).dialog('destroy');	 
                    jQuery.post('lib/aspx/gioHang.aspx',{'action': 'clear'},function(){
                        jQuery('#gioHangDetails').html('Giỏ hàng: 0 (chi tiết)');
                    });                 
                },
                'Cập nhật': function() {		                    		                    
                    jQuery(this).dialog('destroy');
                    loadStatus();			                
                }
            }		                
        });                         
        // end dialog
    });  
 }
 function checkOut()
 {
    jQuery('#pop').load('lib/aspx/gioHang_check.htm',function(){
        // dialog
        jQuery('#pop').dialog({
            close: function(){
                jQuery(this).dialog('destroy');                                    
            },            
            open: function(){                                                                                                               
                var KH_Ten = jQuery('#KH_Ten').val('');
                var KH_DiaChi = jQuery('#KH_DiaChi').val('');
                var KH_Email = jQuery('#KH_Email').val('');
                var KH_Telephone = jQuery('#KH_Telephone').val('');
                var KH_Mobile = jQuery('#KH_Mobile').val('');
                var KH_TaiKhoan = jQuery('#KH_TaiKhoan').val('');
                var KH_NhanHangNguoiNhan = jQuery('#KH_NhanHangNguoiNhan').val('');
                var KH_NhanHangDiaChi = jQuery('#KH_NhanHangDiaChi').val('');
                var KH_NhanHangEmail = jQuery('#KH_NhanHangEmail').val('');
                var KH_NhanHangTelephone = jQuery('#KH_NhanHangTelephone').val('');
                var KH_NhanHangMobile = jQuery('#KH_NhanHangMobile').val('');
                var KH_NhanHangTaiKhoan = jQuery('#KH_NhanHangTaiKhoan').val('');
                var KH_MoTa = jQuery('#KH_MoTa').val('');
                jQuery('#KH_Ten').keyup(function(){
                    jQuery('#KH_NhanHangNguoiNhan').val(jQuery('#KH_Ten').val());
                });
                
                jQuery('#KH_DiaChi').keyup(function(){
                    jQuery('#KH_NhanHangDiaChi').val(jQuery('#KH_DiaChi').val());
                });
                
                jQuery('#KH_Email').keyup(function(){
                    jQuery('#KH_NhanHangEmail').val(jQuery('#KH_Email').val());
                });
                
                jQuery('#KH_Telephone').keyup(function(){
                    jQuery('#KH_NhanHangTelephone').val(jQuery('#KH_Telephone').val());
                });
                
                jQuery('#KH_Mobile').keyup(function(){
                    jQuery('#KH_NhanHangMobile').val(jQuery('#KH_Mobile').val());
                });
                
                jQuery('#KH_TaiKhoan').keyup(function(){
                    jQuery('#KH_NhanHangTaiKhoan').val(jQuery('#KH_TaiKhoan').val());
                });
            },
            height : 400,
            width: 800,   
            show: 'slide',                     
            title: 'Gửi đơn hàng',
            modal: true,
            buttons: {                
                'Quay lại giỏ hàng': function() {		                    		                    
                    jQuery(this).dialog('destroy');	
                    loadCart()		                
                },
                'Gửi đơn hàng': function() {
                    var loi = '';	
                    var KH_Ten = jQuery('#KH_Ten').val();
                    var KH_DiaChi = jQuery('#KH_DiaChi').val();
                    var KH_Email = jQuery('#KH_Email').val();
                    var KH_Telephone = jQuery('#KH_Telephone').val();
                    var KH_Mobile = jQuery('#KH_Mobile').val();
                    var KH_TaiKhoan = jQuery('#KH_TaiKhoan').val();
                    var KH_NhanHangNguoiNhan = jQuery('#KH_NhanHangNguoiNhan').val();
                    var KH_NhanHangDiaChi = jQuery('#KH_NhanHangDiaChi').val();
                    var KH_NhanHangEmail = jQuery('#KH_NhanHangEmail').val();
                    var KH_NhanHangTelephone = jQuery('#KH_NhanHangTelephone').val();
                    var KH_NhanHangMobile = jQuery('#KH_NhanHangMobile').val();
                    var KH_NhanHangTaiKhoan = jQuery('#KH_NhanHangTaiKhoan').val();
                    var KH_MoTa = jQuery('#KH_MoTa').val();
                    if (KH_Ten == null || KH_Ten == '')
                    {		     
                        loi += ' - Nhập tên\n';                   
                    }
                    if (KH_DiaChi == null || KH_DiaChi == '')
                    {		     
                        loi += ' - Nhập địa chỉ\n';                   
                    }
                    if (KH_Email == null || KH_Email == '')
                    {		     
                        loi += ' - Nhập email\n';                   
                    }
                    if (KH_Telephone == null || KH_Telephone == '')
                    {		     
                        loi += ' - Nhập số điện thoại\n';                   
                    }
                    if (KH_Mobile == null || KH_Mobile == '')
                    {		     
                        loi += ' - Nhập di động\n';                   
                    }
                    if (loi != '')
                    {
                        alert('Lỗi\n' + loi + '- - - - - - -');
                        return false;
                    }
                    jQuery.post('lib/aspx/gioHang.aspx',{
                        'action' : 'check',
                        'KH_Ten' : KH_Ten,
                        'KH_DiaChi' : KH_DiaChi,
                        'KH_Email' : KH_Email,
                        'KH_Telephone' : KH_Telephone,
                        'KH_Mobile' : KH_Mobile,
                        'KH_TaiKhoan' : KH_TaiKhoan,
                        'KH_NhanHangNguoiNhan' : KH_NhanHangNguoiNhan,
                        'KH_NhanHangDiaChi' : KH_NhanHangDiaChi,
                        'KH_NhanHangEmail' : KH_NhanHangEmail,
                        'KH_NhanHangTelephone' : KH_NhanHangTelephone,
                        'KH_NhanHangMobile' : KH_NhanHangMobile,
                        'KH_NhanHangTaiKhoan' : KH_NhanHangTaiKhoan,
                        'KH_MoTa' : KH_MoTa
                    },function(data){ 
                        alert('Chân thành cám ơn ' + KH_Ten + '\nChúng tôi sẽ liên lạc với bạn trong thời gian sớm nhất có thể');
                    });                    		                    
                    jQuery(this).dialog('destroy');			
                    loadStatus();                
                }                
            }		                
        });                         
        // end dialog
    });          
 }
 
function loadStatus()
{
    jQuery.getJSON('lib/aspx/gioHang.aspx',function(data){  
       var total=0;
       jQuery.each(data.GioHangList,function(i,hang){                                                            
            total += hang.SoLuong                                                                                                                      
        });  
       jQuery('#gioHangDetails').html('Giỏ hàng: ' + total + ' (chi tiết)');
    });
}
