function getFileName(className){
	var ImageFile = "";
	switch (className){
		case "tab02":
			ImageFile = "carname";
			break;
		case "tab03":
			ImageFile = "price";
			break;
		default:
			ImageFile = "bodytype";
			break;
	}
	return ImageFile;
}

function changeTab(tab){
	$(".searchContainer").hide();
	$("#contents_tab01").hide();
	$("#contents_tab02").hide();
	$("#contents_tab03").hide();
	$(".contents_tab01").hide();
	$(".contents_tab02").hide();
	$(".contents_tab03").hide();
	$(".selected").removeClass("selected");
	$(".tab01").find("img").attr("src", "/CARSINDEALER/IMAGES/tab_bodytype_o.gif");
	$(".tab02").find("img").attr("src", "/CARSINDEALER/IMAGES/tab_carname_o.gif");
	$(".tab03").find("img").attr("src", "/CARSINDEALER/IMAGES/tab_price_o.gif");


	$("#contents_"+tab.attr("class")).show();
	$(".contents_"+tab.attr("class")).show();
	var ImageFile = getFileName(tab.attr("class"));
	tab.children("img").attr("src", "/CARSINDEALER/IMAGES/tab_"+ImageFile+"_h.gif");
	tab.addClass("selected");

	$(".tabList").children("li").hover(
			function(){
				if(!$(this).hasClass("selected")){
					var ImageFile = getFileName($(this).attr("class"));
					$(this).find("img").attr("src", "/CARSINDEALER/IMAGES/tab_"+ImageFile+"_h.gif");
				}
			},
			function(){
				if(!$(this).hasClass("selected")){
					var ImageFile = getFileName($(this).attr("class"));
					$(this).find("img").attr("src", "/CARSINDEALER/IMAGES/tab_"+ImageFile+"_o.gif");
				}
			}
	);
	return false;
}


function clickCarBox(button){
	var carBox = button.parent().parent().parent().parent();
	if(carBox.hasClass("carBox02")){
		carBox.addClass("carBox03");
		carBox.removeClass("carBox02");
		button.parent().find(".searchBtn02_List").show();
		button.parent().parent().parent().css("background","url(/CARSINDEALER/IMAGES/line_select.gif) no-repeat left top").css("height","300px");
		//button.css("background","url(/CARSINDEALER/IMAGES/srch_dtl_h.gif) no-repeat left top");
		button.addClass("btn_on");
	} else {
		carBox.addClass("carBox02");
		carBox.removeClass("carBox03");
		button.parent().find(".searchBtn02_List").hide();
		button.parent().parent().parent().css("background","url(/CARSINDEALER/IMAGES/line_click.gif) no-repeat left top").css("height","199px");
		button.removeClass("btn_on");
	}
}

function closeSearchContainer(CarBox){
	CarBox.addClass("carBox01");
	CarBox.removeClass("carBox02");
	CarBox.removeClass("carBox03");
	CarBox.find(".searchContainer").hide();
	CarBox.css("z-index" , "0");
	CarBox.find(".srchBtn02_a").removeClass("btn_on");
}


$(window).load(function () {
	$(".carBox01").hover(
		  function () {
			  if($(this).hasClass("carBox01")){
				  $(this).append("<p class=\"carLine1\"><img src=\"/CARSINDEALER/IMAGES/carBox01_line.gif\" alt=\"\" width=\"109\" height=\"95\" /></p>");
			  }
		  },
		  function () {
			  $(this).find("p.carLine1").remove();
		  }
	);

	$(".carLink").hover(
		  function () {
			  $(this).append("<p class=\"carLine1\"><img src=\"/CARSINDEALER/IMAGES/carBox01_line.gif\" alt=\"\" width=\"109\" height=\"95\" /></p>");
			  if( $(this).children('a').attr("href") != "undefined" && $(this).children('a').attr("href")){
				  var link = "<a href=\""+ $(this).children('a').attr("href")+"\" target=\""+ $(this).children('a').attr("target")+"\"></a>" ;
				  $(this).find('.carLine1').wrapInner(link);
			  }
		  },
		  function () {
			  $(this).find("p.carLine1").remove();
		  }
	);

	$(".carBox01").live("click", function () {
		$(".carBox02").each(function(){ closeSearchContainer($(this)); });
		$(".carBox03").each(function(){ closeSearchContainer($(this)); });
		$(this).find("p.carLine1").remove();
		if($(this).find(".searchBox").children("li").length < 4){
			$(this).addClass("carBox02");
			$(this).removeClass("carBox01");
			$(this).find(".searchContainer").css("background","url(/CARSINDEALER/IMAGES/line_click.gif) no-repeat left top").css("height","199px").show();
		}else{
			$(this).addClass("carBox03");
			$(this).removeClass("carBox01");
			$(this).find(".searchContainer").css("background","url(/CARSINDEALER/IMAGES/line_select.gif) no-repeat left top").css("height","300px").show();
		}
		$(this).css("z-index" , "2");
		$(this).find(".searchContainer").show();
		$(this).find(".searchBtn02_List").hide();
		$(this).find(".srchClose").click(function(){
			var CarBox = $(this).parent().parent();
			closeSearchContainer(CarBox);
			return false;
		});
	});

	$("#contents_tab01 .srchBtn02_a").live("click", function () {
		clickCarBox($(this));
		return false;
	});
	$("#contents_tab02 .srchBtn02_a").live("click", function () {
		clickCarBox($(this));
		return false;
	});
	$("#contents_tab03 .srchBtn02_a").live("click", function () {
		$(this).parent().find(".searchBtn02_List").toggle();
		return false;
	});

	$(".carName").live("click", function () {
		$(this).css('position' , "relative");
		var showSearch = true;
		if($(this).hasClass("selected")){
			showSearch = false;
		}
		$(".carName.selected").each(function(){
			var imgOff = $(this).find('img').attr('src').replace(/_select.gif$/g, "_off.gif");
			$(this).find('img').attr('src', imgOff);
			$(this).removeClass("selected");
			$(this).parent().find(".searchContainer").hide();
			$(this).css("z-index" , "0");
		});
		if(showSearch){
			var imgOn2 = $(this).find('img').attr('src').replace(/_off.gif$/g, "_on.gif");
			imgOn2 = imgOn2.replace(/_on.gif$/g, "_select.gif");
			$(this).find('img').attr('src', imgOn2);
			$(this).addClass("selected");
			$(this).css("z-index" , "5");
			$(this).parent().find(".searchContainer").show();
			$(this).parent().find(".searchBtn02_List").hide();
		}
	});

	getPageHeight($("#contents_tab01"));
	getPageHeight($("#contents_tab02"));
	getPricePageHeight($("#contents_tab03"));
	changeTab($(".tab01"));
	$(".tabList > li").live("click",function(){
		changeTab($(this));
	});
});

$(function(){
	$.ajax({
		type: "GET",
		url: "/GENERAL/XML/data.xml",
		async: true,
		cache: false,
		dataType:"xml",
		success: function(xml){
			setLinkContents(xml);
		}
	});
})

