var slide='';
var whatsnew='';
var int=0;
var totalNew = 0;

jq(function(){	
	// What's new slideshow
	jq('.page_arrow').mouseout(_initNew);
	
	jq('.page_arrow').hover(function(){
		clearInterval(whatsnew);
	});		

	jq('.prev_arrow').click(getPrevNew);
	jq('.next_arrow').click(getNextNew);
	
	jq('.link_scrn').hover(function(){
		jq('.slide').eq(1).show();
	},function(){
		jq('.slide').eq(1).hide();
	});
	
	jq( '.slide' ).css( 'zoom', 1 ); 

	loadSlideXML(); 
	//loadDataservice();
});


function loadSlideXML(){
	//jq.get('/xml/whatsnew.xml',loadWhatsNew);
	jq.get(blogRSS+'?numEntries=3',loadWhatsNew);
}

function loadWhatsNew(data){
	totalNew = jq('item',data).length;

	var output = '';	
	jq('item',data).each(function(i){	
		if(i==0){
			output += '<div class="promote_slide current_slide_p" style="z-index:5;"><a href="'+jq(this).children('link').text()+'" class="slide_scrn"></a><h3>'+jq(this).children('title').text()+'</h3><p class="timestamp">'+jq(this).children('pubDate').text()+'</p><p class="promote_desc">'+jq(this).find('*[type=html]').text()+'</p></div><!-- promote slide -->';
		}else{
			output += '<div class="promote_slide" style="z-index:1;display:none;"><a href="'+jq(this).children('link').text()+'" class="slide_scrn"></a><h3>'+jq(this).children('title').text()+'</h3><p class="timestamp">'+jq(this).children('pubDate').text()+'</p><p class="promote_desc">'+jq(this).find('*[type=html]').text()+'</p></div><!-- promote slide -->';
		}		
	});
	
	jq('#promote_2').append(output);
	_initNew();
}

function _initNew(){
	whatsnew = setInterval("switchSlide('promote_slide','current_slide_p')",3000);
}

function getNextNew(){
	var getNext = int+1;
	if(getNext < totalNew){
		getNewByIndex(getNext);
		int++;
	}else{
		getNewByIndex(0);		
		int=0;
	}
}

function getPrevNew(){
	if(int > 0){
		getNewByIndex(int-1);
		int--;
	}else{
		getNewByIndex(totalNew-1);		
		int=totalNew-1;
	}
}

function getNewByIndex(index){
	clearInterval(whatsnew);
	slide = index;
	var pArray = [-4,11,26];
	jq('.promote_slide').each(function(i){
		jq(this).removeClass('promote_slide_p');																
		if(i == index){			
			jq('.promote_slide').eq(i).show();
			jq('.promote_slide').eq(i).addClass('promote_slide_p');
			jq('.lower_glow').css({'left':pArray[i]+'px'});
		}else{
			jq('.promote_slide').eq(i).hide();
		}
	});
}

function switchSlide(){
	var pArray = [11,26];
	var currentSlide = jq('.promote_slide:visible');
	var totalSlides = jq('.promote_slide').length;
	
	currentSlide.fadeOut('fast',function(){
		var nextSlide = currentSlide.next('.promote_slide');

		cs=jq('.promote_slide').index(currentSlide);
		
		if(nextSlide.length>0){
			nextSlide.fadeIn();			
			jq('.lower_glow').fadeOut('fast',function(){																			 
				jq('.lower_glow').css({'left':pArray[cs]+'px'});
				jq(this).fadeIn('fast');
			});
		}else{
			jq('.promote_slide:first').fadeIn();
			
			jq('.lower_glow').fadeOut('fast',function(){																			 
				jq('.lower_glow').css({'left':'-3px'});
				jq(this).fadeIn('fast');
			});
		}
	});
}
