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

	//
	// plugin definition
	//
	$.fn.extend({
		
		mainbtnaction: function(options) { 

			$.ma_pv.opt = $.extend({},$.mainbtnactionSetup.defaults, options);		// extend options
			$this = $(this);
			
			new $.mainbtnactionSetup();
		}
	});

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

	//
	// plugin setup
	//
	$.mainbtnactionSetup = function() {
		$this.css({
			"width": $.ma_pv.opt.width+"px",
			"height": $.ma_pv.opt.height+"px",
			"overflow": "hidden",
			"background": "transparent",
			"z-index": 100
		});

		var i;
		var data = $.ma_pv.opt.data;
		$.each(data, function(i, item) {
			var imgbox = document.createElement("div");
			$(imgbox).css({
				"position": "absolute",
				"left": $.ma_pv.opt.width+"px",
				"top": "0px",
				"z-index": "100",
				"visibility": "visible",
				"border": "0",
				"background-color": "#000000",
				"overflow": "hidden",
				"width": $.ma_pv.opt.width+"px",
				"height": $.ma_pv.opt.height+"px"
			}).attr({
				"id": $.ma_pv.opt.prefix+item.no+'_img'
			}).hover(
				function() {
					var j;
					for (j = 0; j < $.ma_pv.opt.data.length; j++) {
						if ($.ma_pv.opt.data[j].hide_timer_id != 0) {
							clearTimeout($.ma_pv.opt.data[j].hide_timer_id);
							$.ma_pv.opt.data[j].hide_timer_id = 0;
						}
					}
				},
				function () {
					var j;
					for (j = 0; j < $.ma_pv.opt.data.length; j++) {
						if ($.ma_pv.opt.data[j].element.attr('id') == $(this).attr('id')) {
							$.ma_pv.opt.data[j].element.stop();
							$.ma_pv.opt.data[j].hide_timer_id = window.setTimeout(
								function() {
									var k;
									for (k = 0; k < $.ma_pv.opt.data.length; k++) {
										var l = $.ma_pv.opt.data[k].element.css('left');
										if (l != ($.ma_pv.opt.width+'px')) {
											$.ma_pv.opt.data[k].element.css({
												'z-index': 100
											}).animate({'left': $.ma_pv.opt.width+'px'}, 500, 'swing', function() {
												$(this).css({'z-index': 100});
											});
										}
										$.ma_pv.opt.data[k].hide_timer_id = 0;
									}
								}
								,1000);
						}
					}
				}
			);

			$this.append($(imgbox));
			if (item.imgtype == 'swf') {
				var html = TH_AC_FL_GenerateContent(
					'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+$.ma_pv.opt.flash_ver,
					'width', $.ma_pv.opt.width,
					'height', $.ma_pv.opt.height,
					'src', item.imgurl,
					'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.imgurl,
					'salign', ''
					);
				$(imgbox).html(html);
				if (item.img_link_url) {
					var $a = $(document.createElement("a"));
					var $dmyimg = $(document.createElement("img"));
					$dmyimg.attr({
						'src': $.ma_pv.opt.spaceimg
					}).css({
						'width': $.ma_pv.opt.width+'px',
						'height': $.ma_pv.opt.height+'px',
						'border': '0'
					});
					$a.append($dmyimg);
					$a.css({
						'position': 'absolute',
						'left': '0px',
						'top': '0px',
						'width': $.ma_pv.opt.width+'px',
						'height': $.ma_pv.opt.height+'px'
					});
					if ($.ma_pv.opt.counter) {
						if (item.img_link_target) {
							$a.attr({
								'href': item.img_link_url,
								'target': item.img_link_target
							}).click(function() {
								$.main_btn_countup($.ma_pv.opt.counter,item.no);
								return true;
							});
						} else {
							$a.attr({
								'href': 'javascript:void(0)'
							}).click(function() {
								$.main_btn_countup2($.ma_pv.opt.counter,item.no,item.img_link_url,item.img_link_target);
								return true;
							});
						}
					} else {
						$a.attr({
							'href': item.img_link_url,
							'target': item.img_link_target
						});
					}
					$(imgbox).append($a);
				}
			} else {
				var f = $.mainbtnaction_calcfitsize($.ma_pv.opt.width, $.ma_pv.opt.height, item.imgwidth, item.imgheight);
				var $a = $(document.createElement("a"));
				$a.css({
					position: 'absolute',
					left: f.left+'px',
					top: f.top+'px',
					width: f.width+'px',
					height: f.height+'px'
				});
				var img = document.createElement("img");
				$(img).css({
					width: f.width+'px',
					height: f.height+'px'
				});
				$(imgbox).append($a);
				$(img).attr("src", item.imgurl);
				$a.append($(img));
				if (item.img_link_url) {
					if ($.ma_pv.opt.counter) {
						if (item.img_link_target) {
							$a.attr({
								'href': item.img_link_url,
								'target': item.img_link_target
							}).click(function() {
								$.main_btn_countup($.ma_pv.opt.counter,item.no);
								return true;
							});
						} else {
							$a.attr({
								'href': 'javascript:void(0)'
							}).click(function() {
								$.main_btn_countup2($.ma_pv.opt.counter,item.no,item.img_link_url,item.img_link_target);
								return true;
							});
						}
					} else {
						$a.attr({
							'href': item.img_link_url,
							'target': item.img_link_target
						});
					}
				}
			}

			$.ma_pv.opt.data[i].element = $(imgbox);






/*
			var imgbox = document.createElement("div");
			$(imgbox).css({
				"position": "absolute",
				"left": $.ma_pv.opt.width+"px",
				"top": "0px",
				"z-index": "100",
				"visibility": "visible",
				"border": "0",
				"background-color": "#000000",
				"overflow": "hidden",
				"width": $.ma_pv.opt.width+"px",
				"height": $.ma_pv.opt.height+"px"
			}).attr({
				"id": $.ma_pv.opt.prefix+item.no+'_img'
			}).hover(
				function() {
					var j;
					for (j = 0; j < $.ma_pv.opt.data.length; j++) {
						if ($.ma_pv.opt.data[j].hide_timer_id != 0) {
							clearTimeout($.ma_pv.opt.data[j].hide_timer_id);
							$.ma_pv.opt.data[j].hide_timer_id = 0;
						}
					}
				},
				function () {
					var j;
					for (j = 0; j < $.ma_pv.opt.data.length; j++) {
						if ($.ma_pv.opt.data[j].element.attr('id') == $(this).attr('id')) {
							$.ma_pv.opt.data[j].element.stop();
							$.ma_pv.opt.data[j].hide_timer_id = window.setTimeout(
								function() {
									var k;
									for (k = 0; k < $.ma_pv.opt.data.length; k++) {
										var l = $.ma_pv.opt.data[k].element.css('left');
										if (l != ($.ma_pv.opt.width+'px')) {
											$.ma_pv.opt.data[k].element.css({
												'z-index': 100
											}).animate({'left': $.ma_pv.opt.width+'px'}, 500, 'swing', function() {
												$(this).css({'z-index': 100});
											});
										}
										$.ma_pv.opt.data[k].hide_timer_id = 0;
									}
								}
								,1000);
						}
					}
				}
			);
		
			$this.append($(imgbox));
			if (item.imgtype == 'swf') {
				var html = TH_AC_FL_GenerateContent(
					'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+$.ma_pv.opt.flash_ver,
					'width', $.ma_pv.opt.width,
					'height', $.ma_pv.opt.height,
					'src', item.imgurl,
					'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.imgurl,
					'salign', ''
					);
				$(imgbox).html(html);
				if (item.img_link_url) {
					var $a = $(document.createElement("a"));
					var $dmyimg = $(document.createElement("img"));
					$dmyimg.attr({
						'src': $.ma_pv.opt.spaceimg
					}).css({
						'width': $.ma_pv.opt.width+'px',
						'height': $.ma_pv.opt.height+'px',
						'border': '0'
					});
					$a.append($dmyimg);
					$a.css({
						'position': 'absolute',
						'left': '0px',
						'top': '0px',
						'width': $.ma_pv.opt.width+'px',
						'height': $.ma_pv.opt.height+'px'
					});
					if ($.ma_pv.opt.counter2) {
						if (item.img_link_target) {
							$a.attr({
								'href': item.img_link_url,
								'target': item.img_link_target
							}).click(function() {
								$.main_btn_countup($.ma_pv.opt.counter2,item.no);
								return true;
							});
						} else {
							$a.attr({
								'href': 'javascript:void(0)'
							}).click(function() {
								$.main_btn_countup2($.ma_pv.opt.counter2,item.no,item.img_link_url,item.img_link_target);
								return true;
							});
						}
					} else {
						$a.attr({
							'href': item.img_link_url,
							'target': item.img_link_target
						});
					}
					$(imgbox).append($a);
				}
			} else {
				var $a = $(document.createElement("a"));
				var img = document.createElement("img");
				$(img).bind("load", this, function(e) {
					$.mainbtnaction_imgfit($(this));
				});
				$(imgbox).append($a);
				$(img).attr("src", item.imgurl);
				$a.append($(img));
				if (item.img_link_url) {
					if ($.ma_pv.opt.counter2) {
						if (item.img_link_target) {
							$a.attr({
								'href': item.img_link_url,
								'target': item.img_link_target
							}).click(function() {
								$.main_btn_countup($.ma_pv.opt.counter2,item.no);
								return true;
							});
						} else {
							$a.attr({
								'href': 'javascript:void(0)'
							}).click(function() {
								$.main_btn_countup2($.ma_pv.opt.counter2,item.no,item.img_link_url,item.img_link_target);
								return true;
							});
						}
					} else {
						$a.attr({
							'href': item.img_link_url,
							'target': item.img_link_target
						});
					}
				}
			}

			$.ma_pv.opt.data[i].element = $(imgbox);
*/
			var $btn = $('#'+$.ma_pv.opt.prefix+item.no);
			var $wrapa = $(document.createElement('a'));
			$btn.parent().append($wrapa);
			$wrapa.append($btn);
			if (item.btn_link_url) {
				if ($.ma_pv.opt.counter) {
					if (item.btn_link_target) {
						$wrapa.attr({
							'href': item.btn_link_url,
							'target': item.btn_link_target
						}).click(function() {
							$.main_btn_countup($.ma_pv.opt.counter,item.no);
							return true;
						});
					} else {
						$wrapa.attr({
							'href': 'javascript:void(0)'
						}).click(function() {
							$.main_btn_countup2($.ma_pv.opt.counter,item.no,item.btn_link_url,item.btn_link_target);
							return true;
						});
					}
				} else {
					$wrapa.attr({
						'href': item.btn_link_url,
						'target': item.btn_link_target
					});
				}
			}
			$btn.hover(
				function() {
					var j;
					var id = $(this).attr('id')+'_img';
					for (j = 0; j < $.ma_pv.opt.data.length; j++) {
						if ($.ma_pv.opt.data[j].hide_timer_id != 0) {
							window.clearTimeout($.ma_pv.opt.data[j].hide_timer_id);
							$.ma_pv.opt.data[j].hide_timer_id = 0;
						}
						if ($.ma_pv.opt.data[j].element.attr('id') == id) {
							$.ma_pv.opt.data[j].element.css({
								'z-index': 110
							}).animate({'left': '0px'}, 500, 'swing', function() {
								$(this).css({'z-index': 100});
							});
						} else {
							$.ma_pv.opt.data[j].element.css({
								'z-index': 100,
								'left': $.ma_pv.opt.width+'px'
							});
						}
					}
				},
				function() {
					var id = $(this).attr('id')+'_img';
					var j;
					for (j = 0; j < $.ma_pv.opt.data.length; j++) {
						if ($.ma_pv.opt.data[j].element.attr('id') == id) {
							$.ma_pv.opt.data[j].element.stop();
							$.ma_pv.opt.data[j].hide_timer_id = window.setTimeout(
								function() {
									var k;
									for (k = 0; k < $.ma_pv.opt.data.length; k++) {
										var l = $.ma_pv.opt.data[k].element.css('left');
										if (l != ($.ma_pv.opt.width+'px')) {
											$.ma_pv.opt.data[k].element.css({
												'z-index': 100
											}).animate({'left': $.ma_pv.opt.width+'px'}, 500, 'swing', function() {
												$(this).css({'z-index': 100});
											});
										}
										$.ma_pv.opt.data[k].hide_timer_id = 0;
									}
								}
								,1000);
						}
					}
				}
			);
		});
	};
	//
	// plugin defaults
	//
	$.mainbtnactionSetup.defaults = {
		prefix: 'mainbtn_',
		data: null,
		width: 680,
		height: 260,
		flash_ver:"6,0,0,0",
		spaceimg:"img/space.gif",
		counter:"",
		counter2:""
	};
	$.ma_pv = {
		timer_id: 0,
		item_loaded: 0,
		timetbldata: null,
		opt: null,
		cur_idx: -1,
		item_count: 0
	};


	$.mainbtnaction_calcfitsize = function (ow, oh, iw, ih) {
		var fw = iw;
		var fh = ih;
		if (iw > ow || ih > oh) {
			var mw = iw / ow;
			var mh = ih / oh;
			if (mw > mh) {
				fw = ow;
				fh = parseInt(ih/mw, 10);
			} else {
				fw = parseInt(iw/mh, 10);
				fh = oh;
			}
		}
		var fl = parseInt((ow-fw)/2, 10);
		var ft = parseInt((oh-fh)/2, 10);
		return { left:fl, top:ft, width:fw, height:fh };
	};

	$.mainbtnaction_imgfit = function($img) {
		var w = $img.width() ? $img.width() : $.ma_pv.opt.width;
		var h = $img.height() ? $img.height() : $.ma_pv.opt.height;
		var bw = $.ma_pv.opt.width;
		var bh = $.ma_pv.opt.height;
		if (w > bw || h > bh) {
			var mw = w / bw;
			var mh = h / bh;
			if (mw > mh) {
				$img.width(bw+'px');
				$img.height(parseInt(h/mw, 10)+'px');
			} else {
				$img.width(parseInt(w/mh, 10)+'px');
				$img.height(bh+'px');
			}
		}
		w = $img.width();
		h = $img.height();
		$img.css({
			'left': parseInt((bw-w)/2, 10)+'px',
			'top': parseInt((bh-h)/2, 10)+'px',
			'visibility': 'visible'
		});
	};

	$.main_btn_countup = function(pg,no) {
		(new Image()).src=pg+no;
		return true;
	};

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

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

