﻿function banner(id,json,tit){
    if(typeof(id) == "undefined" || typeof(json) == "undefined"){return false};
	var id = id;
	var json = json;
    var bimg = "<a href='"+json[0].url+"'><img src='"+json[0].bimg+"' /></a>";
    var btn="";
	var imgID = 0;
    for( var i=0;i<json.length;i++){
        btn += "<li>"+(i+1)+"</li>";
    }
	$("#"+id).append(bimg);
	$("#"+id).append("<ul>"+btn+"</ul>");
	$("#"+id+" li:first-child").attr("class","selected");
    if(tit){
        $("#"+id).append("<span>"+json[0].tit+"</span>")
    }
	$("#"+id+" li").each(function(i){
		$(this).click(function(){
			_play($("#"+id+" li").index($(this)));
		});
	});
	
	
	var _emt1 = $("#"+id+" img:first-child");
	var _emt2 = document.getElementById(id).getElementsByTagName("a")[0];
	var _emt3 = $("#"+id+" img:first-child");
	var _emt4 = $("#"+id+" img:first-child");
	var _emt5 = $("#"+id+" span");
	function _play(num){
		if(typeof(num)!= "undefined"){
			imgID = num;
		}else{
			imgID < json.length-1 ? imgID++ : imgID = 0;
		}
		$("#"+id+" li").each(function(i){
			if(i != imgID){
				$(this).attr("class","");
			}else{
				$(this).attr("class","selected");
			}
		});
		_emt2.href=json[imgID].url;
	    
		_emt1.fadeTo(100,0.25,function(){
		    _emt3.attr("src",json[imgID].bimg);
	        _emt4.fadeTo(100,1);
	        if(tit){
	            _emt5.text(json[imgID].tit);
	        }
		});
	}
	setInterval(function(){_play()},4000);
}

