/**
 * @author wangjun
 */


$.fn.swapImg = function(a, b) {
   b = jQuery.extend( {
      alt : '', title : '', id : '', usemap : '', map : '', noAnimate : false, speed : 500, rm_name: ''}
   , b);
   var c = this;
   var d = 2;
   var e = $('<img src="' + a + '" />').load(next);
   if(!b.noAnimate) {
      c.fadeTo(b.speed, 0.0, next)}
   else {
      c.attr( {
         src : a, id : b.id, alt : b.alt, title : b.title, useMap : b.usemap}
      ).show(1, function() {
         c.next().remove(); c.after(b.map)}
      );
      b.noAnimate = false}
   function next() {
      if(--d)return;
      c.attr( {
         src : a, id : b.id, alt : b.alt, title : b.title, useMap : b.usemap}
      ).fadeTo(b.speed, 1.0, function() {
         c.next().remove(); c.after(b.map)}
      )}
   };
function setActiveImg(imgs){
		var i = imgs.length - 1;
		do{	
			imgs[i].active = (imgs[i].segment == "test") ? true : false;
		}while(i--)
	}
var RT = {
   id : '', 
   linksDiv : 'rt_links',
   displayDiv : 'rt_show', 
   controlsDiv : 'rt_controls', 
   isExtLinkEvents : false, 
   cycles : 3, 
   fadeSpeed : 1000, 
   playSpeed : 5000, imgDir : '', 
   index : null, 
   counter : 0, 
   autoplay : true, 
   timer : null, 
   cycleCounts : 0, 
   playin : true, 
   complete : false, 
   imgs : [], 
   imgCount : 0, 
   imgTimer : null, 
   preload : function(a) {
      var o = RT;
      if(!o.complete) {
         var f = a.length - 1;
         var b = 0;
         do {
            b = a[f].images.length + b}
         while(f--)var i = a.length - 1;
         do {
            var p = a[i].images.length - 1;
            o.imgs[i] = [];
            o.imgs[i].images = [];
            o.imgs[i].active = a[i].active;
            o.imgs[i].segment = a[i].segment;
            do {
               o.imgs[i].images[p] = new Image();
               o.imgs[i].images[p].onload = function() {
                  o.imgCount++;
                  if(b == o.imgCount) {
                     o.complete = true}
                  };
	       if ( a[i].images[p].src.charAt(0) == '/' || a[i].images[p].src.substring(0,"http://".length) == "http://" ) {
                 o.imgs[i].images[p].src = a[i].images[p].src;
	       }
	       else {
                 o.imgs[i].images[p].src = mtx_img_server+a[i].images[p].src;
	       }
               o.imgs[i].images[p].title = a[i].images[p].title;
               o.imgs[i].images[p].id = a[i].images[p].id;
               o.imgs[i].images[p].usemap = a[i].images[p].usemap;
               o.imgs[i].images[p].map = a[i].images[p].map
               o.imgs[i].images[p].rm_name = a[i].images[p].rm_name;
               }
            while(p--)}
         while(i--)}
      return true
	  }
   , ready : function(a) {
      var o = RT;
      if(o.complete) {
         a();
         return};
      if(a &&!o.imgTimer) {
         o.imgTimer = window.setInterval(function() {
            if(o.complete) {
               a(); clearInterval(o.imgTimer); o.imgTimer = null}
            }
         , 10)}
      }
   , load : function() {
      RT.setImgHolder();
      RT.drawControls();
      if(!RT.isExtLinkEvents) {
         RT.addLinksEvent();
         RT.addControlersEvent()}
      }
   , set : function(a, b) {
      RT.id = a;
      RT.linksDiv = b;
      $('#' + RT.id).append('<div id="' + RT.displayDiv + '" style=""></div><div id="' + RT.controlsDiv + '" ></div>')}
   , setImgHolder : function() {
      RT.setIndex();
      var a = RT.imgs[RT.index].images;
	  //revised by mengjun
      $('#' + RT.displayDiv).append('<img style="" id="' + a[0].id + '" src="' + a[0].src + '"border="0" title="' + a[0].title + '" usemap="' + a[0].usemap + '" \/>' + a[0].map)}
   , drawControls : function() {
      var d = [_1st_slide_, _2nd_slide_, _3rd_slide_, _4th_slide_, _5th_slide_];
      var a = '';
      var c = 0;
      var i = RT.imgs[RT.index].images.length - 1;
      var p = 0;
      var b;
      if((i + 1) > 1) {
         do {
            width = (i == 0) ? 'width: 26px;':'';
            a += '<a id="slide_' + (c) + '" class="control rt_num" style="background-position: -' + p + 'px 0; ' + width + ';" href="#" alt="' + d[c] + '" title="' + d[c] + '" tabindex="2"></a>';
            p = p + 25;
            c++}
         while(i--)a += '<span style="float: left">&nbsp;&nbsp;</span>';
         a += '<a id="rt_stop_play" "class="control rt_stop" style="background-position: 0 -80px" href="#" alt="'+ _pause_slideshow_ +'" title="'+ _pause_slideshow_ +'" tabindex="1"></a>';
         a += '<a id="rt_prev" style="background-position: -50px -80px" "class="control" href="javascript:RT.prev();" alt="'+ _prev_slide_ +'" title="'+ _prev_slide_ +'" tabindex="1"></a>';
         a += '<a id="rt_next" style="background-position: -75px -80px; width: 26px" "class="control" href="javascript:RT.next();" alt="'+ _next_slide_ +'" title="'+ _next_slide_ +'" tabindex="1"></a>';
         $("#" + RT.controlsDiv).html(a)}
      else {
         $("#" + RT.controlsDiv).empty()}
      }
   , setIndex : function() {
      var i = RT.imgs.length - 1;
      do {
         if(RT.imgs[i].active) {
            RT.index = i}
         }
      while(i--)}
   , addLinksEvent : function() {
      if(!RT.isExtLinkEvents) {
         $('#' + RT.linksDiv + ' a').hoverIntent( {
            sensitivity : 3, interval : 200, over : RT.linksEvent, timeout : 200, out : function() {
               }
            }
         );
         $('#' + RT.linksDiv + ' a').bind('click focus', RT.linksEvent);
         $('#' + RT.linksDiv + ' a').click(function() {
            return false}
         )}
      }
   , linksEvent : function(a) {
      a = ((typeof(a) != 'string') && (window.event) || (a.target)) ? this.className : a;
      var b = /\brotate_[0-9]+\b/i;
      var c = b.exec(a)[0].split('_')[1];
      RT.loadImageGroup(c);
      return false}
   , loadImageGroup : function(a, b) {
      RT.stop();
      RT.counter = 0;
      RT.cycleCounts = 0;
      RT.imgs[RT.index].active = false;
      RT.index = a;
      RT.imgs[RT.index].active = true;
      RT.drawControls();
      RT.addControlersEvent();
      if(b)var b = (b) ? b : false;
      var c = RT.imgs[RT.index].images[0];
      $('#' + RT.displayDiv + ' img').swapImg(c.src, {
         noAnimate : b, speed : (RT.fadeSpeed / 2), id : c.id, title : c.title, alt : c.title, usemap : c.usemap, map : c.map}
      );
      if(RT.autoplay) {
         RT.play()}
      else {
         $('a#rt_stop_play').addClass('rt_play');
         $('a#rt_stop_play').removeClass('rt_stop');
         $('a#rt_stop_play').css( {
            backgroundPosition : '-25px -80px'}
         );
         $('a#rt_stop_play').attr( {
            alt : _resume_slideshow_, title : _resume_slideshow_}
         )}
      }
   , addControlersEvent : function() {
      $('a#rt_stop_play').click(function() {
         if(this.className.indexOf('rt_stop') !=- 1) {
            RT.stop(); $(this).addClass('rt_play'); $(this).removeClass('rt_stop'); $(this).css( {
               backgroundPosition : '-25px -80px'}
            ); $(this).attr( {
               alt : _resume_slideshow_, title : _resume_slideshow_}
            ); RT.autoplay = false}
         else {
            RT.autoplay = true; RT.play(); $(this).addClass('rt_stop'); $(this).removeClass('rt_play'); $(this).css( {
               backgroundPosition : '0 -80px'}
            ); $(this).attr( {
               alt : _pause_slideshow_, title : _pause_slideshow_}
            )}
         return false}
      );
      $('#' + RT.controlsDiv + ' a.rt_num').click(function() {
         RT.stop(); if(RT.imgs[RT.index].images.length > 1) {
            $('a#rt_stop_play').addClass('rt_play'); $('a#rt_stop_play').removeClass('rt_stop'); $('a#rt_stop_play').css( {
               backgroundPosition : '-25px -80px'}
            ); $('a#rt_stop_play').attr( {
               alt : _resume_slideshow_, title : _resume_slideshow_}
            ); var a = $('#' + RT.controlsDiv + ' a.rt_num').index(this); RT.counter = a; RT.jump(a); }
         return false}
      );
      $('#slide_0').css( {
         backgroundPosition : '0 -40px'}
      )}
   , jump : function(a) {
      var p = ['0', '-25px', '-50px', '-75px', '-100px'];
      var b = RT.imgs[RT.index].images;
      $('#' + RT.displayDiv + ' img').swapImg(b[a].src, {
         speed : (RT.fadeSpeed / 2), id : b[a].id, title : b[a].title, alt : b[a].title, usemap : b[a].usemap, map : b[a].map}
      );
      $('.rt_num').each(function() {
         this.style.backgroundPosition = this.style.backgroundPosition.replace('-40px', '0')}
      );
      $('#slide_' + (RT.counter)).css( {
         backgroundPosition : p[RT.counter] + ' -40px'}
      );
      if(RT.onPlay) {
         RT.onPlay()}
      RT.cycleCounts++;
      if(RT.cycleCounts >= (RT.imgs[RT.index].images.length * RT.cycles)) {
         RT.stop();
         $('a#rt_stop_play').addClass('rt_play');
         $('a#rt_stop_play').removeClass('rt_stop');
         $('a#rt_stop_play').css( {
            backgroundPosition : '-25px -80px'}
         );
         $('a#rt_stop_play').attr( {
            alt : _resume_slideshow_, title : _resume_slideshow_}
         );
         return}
      }
   , play : function() {
      RT.autoplay = true;
      RT.stop();
      if(RT.imgs[RT.index].images.length > 1) {
         RT.timer = window.setInterval(RT.autop, RT.playSpeed)}
      else {
         RT.stop()}
      }
   , autop : function() {
      RT.counter++;
      if(RT.counter == $('#' + RT.controlsDiv + ' a.rt_num').length) {
         RT.counter = 0}
      RT.jump(RT.counter)}
   , stop : function() {
      if(RT.timer != null)clearInterval(RT.timer)}
   , prev : function() {
      RT.stop();
      if((RT.counter - 1) >= 0) {
         RT.counter--;
         RT.jump(RT.counter)}
      $('a#rt_stop_play').addClass('rt_play');
      $('a#rt_stop_play').removeClass('rt_stop');
      $('a#rt_stop_play').css( {
         backgroundPosition : '-25px -80px'}
      );
      $('a#rt_stop_play').attr( {
         alt : _resume_slideshow_, title : _resume_slideshow_}
      );
      RT.autoplay = false}
   , next : function() {
      RT.stop();
      if((RT.counter + 1) < RT.imgs[RT.index].images.length) {
         RT.counter++;
         RT.jump(RT.counter)}
      $('a#rt_stop_play').addClass('rt_play');
      $('a#rt_stop_play').removeClass('rt_stop');
      $('a#rt_stop_play').css( {
         backgroundPosition : '-25px -80px'}
      );
      $('a#rt_stop_play').attr( {
         alt : _resume_slideshow_, title : _resume_slideshow_}
      );
      RT.autoplay = false}
   }
   RT.imgDir = hpCoreDir + 'img/';
	
   setActiveImg(accImgs);
   RT.preload(accImgs);	
	
   RT.stop();
   RT.set('mainWideColumn', 'blockAccordion');
   RT.ready(function(){
			RT.isExtLinkEvents = true;
			RT.playSpeed = 6000;
			RT.load();				
			RT.addControlersEvent();
			RT.loadImageGroup(0, true);
	});
