﻿$(function(){
	//$("#brandImages").css({visiblity:"hidden"})
	$.ajax({
		type: "GET",
		url: "/common/xml/top/brand_area.xml",
		dataType: "xml",
		success:$.brandImage
	});
});
$.extend({
  len:0,
  count:0,
  timer:0,
  timerChick:function(time){
	  $.timer = setTimeout(function(){$(window).trigger("onChange",[1])},time)
  },
  defaultTime:8000,
  brandImage:function (xml) {
	  $.len =$(xml).find("image").length -1;
	  $(xml).find("image").each(function(i){
		  $("#brandImages").append('<li id="brandImage'+i+'"></li>');
		  var path = $(this).find("src").text();
		  $("#brandImage"+i).append('<img src='+path+' />').css({marginTop:"27px",position:"absolute","zIndex":i,display:"none"});
	  });
	  $("#brandImageButtons")
	  .find("li:first").bind("click",function(){$(window).trigger("onChange",[-1])})
	  .hover(function(){
		  var nowsrc = $(this).find("img").attr("src").replace(".gif","_on.gif");
		  $(this).find("img").attr({"src":nowsrc})
	   },
	   function(){
		  var nowsrc = $(this).find("img").attr("src").replace("_on.gif",".gif");
		  $(this).find("img").attr({"src":nowsrc})
	   })
	  .end()
	  .find("li:last").bind("click",function(){
		  $(window).trigger("onChange",[1]);
	   })
	  .hover(function(){
		  var nowsrc = $(this).find("img").attr("src").replace(".gif","_on.gif");
		  $(this).find("img").attr({"src":nowsrc})
	   },
	   function(){
		  var nowsrc = $(this).find("img").attr("src").replace("_on.gif",".gif");
		  $(this).find("img").attr({"src":nowsrc})
	   })
	   .end()
	   $("#brandImage0,#brandImageButtons").fadeIn("slow");
	   $(window).bind("onChange",function(e,value){
		   clearTimeout($.timer);
		   var oldCount = $.count;
		   if(value>0){
		     if($.count == $.len){
			     $.count =0;
		     }else{
		       $.count++;
		     }
		   }else{
		     if($.count == 0){
			     $.count =$.len;
		     }else{
		       $.count--;
		     }
		    }
		   $("#brandImage"+oldCount).fadeOut("slow");
		   $("#brandImage"+$.count).fadeIn("slow");
    	   $.timerChick($.defaultTime);		   
	   });
	   $.timerChick($.defaultTime);
  }
})

