/***滚动播放插件**************/
$.fn.extend({
	rollscreen : function(options){
		var defaults = {
			renderto : 'body',
			rollimg : new Array(), 
			interval : 5000,
			defaultselect : 0,
			blank : 0,
			maskinterval : 200
		}
		
		var options = $.extend(defaults,options);
		var renderObj = options.renderto == 'body' ? $('body') : $('#'+options.renderto);
		var timer = null;
		//设定当前index
		options.currentIndex = options.defaultselect == 0 ? -1 : options.defaultselect;
		//生成html
		createHtml();
		//绑定事件
		bindEvent();
		
		
		function createHtml(){
			var html = '';
			html += "<div class=\"screen\">";
			html += "<div class=\"img\">";
			html += "<a "+(options.blank == 1 ? "target=\"_blank\"" : '')+" href=\""+options.rollimg[options.defaultselect].url+"\">";
			html += "<img src=\""+options.rollimg[options.defaultselect].imglarge+"\" />";
			html += "</a>";
			html += "</div>";
			html += "<ul>";
			$.each(options.rollimg,function(i){
				html += "<li> <a href=\"#\" class=\""+(i == options.defaultselect ? 'hover' : '')+"\"> <span class=\"first\">"+options.rollimg[i].subject+"</span> <span class=\"last\">"+options.rollimg[i].desc+"</span> </a> </li>";
			});
			html += "</ul>";
			html += "<div class=\"clear\"></div>";
			html += "</div>";
			renderObj.html(html);
		}
		
		function bindEvent(){
			renderObj.find('ul').find('a').each(function(i){
				$(this).bind('click',function(){
					window.clearInterval(timer);
					timer = null;
					if(i != options.currentIndex){
						bindEventSingle(i);
					}
					return false;
				});
				$(this).bind('mouseenter',function(){
					window.clearInterval(timer);
					timer = null;
					if(i != options.currentIndex){
						bindEventSingle(i);
					}
					return false;
				});
				
			});
			
			renderObj.bind('mouseleave',function(){
				//setAutoRoll();
				if(!timer || timer == null){
					timer = window.setInterval(setAutoRoll,options.interval);
				}
			});
		}
		
		function bindEventSingle(ii){
			//换缩略图样式
			renderObj.find('ul').find('a').each(function(j){
				if(j == ii){
					$(this).addClass('hover');
				}
				else{
					$(this).removeClass('hover');
				}
			});
			//换图片路径
			var img = renderObj.find('.img').find('a').find('img');
			if($.browser.msie){
					//设置变换参数 
				 img.css('filter','revealTrans()')
				 with(img[0].filters.revealTrans){ 
						 Transition =23;	
						 Duration = 0.25; 
						 apply(); 
						 play();
				} 
				img.attr('src',options.rollimg[ii].imglarge);
			}
			else{
				img.animate({opacity : '0'},options.maskinterval);
				img.attr('src',options.rollimg[ii].imglarge);
				img.animate({opacity : '1'},options.maskinterval);
				//img.attr('src',options.rollimg[ii].imglarge);
				//renderObj.find('.block-l').find('a.img').find('img').animate({src : options.rollimg[ii].imglarge},options.maskinterval);
				//alert(options.rollimg[ii].imglarge);
				
			}


			//换图片点击跳转路径
			renderObj.find('.img').find('a').attr('href',options.rollimg[ii].url);
			
			options.currentIndex = ii;
			return false;
		}
		
		function setAutoRoll(){

			//alert(options.currentIndex);
			options.currentIndex ++;
			if(options.currentIndex == options.rollimg.length){
				options.currentIndex = 0;
			}
			
			bindEventSingle(options.currentIndex);
		
			//timer = window.setInterval(setAutoRoll,5000);
		}
		
		timer = window.setInterval(setAutoRoll,options.interval);
	}
});



