//
// create closure
//
(function($) {
	var $this;

	//
	// plugin definition
	//
	$.fn.extend({
		
		bannerband: function(options) { 
			$.bband_pv.opt = $.extend({},$.bannerbandSetup.defaults, options);		// extend options
			$.bband_pv.view_width = $.bband_pv.opt.width - $.bband_pv.opt.scrbtnwidth * 2;
			$this = $(this); 
			bannerdata = null;
			new $.bannerbandSetup();
		}
	});

	//
	// private function for debugging
	//
	function debug(msg) {
		if (window.console && window.console.log)
		window.console.log(msg);
		window.status = msg.srcElement;
	};

	//
	// plugin setup
	//
	$.bannerbandSetup = function() {
//		debug($.bband_pv.opt.data);
		// left scroll button
//		debug($this);
		$this.css({
			width: $.bband_pv.opt.width+"px",
			height: $.bband_pv.opt.height+"px",
			overflow: "hidden",
			position: "relative"
		});
		var lbtn = document.createElement("div");
		$(lbtn).css({
			"position": "absolute",
			"z-index": 100,
			"left": "0px",
			"top":  "0px",
			"width": $.bband_pv.opt.scrbtnwidth+"px",
			"height": $.bband_pv.opt.scrbtnheight+"px",
			"background": "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.lbtn_img_d+") no-repeat",
			"opacity": $.bband_pv.opt.btn_opacity
		})
		.attr({
			"id": $this.attr("id")+"_scrbtnl",
			"class": "scrbtnl_dimm"})
		.hover(
			function() {
				switch ($.bband_pv.move_dir) {
				case "nomove":
				case "leftf":
					return;
				default:
					$.bband_pv.move_dir = "leftf";
					$(this).css("background", "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.lbtn_img_o+") no-repeat");
					break;
				}
			},
			function() {
				switch ($.bband_pv.move_dir) {
				case "nomove":
					return;
				default:
					$.bband_pv.move_dir = "left";
					$(this).css("background", "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.lbtn_img+") no-repeat");
					break;
				}
			}
		);
		$this.append($(lbtn));
		var rbtn = document.createElement("div");
		$(rbtn).css({
			"position": "absolute",
			"z-index": 100,
			"left": ($.bband_pv.opt.width-$.bband_pv.opt.scrbtnwidth)+"px",
			"top": "0px",
			"width": $.bband_pv.opt.scrbtnwidth+"px",
			"height": $.bband_pv.opt.scrbtnheight+"px",
			"background": "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.rbtn_img_d+") no-repeat",
			"opacity": $.bband_pv.opt.btn_opacity
		})
		.attr({
			"id":  $this.attr("id")+"_scrbtnr",
			"class": "scrbtnl_dimm"})
		.hover(
			function() {
				switch ($.bband_pv.move_dir) {
				case "nomove":
				case "rightf":
					return;
				default:
					$.bband_pv.move_dir = "rightf";
					$(this).css("background", "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.rbtn_img_o+") no-repeat");
					break;
				}
			},
			function() {
				switch ($.bband_pv.move_dir) {
				case "nomove":
					return;
				default:
					$.bband_pv.move_dir = "left";
					$(this).css("background", "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.rbtn_img+") no-repeat");
					break;
				}
			}
		);
		$this.append($(rbtn));
		var $div_tip = $(document.createElement("div"));
		var $div_tip_bk = $(document.createElement("div"));
		$div_tip_bk.css({
			'position': 'absolute',
			'left': '0px',
			'top': '0px',
			'border': '1px solid #666666',
			'background-color': '#000000',
			'width': '1px',
			'height': $.bband_pv.opt.tip_height+'px',
			'opacity': 0.75,
			'z-index': -10
		});
		$div_tip.append($div_tip_bk);
		$div_tip.attr({
			'id': 'bannerband_tip'
		}).css({
			'position': 'absolute',
			'display': 'none',
			'padding': '4px',
			'width': $.bband_pv.opt.tip_width+'px',
			'height': $.bband_pv.opt.tip_height+'px',
			'overflow': 'hidden',
			'z-index': 9999
		});
		var $span_tip = $(document.createElement("span"));
		$span_tip.css({
			'color': '#FFFFFF',
			'font-size': '12px',
			'background': 'url('+$.bband_pv.opt.skindir+$.bband_pv.opt.tip_icon+') no-repeat 8px 4px',
			'padding': '4px 4px 4px 28px',
			'display': 'block',
			'z-index': 10
		});
		$div_tip.append($span_tip);
		$(document.body).append($div_tip);
		var jsonurl = $.bband_pv.opt.data;
		if (jsonurl.match(/\.js$/)) {
			var now = new Date();
			jsonurl += '?'+now.getTime();
		}
		$.getJSON(
			jsonurl,
			function(data){
				bannerdata = data;
				var band = document.createElement("div");
				$.bband_pv.band = $(band);
				$(band).css({
					position: "absolute",
					left: "0px",
					top: parseInt(($.bband_pv.opt.height-$.bband_pv.opt.imgheight)/2,10)+"px",
					"z-index": 0,
					visibility: "visible",
					border: "0",
					overflow: "hidden",
					width: $.bband_pv.opt.width+"px",
					height: $.bband_pv.opt.imgheight+"px"
				}).addClass("band").hover(
					function(e) {
						switch ($.bband_pv.move_dir) {
						case "nomove":
						case "pause":
							return;
						default:
							$.bband_pv.move_dir = "pause";
							break;
						}
					},
					function() {
						switch ($.bband_pv.move_dir) {
						case "nomove":
							return;
						default:
							$.bband_pv.move_dir = "left";
							break;
						}
					}
				);
				$.bband_pv.item_count = bannerdata.banners.length;
				$.bband_pv.item_loaded = 0;
				if (bannerdata.banners.length) {
					$this.css({height:'0px', display:'block'});
				} else {
					return;
				}
				$this.append($(band));
				$.bannerbandSetupSub(band);
			}
		);
	};

	$.bannerbandSetupSub = function(band) {
		$.each(bannerdata.banners, function(i,item){
			var alt = unescape(item.alt);
			if (alt.length > 50) {
				alt = alt.substr(0, 50)+'...';
			}
			var $img_box = $(document.createElement("div"));
			var h = item.height ? item.height : $.bband_pv.opt.imgheight;
			var w = item.width ? item.width : $.bband_pv.opt.imgwidth;
			var ibw = 0;
			var ibh = 0;
			if (h > $.bband_pv.opt.imgheight) {
				var m = $.bband_pv.opt.imgheight / h;
				ibw = parseInt(w * m, 10);
				ibh = $.bband_pv.opt.imgheight;
			} else {
				ibw = w;
				ibh = h;
			}
			if ($.bband_pv.mmaxwidth < w) {
				$.bband_pv.mmaxwidth = w;
			}
			var ibl;
			if ($.bband_pv.total_width == 0) {
				$.bband_pv.first_left = ibl = parseInt($.bband_pv.opt.scrbtnwidth, 10) + parseInt($.bband_pv.opt.cellspacing, 10);
			} else {
				ibl = $.bband_pv.first_left + $.bband_pv.total_width;
			}
			var ibt = parseInt(($.bband_pv.opt.imgheight-ibh)/2, 10);

			$img_box.css({
				position: "absolute",
				left: ibl+"px",
				top: ibt+"px",
				width: ibw+"px",
				height: ibh+"px",
				overflow:"hidden"
			}).attr("class", "img_box");
			$(band).append($img_box);
			$.bband_pv.total_width += parseInt($.bband_pv.opt.cellspacing, 10)+ibw;

			$.bband_pv.blist.push({
				e: $img_box.get(0),
				lmin: -ibw,
				lmax: -ibw,
				x: ibl
			});

			$.bband_pv.item_count++;

			if (item.type == "img") {
				var a = document.createElement("a");
				$(a).css({
					display: "block",
					position: "absolute"
				});
				var img = document.createElement("img");
				$(img).addClass("banner_img")
				.attr("name", "bannerbanditem_"+$.bband_pv.item_count)
				.bind("load", this, function(e) {
					$(this).unbind('load');
					$(this).css({width:ibw+"px",height:ibh+"px"});
				});
				$img_box.append($(a));
				$(a).append($(img));
				$(img).attr("src", item.src).css({width:ibw+"px",height:ibh+"px"});

				if (item.link) {
					if ($.bband_pv.opt.counter) {
						if (item.target) {
							$(a).attr({
								'href': item.link,
								'target': item.target
							}).click(function() {
								$.bn_countup($.bband_pv.opt.counter,item.mid);
								return true;
							});
						} else {
							$(a).attr({
								'href': 'javascript:void(0)'
							}).click(function() {
								$.bn_countup2($.bband_pv.opt.counter,item.mid,item.link,item.target);
								return true;
							});
						}
					} else {
						$(a).attr({
							'href': item.link,
							'target': item.target
						});
					}
				}
				var $p = $(document.createElement("p"));
				$(a).append($p);
				$p.css({
					'display': 'none'
				}).text(alt);
			} else if (item.type == "swf") {
				var $dmydiv = $(document.createElement("div"));
				$dmydiv.css({
					'position': 'relative'
				});
				$img_box.css({
					"position": "absolute"
				}).append($dmydiv);
				var swf_w = item.width ? item.width : $.bband_pv.opt.imgwidth;
				var swf_h = item.height ? item.height : $.bband_pv.opt.imgheight;
				$img_box.css({
					width: swf_w+"px",
					height: swf_h+"px"
				});
				var html = TH_AC_FL_GenerateContent(
					'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+$.bband_pv.opt.flash_ver,
					'width', swf_w,
					'height', swf_h,
					'src', item.src,
					'quality', 'high',
					'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
					'align', 'middle',
					'play', 'true',
					'loop', 'true',
					'scale', 'showall',
					'wmode', 'transparent',
					'devicefont', 'false',
					'menu', 'true',
					'allowFullScreen', 'false',
					'allowScriptAccess','sameDomain',
					'movie', item.src,
					'salign', '',
					'class', 'banner_img',
					'name', 'bannerbanditem_'+i
					);
				$dmydiv.html(html);
				var $swfmask = $(document.createElement("div"));
				$swfmask.css({
					'position': 'absolute',
					'left': '0px',
					'top': '0px',
					'width': swf_w+"px",
					'height': swf_h+"px"
				});
				var a = $(document.createElement("a"));
				if (item.link) {
					if ($.bband_pv.opt.counter) {
						if (item.target) {
							$(a).attr({
								'href': item.link,
								'target': item.target
							}).click(function() {
								$.bn_countup($.bband_pv.opt.counter,item.mid);
								return true;
							});
						} else {
							$(a).attr({
								'href': 'javascript:void(0)'
							}).click(function() {
								$.bn_countup2($.bband_pv.opt.counter,item.mid,item.link,item.target);
								return true;
							});
						}
					} else {
						$(a).attr({
							'href': item.link,
							'target': item.target
						});
					}
				}
				var $dmyimg = $(document.createElement("img"));
				$dmyimg.attr("src", $.bband_pv.opt.skindir+$.bband_pv.opt.spacer).css({
					'width': swf_w,
					'height': swf_h
				});
				$(a).append($dmyimg);
				$swfmask.append($(a));
				var $p = $(document.createElement("p"));
				$swfmask.append($p);
				$p.css({
					'display': 'none'
				}).text(alt);
				$dmydiv.append($swfmask);
				if ($.bband_pv.mmaxwidth < swf_w) {
					$.bband_pv.mmaxwidth = swf_w;
				}
			}
			$img_box.hover(
				function(evt) {
					var $div_tip = $('#bannerband_tip');
					var $div_tip_bk = $div_tip.children('div');
					var $elem = $(this).find('.banner_img');
					var idx = parseInt($elem.attr('name').split(/_/)[1], 10);
					if ($.bband_pv.cur_tip != idx) {
						$.bband_pv.cur_tip = idx;
						$div_tip.css({
							'display': 'none'
						});
						$div_tip_bk.css({
							'width': '1px'
						});
						var alt = $elem.next().text();
						if (alt != '') {
							var $span_tip = $div_tip.find('span');
							$span_tip.text(alt).css('visibility', 'hidden');
							var off = $this.offset();
							if (($.bband_pv.opt.width-(evt.pageX-off.left)-16)<$.bband_pv.opt.tip_width) {
								$div_tip.css({
									'left': (evt.pageX-$.bband_pv.opt.tip_width-16)+'px',
									'top': evt.pageY+'px',
									'display': 'block'
								});
								$div_tip_bk.css({
									'left':$.bband_pv.opt.tip_width+'px'
								}).animate({'left':'0px', 'width':$.bband_pv.opt.tip_width+'px'}, 300, 'swing', function() {
									$(this).parent().find('span').css('visibility', 'visible');
								});
							} else {
								$div_tip.css({
									'left': (evt.pageX+16)+'px',
									'top': evt.pageY+'px',
									'display': 'block'
								});
								$div_tip_bk.animate({'width':$.bband_pv.opt.tip_width+'px'}, 300, 'swing', function() {
									$(this).parent().find('span').css('visibility', 'visible');
								});
							}
						}
					} else {
						// todo : move tip
					}
				},
				function() {
					var $div_tip = $('#bannerband_tip');
					$div_tip.css('display', 'none');
					$.bband_pv.cur_tip = -1;
				}
			).mousemove(function(evt) {
				var $div_tip = $('#bannerband_tip');
				if ($div_tip.css('display') == 'block') {
					var off = $this.offset();
					var off = $this.offset();
					if (($.bband_pv.opt.width-(evt.pageX-off.left)-16)<$.bband_pv.opt.tip_width) {
						$div_tip.css({
							'left': (evt.pageX-$.bband_pv.opt.tip_width-16)+'px',
							'top': evt.pageY+'px',
							'display': 'block'
						});
					} else {
						$div_tip.css({
							'left': (evt.pageX+16)+'px',
							'top': evt.pageY+'px',
							'display': 'block'
						});
					}
				}
			});
		});

		if ($.bband_pv.total_width <= $.bband_pv.view_width) {
			$.bband_pv.move_dir = "nomove";
			$.bannerbandStart();
		} else if (($.bband_pv.total_width - $.bband_pv.mmaxwidth) < $.bband_pv.opt.width) {
			$.bannerbandSetupSub(band);
		} else {
			$.bband_pv.move_dir = "left";
			$.bannerbandStart();
		}
		
		
	};
	//
	// plugin defaults
	//
	$.bannerbandSetup.defaults = {
		data: '',
		width: 930,
		height: 58,
		cellspacing: 4,
		imgwidth: 218,
		imgheight: 50,
		scrbtnwidth: 19,
		scrbtnheight: 58,
		spd_normal: 2,
		spd_fast: 8,
		timer_interval: 60,
		flash_ver:"9,0,0,0",
		skindir: "images/",
		lbtn_img:"scr_l_btn.gif",
		lbtn_img_o:"scr_l_btn_o.gif",
		lbtn_img_d:"scr_l_btn_d.gif",
		rbtn_img:"scr_r_btn.gif",
		rbtn_img_o:"scr_r_btn_o.gif",
		rbtn_img_d:"scr_r_btn_d.gif",
		btn_opacity: "0.6",
		tip_icon:"redball.gif",
		tip_width: 320,
		tip_height: 40,
		spacer:"space.gif",
		counter: ''
	};
	$.bband_pv = {
		timer_id: 0,
		move_dir: "left",
		total_width: 0,
		item_count: 0,
		item_loaded: 0,
		view_width: 0,
		bannerdata: null,
		opt: null,
		cur_tip: -1,
		blist: [],
		band: null,
		mmaxwidth: 0,
		first_left: 0
	};
	//
	// plugin start scroll timer
	//
	$.bannerbandStart = function() {
		var i;
		var v = $.bband_pv.total_width + parseInt($.bband_pv.opt.scrbtnwidth, 10);
		for (i = 0; i < $.bband_pv.blist.length; i++) {
			$.bband_pv.blist[i].lmax+=$.bband_pv.total_width;
		}

		if ($.bband_pv.move_dir == "nomove") {
			$.bband_pv.band.css({
				visibility: "visible"
			});
			$this.animate({
				height: $.bband_pv.opt.height+"px"
			}, 500, 'swing');
			return;
		}

		$this.find("#"+$this.attr("id")+"_scrbtnl").css("background", "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.lbtn_img+") no-repeat");
		$this.find("#"+$this.attr("id")+"_scrbtnr").css("background", "url("+$.bband_pv.opt.skindir+$.bband_pv.opt.rbtn_img+") no-repeat");

		$.bband_pv.band.css({
			visibility: "visible"
		});

		$this.animate({
			height: $.bband_pv.opt.height+"px"
		}, 500, 'swing', function() {
			$.bband_pv.timer_id = setInterval(function() {
				var move_size = 0;
				switch ($.bband_pv.move_dir) {
					case "pause":
						return;
					case "left":
						move_size = - $.bband_pv.opt.spd_normal;
						break;
					case "right":
						move_size = $.bband_pv.opt.spd_normal;
						break;
					case "leftf":
						move_size = - $.bband_pv.opt.spd_fast;
						break;
					case "rightf":
						move_size = $.bband_pv.opt.spd_fast;
						break;
					default:
						return;
				}

				var i;
				if (move_size < 0) {
					for (i = 0; i < $.bband_pv.blist.length; i++) {
						$.bband_pv.blist[i].x += move_size;
						if ($.bband_pv.blist[i].x < $.bband_pv.blist[i].lmin) {//alert($.bband_pv.total_width);
							$.bband_pv.blist[i].x += $.bband_pv.total_width;
						}
						$.bband_pv.blist[i].e.style.left = $.bband_pv.blist[i].x+"px";
	//					$.bband_pv.blist[i].e.css('left', $.bband_pv.blist[i].x+"px");
					}
				} else {
					for (i = 0; i < $.bband_pv.blist.length; i++) {
						$.bband_pv.blist[i].x += move_size;
						if ($.bband_pv.blist[i].x > $.bband_pv.blist[i].lmax) {
							$.bband_pv.blist[i].x -= $.bband_pv.total_width;
						}
						$.bband_pv.blist[i].e.style.left = $.bband_pv.blist[i].x+"px";
	//					$.bband_pv.blist[i].e.css('left', $.bband_pv.blist[i].x+"px");
					}
				}
			}, $.bband_pv.opt.timer_interval);
		});
		
	};
	
	$.bn_countup = function(pg,mid) {
		(new Image()).src=pg+mid;
		return true;
	};

	$.bn_countup2 = function(pg,mid,l,t) {
		$(document.createElement("img")).load(function(){$(this).unbind('load');window.open(l, t?t:'_self');}).attr("src", pg+mid);
		return true;
	};

//
// end of closure
//
})(jQuery);


