// JavaScript Document

$(function() {
	//表单验证
	$('form').each(function()
	{
		if ($(this).attr('sendto')) $(this).attr('action', $(this).attr('sendto'));
		$(this).validate();
	});
	$('form input[type=text].required, form input[type=password].required, form textarea.required, form select.required').after('&nbsp;<span class="waring">*</span>');
	
	//加入收藏
	$('.addfavorite').click(function() {
		AddFavorite(document.location.href, document.title);
		return false;
	});

	//设为首页
	$('.sethome').click(function() {
		SetHomepage(document.location.href);
		return false;
	});
	
	//媒体播放器
	$('.media').each(function()
	{
		$id = '_media_' + $(this).index();
		$(this).attr('id', $id);
		jwplayer($id).setup({flashplayer:'js/mediaplayer/player.swf'});
	});
	
	//导航
	$('.header .contact').hover(function()
	{
		$(this).find('> div').show();
	}, function()
	{
		$(this).find('> div').fadeOut('fast');
	});
	$('.header .contact > div').hide().accordion({
		active		: false,
		header		: '.title',
		autoheight	: false
	});
	
	//二手车
	$('.s_title, .s_year, .s_price').hide();
	$('.s_' + $('select[name=s[type]]').val()).show();
	$('select[name=s[type]]').change(function() {
		$('.s_title, .s_year, .s_price').hide();
		$('.s_' + $('select[name=s[type]]').val()).show();
	});
});

//加入收藏
function AddFavorite(sURL, sTitle) {
    try {
        window.external.AddFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, '');
        }
        catch (e) {
            alert('请使用 "Ctrl+D" 收藏本页');
        }
    }
}
//设为首页
function SetHomepage(sURL) {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(sURL);
	} else if (window.sidebar) {
		if (window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage', sURL);
			} 
			catch (e) {
				alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config ，然后将项 signed.applets.codebase_principal_support 值该为 true ！");
			} 
		} 
	} 
}

/*
 * 引用js文件
 * usage : js('path/file.js');
 */
function js(path)
{
	if (fileExists(path))
	{
		var a = document.createElement("script");
		a.type = "text/javascript";
		a.src = path;
		var head = document.getElementsByTagName("head")[0];
		head.appendChild(a);
	}
	else
	{
		alert('The file "' + path + '" dose not exists!')
	}
}

/*
 * 判断文件是否存在
 * usage : return = fileExists('path/file.ext');
 */
 function fileExists(file)
{
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		if(xmlhttp.overrideMimeType)
		{
			xmlhttp.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e2)
			{
				xmlhttp = false;
			}
		}
	}
	xmlhttp.open("GET",file,false);
	xmlhttp.send(null);
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//生成随机数
function getRandom(seeds)
{
	if (!seeds) seeds = 10000;
	return Math.round(Math.random()*seeds);
}
