function widthheightbug()
{
	jQuery.attrHooks['width']=jQuery.attrHooks['height']=null;
}
addLoadEvent(widthheightbug);
function showWindow(wintitle, url)
{
	if (wintitle == 'login')
		wintitle = "用户登录";
	else if (wintitle == 'cart')
		wintitle = "购物车";
	else if (wintitle == 'order')
		wintitle = "订单";
	else
		wintitle = "提示框";
	$.jWindow.ajax(url, {}, {title: wintitle});
}

function showFrame(wintitle, url)
{
	if (wintitle == 'explorer')
		wintitle = '提示框';
	$.jWindow.frame(url, {}, {title: wintitle});
}

function login()
{
	showWindow('login', 'index.php?app=user&mod=login&ajax=1');
}

function register()
{
	showWindow('register', 'index.php?app=user&mod=register&ajax=1');
}

function logout()
{
	$.get("index.php?app=user&mod=logout&ajax=1", {}, function(data){
		if(data == 'Success')
			window.location.href=window.location.href;
		else
			$.jWindow.remind("退出失败。");
	});
}
function addLoadEvent(func) {
  	var oldonload = window.onload;
	if (typeof window.onload != 'function')
  		window.onload = func;
  	else
  	{
    	window.onload = function() {
      		oldonload();
      		func();
    	}
  	}
}
function backToTop()
{
    var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
        .text($backToTopTxt).attr("title", $backToTopTxt).click(function() {
            $("html, body").animate({ scrollTop: 0 }, 120);
    }), $backToTopFun = function() {
        var st = $(document).scrollTop(), winh = $(window).height();
        (st > 0)? $backToTopEle.show(): $backToTopEle.hide();
        //IE6下的定位
        if (!window.XMLHttpRequest) {
            $backToTopEle.css("top", st + winh - 166);
        }
    };
    $(window).bind("scroll", $backToTopFun);
    $(function() { $backToTopFun(); });
}
addLoadEvent(backToTop);

function resizeImage(img,maxWidth,maxHeight){
	var ratio = 0;  // 缩放比例  
	var width = $(img).width();    // 图片实际宽度   
	var height = $(img).height();  // 图片实际高度     // 检查图片是否超宽   
	if(width > maxWidth)
	{       
		ratio = maxWidth / width;   // 计算缩放比例       
		$(img).css("width", maxWidth); // 设定实际显示宽度       
		height = height * ratio;    // 计算等比例缩放后的高度       
		$(img).css("height", height);  // 设定等比例缩放后的高度   
	}     // 检查图片是否超高  
	if(height > maxHeight)
	{       
		ratio = maxHeight / height; // 计算缩放比例      
		$(img).css("height", maxHeight);   // 设定实际显示高度       
		width = width * ratio;    // 计算等比例缩放后的高度       
		$(img).css("width", width);    // 设定等比例缩放后的高度
	}
}

var HomepageFavorite = {
    //设为首页
    Homepage: function () {
        if (document.all) {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage(window.location.href);
        }
        else
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e)
            {
            	try
            	{
            		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        			prefs.setCharPref('browser.startup.homepage', window.location.href);
            	}
            	catch (e)
            	{
                	alert("您的浏览器不支持该操作，请使用浏览器菜单手动设置");
           		}
        	}
        }
    }
    ,
    //加入收藏夹
    Favorite: function Favorite(sURL, sTitle) {
        try { //IE
            window.external.addFavorite(sURL, sTitle);
        }
        catch (e) {
            try {  //Firefox
                window.sidebar.addPanel(sTitle, sURL, "");
            }
            catch (e) {
                alert("您的浏览器不支持自动加入收藏，请使用浏览器菜单手动设置.");
            }
        }
    }
}
