
var sliderDelay = 10000;// in millis
var slideWidth = 598;
var numberOfSlides;
var sliderTimer;
var current_slide_index=0;

$(document).ready(function(){

	// Remove scrollbar in JS
	$('#content-highlights').css({'overflow':'hidden', 'margin':'0', 'border' : '2px solid #c9c9c9'});
	
	// Set correct width for slider.
	$('#content-highlights div.entry').css({'width':slideWidth+'px'});
	
	// clone first to last to enable infinend sliding effect
	$('#content-highlights div.entry:first').clone().appendTo('#content-highlights');
	
	//var textEl = $('#content-highlights div.entry div.data p:first');
	
	var slides = $('#content-highlights div.entry');
	numberOfSlides = slides.length-1;
	
	// Wrap all .slides with #slideInner div
	// Float left to display horizontally, readjust .slides width
	slides.wrapAll('<div id="slideInner"></div>').css({ 'float' : 'left', 'width' : slideWidth});

	$('#slideInner').css('width', slideWidth * (numberOfSlides+1));
	
	// move links to the end of the content text.
	$('#slideInner .entry').each(function(index) {
		
		var links = $('<div/>');
		//console.log('data_p:'+data_p.html());
		$(this).find('p.related-internal-link, p.related-doc, p.related-enternal-link').each(function(){
			var img = $(this).find('img:eq(0)');
			var a = $(this).find('a:eq(0)');
			
			$(a).appendTo(links);
			if(img != null)
				img.appendTo(links);
			$(this).remove();
			
		});
		$(this).find('p').each(function(index) {
			if(jQuery.trim($(this).text()).length==0){
				$(this).remove();
			}
		});
		$(links.html()).appendTo($(this).find('p:last'));
	});
	/*
	$('#slideInner a.internal, #slideInner a.external').each(function(index) {
		var data_div = $(this).parent().parent();
		var data_p = data_div.find('p:eq(2)');
		var link_p = $(this).parent();
		var img = link_p.find('img');
		$(this).appendTo(data_p);
		if(img != null)
			img.appendTo(data_p);
		link_p.remove();
	});
	 */
	// append image caption texts to image alt-text.
	$('#slideInner p.caption').each(function(index) {
		var caption = $(this).text();
		var p_img = $(this).parent().find('img:eq(0)');
		if(p_img!=null){
			var i_alt = p_img.attr('alt');
			i_alt = i_alt==null?caption:i_alt+caption;
			p_img.attr('alt', i_alt);
		}
	});
		
	$('#content-highlights').after('<div class="controls"></div>');
	
	for(var i=numberOfSlides;i>0;i--){
		$('div.controls').append('<a id="c'+(i-1)+'" href="javascript:void(0);" >O</a>');	
	}

	$('div.controls a').bind('mouseover', function(){
		$(this).css('background-position', '-17px 0');
	});
	$('div.controls a').bind('mouseout', function(){
		if($(this).hasClass('sel'))
			$(this).css('background-position', '0 0');
		else
			$(this).css('background-position', '-34px 0');
	});
	  
	$('div.controls a').bind('click', function(){
		var position = $(this).attr('id').substring(1);
		show_slide(position, false);	
	});
	
	
	// ie 7 and 8 fix
	$('#slideInner a.highlights-video-link"').each(function(index) {
		var img_v = $(this).find('img');
				
		$(this).find('.highlights-video-overlay').html($('<a/>').attr({'href':$(this).attr('href'), 'alt':$(this).attr('alt')}).html(
				$('<img/>').attr({'src':img_v.attr('src'), 'alt':img_v.attr('alt')})));
	});
	
	show_slide(0, true);
	
	$(document).blur(function(){
		clearTimeout(sliderTimer);
	});
	
	current_slide_index = 0;
	
	$(document).focus(function(){
		clearTimeout(sliderTimer);
		sliderTimer = setTimeout(function () {show_slide(current_slide_index,true);}, sliderDelay);
	});
});

function show_slide(index, setTimer){
	clearTimeout(sliderTimer);

	$('div.controls a').css('background-position', '-34px 0');
	$('div.controls a').removeClass('sel');
	
	$('#slideInner').animate({'marginLeft' : slideWidth*(-index)}, 'slow', function(){
		if(index==0 || index == numberOfSlides){
			$('#slideInner').css({'marginLeft' : '0px'});
		}
	});
	
	index = index == numberOfSlides?0:index;	
	
	$('a#c'+index).css('background-position', '0 0');
	$('a#c'+index).addClass('sel');
	
	current_slide_index = parseInt(index)+1;
	
	if(setTimer){
		sliderTimer = setTimeout(function () {show_slide(current_slide_index, true);}, sliderDelay);
	}
}


