
function Slideshow(){
	var ltr = null;
	var idx = -1;
	var items = [];
	var me = this;
	var $root;
	var previousIndex = -1;
	var _mouseover = false;

	var build = function(){
		$root = $(me.target);
		$root.empty().addClass('slideshow');

		itemNode = '';
		for (x=0;x<me.data.length;x++) {
	    itemNode = itemNode+'<p _index="'+x+'">'+x+'</p>';
		}
		slideshowlinks = '<div class="slideshow-link">'+itemNode+'</div>';

		items = [];
		for(var i=0; i < me.data.length; i++){
			var $item = $(me.itemRenderer(i,me.data[i]));
			$item.attr('_index',i);
			$item.fadeOut(0);
		  $item.find('div.slideshow-nav').append(slideshowlinks);
			$item.appendTo($root);
			items.push($item);
		}
	}
	
	this.start = function(){
		Slideshow.init();
		idx = -1;
		me.index = -1;
		build();
		me.next();
	}
	
	var _render = function(){
    $(items[me.index]).mouseenter(function () {
      _mouseover = true;
      $(this).find('div.slideshow-overlay').show();
    }).mouseleave(function () {
      _mouseover = false;
      $(this).find('div.slideshow-overlay').hide();
  		clearTimeout(me.ltr);
	    me.ltr = setTimeout(me.next, me.interval);
    });
    
    if ( _mouseover != true) {
  		if(me.index >= 0 && me.index < items.length){
  			$(items[me.index]).addClass('slideshow-item-selected').fadeIn(me.transitionTime,function(){
  				if(previousIndex>=0 && previousIndex != me.index){
  					$(items[previousIndex]).removeClass('slideshow-item-selected').hide();
  				}
  			});
  		}
	    me.ltr = setTimeout(me.next, me.interval);
  		idx = me.index;
    }
    $(items[me.index]).find('p[_index='+me.index+']').addClass('active');
    $(items[me.index]).find('div.slideshow-link p').click(function() {
      me.moveTo($(this).attr('_index'));
    });
	}
	
	this.moveTo = function(newIndex){
		if(items.length < 1){
			alert('Slideshow/moveTo : no items given');
			return;
		}
		previousIndex = idx = me.index;
		
		me.index = newIndex;
		me.index%= items.length;
		if(me.index < 0 ) me.index ++;
		if(previousIndex>=0 && previousIndex != me.index){
			$(items[previousIndex]).removeClass('slideshow-item-selected').fadeOut(0);
		}
		$(items[me.index]).addClass('slideshow-item-selected').fadeIn(me.transitionTime,function(){
      $(items[me.index]).find('p[_index='+me.index+']').addClass('active');
      $(items[me.index]).find('div.slideshow-link p').click(function() {
        me.moveTo($(this).attr('_index'));
      });
		});
    $(items[me.index]).mouseenter(function () {
      _mouseover = true;
      $(this).find('div.slideshow-overlay').show();
    }).mouseleave(function () {
      _mouseover = false;
      $(this).find('div.slideshow-overlay').hide();
  		clearTimeout(me.ltr);
	    me.ltr = setTimeout(me.next, me.interval);
    });
	}
	
	this.next = function(){
		if(items.length < 1){
			alert('Slideshow/next : no items given');
			return;
		}
		
		if (_mouseover != true) {
  		clearTimeout(me.ltr);
  		previousIndex = idx = me.index;
  		
  		me.index = idx +1;
  		me.index%= items.length;
  		_render();
    }
	}
	
	this.previous = function(){
		clearTimeout(ltr);
		if(items.length < 1){
			alert('Slideshow/previous : no items given');
			return;
		}
		previousIndex = idx = me.index;
		
		me.index = idx -1;
		if(me.index >= 0) me.index += items.length;
		
		_render();
	}

	this.itemRenderer = function(i,dataRow){
	  if (dataRow.content != "") {
  		var $item = $('<div class="slideshow-item"><div class="slideshow-overlay" style="display:none;background-color:#000;opacity:0.8;filter:alpha(opacity=80);position:absolute;width:990px;height:445px;"><div class="slideshow-header" style="color:#fff;background-color:none;padding-left:20px;padding-top:20px;">'+dataRow.content+'</div><div class="slideshow-nav"></div></div><img src="'+dataRow.image+'" border="0" /></div>');
	  } else {
		  
  		//var $item = $('<div class="slideshow-item"><div class="slideshow-overlay" style="display:none;position:absolute;width:990px;height:445px;bottom:0px;"><div class="slideshow-nav"></div></div><img src="'+dataRow.image+'" border="0" /></div>');


/*add by K*/

  		var $item = $('<div class="slideshow-item" ><div class="slideshow-overlay" style="display:none;position:absolute;width:990px;height:445px;bottom:0px;"><div class="slideshow-header" style="color:#fff;background-color:none;padding-left:20px;padding-top:20px;">'+dataRow.content+'</div></div><img src="'+dataRow.image+'" border="0" /></div>');
					if(dataRow.url){
						$item.addClass('slideshow-clickable');
						if(dataRow.url.target == 'lightbox'){
							$item.find('div.slideshow-overlay').click(function(){ // chrome, can't find img to attach event
                                $.colorbox({iframe:true,href:dataRow.url.href,width:dataRow.url.width,height:dataRow.url.height});
                            });
							$item.find('img').click(function(){
								$.colorbox({iframe:true,href:dataRow.url.href,width:dataRow.url.width,height:dataRow.url.height});
							});
						}
					}

/*end*/					
						
						
    }
		return $item;
	}
}

Slideshow.init = function(){
	if(Slideshow.init.__initialized) return;
	Slideshow.init.__initialized = true;
	
	$('head').append('<style>.slideshow-item{ position:absolute; z-index:1; display:none; width:100%; height:100%;}.slideshow-item-selected{z-index:2;}.slideshow-clickable{cursor:pointer;}</style>');
}

Slideshow.Styles= {
	Normal : 0,
	Cross : 1
};

Slideshow.prototype.target = "#swf_banner";
Slideshow.prototype.images = [];
Slideshow.prototype.index = -1;
Slideshow.prototype.transitionTime = 4000;
Slideshow.prototype.interval = 16000;
Slideshow.prototype.next = function(){}
Slideshow.prototype.previous = function(){}
Slideshow.prototype.start = function(){}
Slideshow.prototype.style = Slideshow.Styles.Cross;
