// JavaScript Document
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('"抱歉！您的浏览器不支持直接设为首页。请在浏览器地址栏输入"about:config"并回车然后将[signed.applets.codebase_principal_support]设置为"true"，点击"加入收藏"后忽略安全提示，即可设置成功。"');
                }
                var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                prefs.setCharPref('browser.startup.homepage', vrl);
            }
        }
    },
    lightbox: function(){
        $.getScript(webpath + 'js/jquery.include.js', function(){
            var files = [webpath + 'css/jquery.lightbox-0.5.css', webpath + 'js/jquery.lightbox-0.5.pack.js'];
            $.include(files, function(){
                if ($('.lightbox img').size() > 0) {
                    var maxwidth = $('.lightbox').attr('maxwidth');
                    $('.lightbox img').width() > maxwidth ? $('.lightbox img').width(maxwidth) : $('.lightbox img').width($('.lightbox img').width());
                    $('.lightbox img').css('cursor', 'pointer').lightBox();
                }
            });
        });
    },
    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'
					});
				});
    }
};
$(function(){
    common.resize();
	if($('.main-left-products-list h1:first'))
    $('.main-left-products-list h1:first').next('ul').show();
    $('.main-left-products-list h1').each(function(){
        $(this).hover(function(){
            $(this).siblings().next('ul').slideUp('fast');
            $(this).next('ul').slideDown('fast');
        },function(){});
    });
	//$('img').lazyload({threshold:4});
});
