// JavaScript Document
var webpath = '/';

$.ajax({type:'POST',beforeSend:function(){},url:webpath + 'islogin.asp',data:'',dataType:'html',error:'',success:function(html){
		if   (navigator.appName   ==   'Netscape') 
			var   language   =   navigator.language; 
		else 
			var   language   =   navigator.browserLanguage; 
		if((language.indexOf( 'zh')>-1)&&(html!="1"))
			window.location.href = webpath + 'login.asp';
	}});


jQuery.fn.loadpic = function(options){
    options = $.extend({
        src: "",
        mw: 120,
        mh: 120,
        mt: 0,
        ml: 0
    }, options);
    var _self = this;
    _self.hide();
    var img = new Image();
    $(img).load(function(){
        _self.attr("src", options.src);
        if (_self.width() > options.mw || _self.height() > options.mh) 
            _self.width() > _self.height() ? _self.width(options.mw) : _self.height(options.mh);
        if (_self.height() < options.mh) {
            _self.css('margin', ((options.mh - _self.height()) / 2 + parseInt(options.mt)) + 'px ' + (parseInt(options.ml) + parseInt(options.mw - _self.width()) * 0.5) + 'px').fadeIn('slow');
        }
        else {
            _self.css('margin', options.mt + 'px ' + (parseInt(options.ml) + parseInt(options.mw - _self.width()) * 0.5) + 'px').fadeIn('slow');
        }
    }).attr("src", options.src);
    return _self;
}
var common = {
    //<a href="javascript:void(0)" onClick="addBookmark()">favorites</a>
    addBookmark: function(){
        var url = parent.location.href;
        if (window.sidebar) {
            window.sidebar.addPanel(document.title, url, "");
        }
        else 
            if (document.all) {
                window.external.AddFavorite(url, document.title);
            }
            else 
                if (window.opera && window.print) {
                    return true;
                }
    },
    //<a href="javascript:void(0)" onclick="setHome(this,window.location)">set home</a> 
    setHome: function(obj, vrl){
        try {
            obj.style.behavior = 'url(#default#homepage)';
            obj.setHomePage(vrl);
        } 
        catch (e) {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                } 
                catch (e) { //Sorry! Your browser does not support direct Homepage
                    alert('"Sorry! Your browser does not support direct Homepage. In the browser address bar enter "about: config" and press Enter and then [signed.applets.codebase_principal_support] is set to "true", click "Add Favorite" after ignoring safety tips, you can set up successfully."');
                }
                var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                prefs.setCharPref('browser.startup.homepage', vrl);
            }
        }
    },
    resize: function(){
        if ($('.h-v-resize').size() > 0 && $('.h-v-resize img.resize').size() > 0) {
            $('.h-v-resize').each(function(){
                var mw = $(this).attr('maxwidth');
                var mh = $(this).attr('maxheight');
                var mt = $(this).attr('mtop');
                var ml = $(this).attr('mleft');
                $('img.resize', this).each(function(){
                    $(this).loadpic({
                        src: $(this).attr('rel'),
                        mw: mw,
                        mh: mh,
                        mt: mt,
                        ml: ml
                    });
                });
            });
        }
    },
    cycle: function(){
        if ($('.banner').size() > 0) 
            $.getScript(webpath + 'js/jquery.cycle.all.min.js', function(){
                $('.banner').cycle({
                    fx: 'fade'
                });
            });
    },
    toNumber: function(num){
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num)) 
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10) 
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
            num.substring(num.length - (4 * i + 3));
        return (num + '.' + cents);
    },
    inputnum: function(){
        if ($('.inputnum').size() > 0) 
            $('.inputnum').keyup(function(){
                if ($(this).val().length > 0) {
                    if (!/^[1-9][0-9]*$/.test($(this).val())) {
                        $(this).val($(this).val().toString().replace(/\D/g, ''));
                    }
                }
                else {
                    $(this).val(1);
                }
            });
    },
    unique: function(arr){
        arr = arr || [];
        if (arr.length <= 1) 
            return arr;
        var uniquearr = [];
        var _a = {};
        for (var i = 0; i < arr.length - 1; i++) {
            if (!_a[arr[i]]) {
                _a[arr[i]] = 1;
                uniquearr.push(arr[i]);
            }
        }
        return uniquearr;
    }
};
(function($){
    // 改变默认配置
    var d = $.dialog.defaults;
    d.path = webpath + 'css';
    // 预缓存皮肤，数组第一个为默认皮肤
    d.skin = ['aero', 'default', 'chrome', 'facebook'];
    d.plus = true;
    // 支持拖动
    d.drag = true;
    // 是否开启特效
    //d.effect = true;
    // 指定超过此面积的对话框拖动的时候用替身
    //d.showTemp = 0;

})(art);
$(function(){
    common.resize();
    $('select').bgiframe();
    common.inputnum();
    if ($('.banner').size() > 0) 
        $('.banner').cycle({
            fx: 'fade'
        });
    
    
    $('.top-location .class-list h1').filter(function(index){
        return $('ul', this).size() > 0;
    }).each(function(){
        $('ul', this).offset({
            'top': parseInt($(this).offset().top) + 'px',
            'left': parseInt($(this).offset().left + 190) + 'px'
        }).find('li:first').css('border-left', '1px solid #FFF');
        $(this).hover(function(){
            var t_h = $('.top-location .class-list').height() + $('ul', this).height();
            var h_h = parseInt($('.top-location .class-list h1').size() * $('.top-location .class-list h1:first').height());
            if (t_h > h_h) 
                $('.top-location .class-list').height($('.top-location .class-list').height() + $('ul', this).height());
            $(this).find('a:first').addClass('selected').end().find('ul').show();
        }, function(){
            var t_h = $('.top-location .class-list').height();
            var h_h = parseInt($('.top-location .class-list h1').size() * $('.top-location .class-list h1:first').height());
            if (t_h > h_h) 
                $('.top-location .class-list').height($('.top-location .class-list').height() - $('ul', this).height());
            $(this).find('a:first').removeClass('selected').end().find('ul').hide();
        });
    });
    $('.drop-pc ').hover(function(){
        $('.class-list', this).stop(true, true).delay(500).slideDown();
    }, function(){
        $('.class-list', this).stop(true, true).delay(500).slideUp();
    });
    
    $('.i-left h1').filter(function(index){
        return $('ul', this).size() > 0;
    }).each(function(){
        $('ul', this).offset({
            top: parseInt($(this).offset().top),
            left: parseInt($(this).offset().left + 190)
        }).find('li:first').css('border-left', '1px solid #FFF');
        $(this).hover(function(){
            $(this).find('a:first').addClass('selected').end().find('ul').show();
        }, function(){
            $(this).find('a:first').removeClass('selected').end().find('ul').hide();
        });
    });
    if ($('#iptab li').size() > 0) 
        $('#iptab li').mouseover(function(){
            $(this).addClass('selected').siblings().removeClass('selected');
            $('#' + $(this).attr('id') + 'content').stop(true, true).show().siblings().hide();
        });
    if ($('#countdown').size() > 0) 
        $('#countdown').countdown({
            until: leftoftime,
            compact: true,
            description: '',
            timezone: +8
        });
    $(".lazyload img").lazyload({
        placeholder: webpath + "images/loading.gif",
        effect: "fadeIn",
        threshold: 200
    });
    /*
     $('.rotateimg').rotate({
     bind: [{
     "mouseover": function(){
     $(this).rotateAnimation(360);
     }
     }, {
     "mouseout": function(){
     $(this).rotateAnimation(0);
     }
     }]
     });
     */
    $('.pwd').chromaHash({
        minimum: 3
    });
    
    $.getScript(webpath + 'js/cart.js');
    /*
     var d = art.dialog.defaults;
     $.dialog.defaults.path =  '/css';
     
     // 按需加载要用到的皮肤，数组第一个为默认皮肤
     // 如果只使用默认皮肤，可以不填写skin
     d.skin = ['default','aero', 'chrome', 'facebook'];
     
     // 支持拖动
     d.drag = true;
     */
    $('.freight').click(function(){
        $.dialog.load('freight.asp?id=' + $(this).attr('pid'), {
            title: 'Freight',
            id: 'FreightDialog',
            yesText: 'yes',
            yesFn: function(){
                var t = $('input[name="shipaction"]:checked');
                if ($('input[name="shipaction"]:checked').val() > 0) {
                    $('#total_price').data('freightprice', t.val());
                    $('.freight_price').html('US $ ' + common.toNumber(t.val()));
                }
                else {
                    $('#total_price').data('freightprice', '0');
                    $('.freight_price').html('<img src="' + webpath + 'images/fs.gif" />');
                }
                $('.freight').attr({
                    'region': t.attr('rid'),
                    'destinations': t.attr('did')
                }).html(t.attr('regionname') + ' Via ' + t.attr('rname'));
                $('#total_price').html('$ ' + common.toNumber($('#total_price').data('price')) + ' * ' + $('#total_price').data('num') + ' + $ ' + common.toNumber($('#total_price').data('freightprice')) + ' = $' + common.toNumber(parseFloat($('#total_price').data('subtotal')) + parseFloat($('#total_price').data('freightprice'))));
            },
            initFn: function(){
                $("select#freight_wrap").jgdDropdown();
                $('#ship').change();
            }
        });
    });
    $('#ship').live('change', function(){
        $('.freight').attr('freight', $(this).val());
        $.ajax({
            type: 'POST',
            beforeSend: function(){
                $('#freight_content').html('loading...');
            },
            url: webpath + 'user/getallinfo.asp',
            data: {
                id: $(this).attr('pid'),
                pnum: $('#pnum_' + $(this).attr('pid')).val(),
                region: $(this).val()
            },
            dataType: 'json',
            error: function(){
                $.dialog({
                    title: 'Error',
                    time: 2,
                    content: 'Error'
                });
            },
            success: function(json){
                if (json.error == 1) {
                    $.dialog({
                        title: 'Error',
                        content: json.html
                    });
                    return false;
                }
                $('#total_price').data('subtotal', json.subtotal);
                $('#total_price').data('price', json.price);
                $('#total_price').data('num', json.num);
                if (json.delivery.length > 0) {
                    html = '<ul>';
                    for (var i = 0; i < json.delivery.length; i++) 
                        if (json.delivery[i].disable == '0') {
                            html += '<li><div>';
                            if (json.delivery[i].pic != '') 
                                html += '<img name="" src="' + json.delivery[i].pic + '" width="150" height="150" alt="' + json.delivery[i].name + '" />';
                            html += '</div><label><span class="name">' + json.delivery[i].name + '</span><span class="price"><font style="text-decoration:line-through;color:gray;">US $ ' + common.toNumber(parseFloat(json.delivery[i].price)) + '</font> US $ ' + common.toNumber(parseFloat(json.delivery[i].price * (json.delivery[i].discount / 100))) + '</span><span class="save">Save US $ ' + common.toNumber(parseFloat(json.delivery[i].price * ((100 - json.delivery[i].discount) / 100))) + '</span><span class="operation"><input name="shipaction" type="radio" value="' + parseFloat(json.delivery[i].price * (json.delivery[i].discount / 100)) + '" rname="' + json.delivery[i].name + '" regionname="' + json.regionname + '" did="' + json.delivery[i].id + '" rid="' + json.region + '" /></span></label></li>';
                        }
                    html += '</ul>';
                    $('#freight_content').html(html);
                }
            }
        });
    });
    if ($('#SpecialsProduct').size() > 0) {
        $.ajax({
            type: 'POST',
            beforeSend: function(){
            },
            url: webpath + 'Specials.asp',
            data: '',
            dataType: 'html',
            error: function(){
                $.dialog({
                    title: 'Error',
                    time: 2,
                    content: 'Error'
                });
            },
            success: function(html){
                $('#SpecialsProduct').append(html);
                carts.addtocart();
            }
        });
    }
    $(".watermark").each(function(){
        $(this).watermark($(this).attr('watermarktip'));
    });
    
    if ($('#right_submit_message').size() > 0) {
        $('#right_submit_message').click(function(){
            $.ajax({
                type: 'POST',
                beforeSend: function(){
                },
                url: webpath + 'user/right_message_act.asp',
                data: $(this).closest('form').serialize(),
                dataType: 'html',
                error: '',
                success: function(html){
                    $.dialog({
                        title: 'Message',
                        time: 2,
                        id: 'loginshow',
                        content: html
                    });
                }
            });
        });
    }
    if ($('#display_list').size() > 0) {
        var displaymode = 1;
        $('#display_grid').click(function(){
            if (displaymode == 2) {
                $(this).attr('src',$(this).attr('src').replace('display_grid', 'display_grid_act'));
                $('#display_list').attr('src',$('#display_list').attr('src').replace('display_list_act', 'display_list'));
				$('.list-style-change').removeClass('h-products-list').addClass('big-products-list');
                displaymode = 1;
            }
        });
        $('#display_list').click(function(){
            if (displaymode == 1) {
                $(this).attr('src',$(this).attr('src').replace('display_list', 'display_list_act'));
                $('#display_grid').attr('src',$('#display_grid').attr('src').replace('display_grid_act', 'display_grid'));
				$('.list-style-change').removeClass('big-products-list').addClass('h-products-list');
                displaymode = 2;
            }
        });
    }
	if($('#right_login_div').size()>0){
		$.ajax({type:'POST',beforeSend:function(){},url:webpath+'user/r-login.asp',data:'',dataType:'html',error:'',success:function(html){
				$('#right_login_div').html(html);
				
    if ($('#leftlogin').size() > 0) {
        $('#leftlogin').click(function(){
            $.ajax({
                type: 'POST',
                beforeSend: function(){
                },
                url: webpath + 'user/left_loginact.asp',
                data: $(this).closest('form').serialize(),
                dataType: 'html',
                error: '',
                success: function(html){
                    $.dialog({
                        title: 'Message',
                        time: 2,
                        id: 'loginshow',
                        content: html
                    });
                    if (html == 'Login Successful') 
                        window.location.reload();
                }
            });
        });
    }
			}});
		}
});

