﻿$(function(){
    $.fn.extend({
        flashv1:function(img_paths,links,tags,img_width,img_height,tag_height){
			var jq_this=$(this);
			var o_this=this;
			var jq_div,jq_p,jq_ul;
			var arr_img=img_paths.split("|");
			var arr_link=links.split("|");
			var arr_tag=tags.split("|");
			this.now_pos=0;
			this.interval=null;
			
			this.Init=function(){
				jq_this.empty();
				jq_this.css({
				    "width":img_width+"px",
					"height":img_height+"px"
				});
				
				if($.trim(img_paths)=="")
				{
					alert("flashv3 holds no picture");
					return false;
				}
				if(arr_img.length!=arr_link.length || arr_img.length!=arr_tag.length)
				{
					alert("The numbers of images,links,tags are not the same.")
					return false;
				}
				return true;
			};
			
			this.CreatStructure=function()
			{
				jq_this.append("<div></div><ul></ul>");
				jq_div=jq_this.children("div");
				jq_ul=jq_this.children("ul");
				
				jq_div.append("<p></p>");
				jq_p=jq_div.children("p");
				
				jq_div.css({
				    "width":img_width+"px",
					"height":img_height+"px",
					"overflow":"hidden",
					"position":"relative"
				});
				
				jq_p.css("position","relative");
				
				jq_ul.css({
				    "width":img_width+"px",
					"lineHeight":tag_height+"px",
					positon:"relative",
					top:295+"px"
				});
				
				var i;
				for(i=0;i<arr_link.length;i++)
				{
					jq_ul.append("<li><a href='"+arr_link[i]+"'>"+arr_tag[i]+"</a></li>");
					jq_p.append("<a href='"+arr_link[i]+"'><img src='"+arr_img[i]+"' width='"+img_width+"' height='"+img_height+"' border='0' /></a>");
				}
				jq_ul.find("a").css({"width":(img_width/arr_link.length)-1+"px",opacity:0.5});
				jq_ul.find("a").eq(0).css({
					"background":"",
				    "backgroundColor":"#990000",
					"color":"#fff",
					opacity:0.75
				});
			};
			
			this.SetInterval=function(){
			    o_this.interval=setInterval(function(){
					var next_pos=o_this.now_pos>=arr_link.length-1?0:o_this.now_pos+1;
					o_this.Focus(next_pos);
				},5000);
			};
			
			this.ClearInterval=function(){
			    if(o_this.interval) clearInterval(o_this.interval);
			};
			
			this.SetEvent=function(){
				jq_ul.find("a").hover(
				    function(e){
					    var idx=jq_ul.find("a").index(this);
						o_this.Focus(idx);
						o_this.ClearInterval();
					},
					function(e){
					    o_this.SetInterval();
					}
				);
			};
			
			this.Focus=function(i){
				jq_ul.find("a").css({
				    "backgroundColor":"#000",
					"color":"#e3e3e3",
					opacity:0.5
				});
				jq_ul.find("a").eq(i).css({
					"background":"",
				    "backgroundColor":"#990000",
					"color":"#fff",
					opacity:0.75
				});
				jq_p.children("a").hide();
				jq_p.children("a").eq(i).fadeIn(1000);
				o_this.now_pos=i;
			};
			
			if(this.Init())
			{
				this.CreatStructure();
				this.SetInterval();
				this.SetEvent();
			}
	    }
    });
});
