var current_idea = 0;
var total_ideas = 0;
var ideas_step = 0; 

$(document).ready(function(){
	$('#ideas').hover(
							function(){
								$('#ideas-nav-line-wrapper').stop().animate({marginTop: 0}, 200);
							},
							function(){
								$('#ideas-nav-line-wrapper').stop().animate({marginTop: 65}, 200);
							}
	);
});

$(window).load(function(){	
	ideas_run($('#ideas-viewport .idea').size() - 1);
	
	var i = $('#ideas-viewport .idea').size()-1;
	for (i; i >= 0; i--){
		$('#id-idea'+i).css('visibility', 'visible');
	}
	
	var last_idea = $('#ideas-viewport .idea:last');
	/*var color = last_idea.css('background-image').match(/.+_(.+)\.[a-z]{3,4}/i);
	if (!color)
		color = new Array();*/
	
	$('#ideas-nav-line-info-text a').text(last_idea.attr('title')).attr('href', last_idea.attr('href'));
	
	/*if (color.lenght < 2)
		color[1] = '000000';	
	last_idea.css('background-color', '#'+color[1]);*/
});

$(window).resize(function(){
		ideas_init();
	});	

function ideas_run(start){
	current_idea = start;
	ideas_init();			
	load_ideas();	
	ideas_arrows_init();	
}

function ideas_init(){
	var window_width = $(document).width();	
	
	$('#ideas-viewport').width(window_width);
	$('#ideas-viewport .idea').width(window_width);
	$('#ideas-line').width((window_width+2)*($('#ideas-viewport .idea').size()+1));
	ideas_step = window_width;
	$('#ideas-line').css({marginLeft: -(ideas_step*current_idea)});
}

function load_ideas(){
	var i = 1;
	$('#ideas-viewport .idea').each(function(){				
		$(this).attr('id','id-idea' + (i-1)) ;
		i++;	
	});	
	
	total_ideas = i-2;	

}

function ideas_arrows_init(){
	//if (current_idea != 0)
		//$('#ideas-left-arrow .idea-num').text(current_idea);
	//else
		//$('#ideas-left-arrow').css('visibility', 'hidden');
	
	//if (total_ideas - current_idea != 0)
		//$('#ideas-right-arrow .idea-num').text(total_ideas - current_idea);
	//else
		$('#ideas-right-arrow').css('visibility', 'hidden');

	$('#ideas-left-arrow').click(function(){
			current_idea--;
			
			var color = $('#id-idea'+current_idea).css('background-image').match(/.+_(.+)\.[a-z]{3,4}/i);
			/*if (!color)
				color = new Array();
				
			if (color.lenght < 2)
				color[1] = '000000';			
			$('#id-idea'+current_idea).css('background-color', '#'+color[1]);*/
			
			$('#ideas-nav-line-info-text a').text($('#id-idea'+current_idea).attr('title')).attr('href', $('#id-idea'+current_idea).attr('href'));
			
			$('#ideas-line').animate({marginLeft: -(ideas_step*current_idea)}, 1000);	
			if (current_idea == 0)
				$(this).css('visibility', 'hidden');		

			//$(this).find('.idea-num').text(current_idea);
			//$('#ideas-right-arrow .idea-num').text(total_ideas - current_idea);	
			$('#ideas-right-arrow').css('visibility', 'visible');
			return false;
	});
	
	$('#ideas-right-arrow').click(function(){
			current_idea++;		
			
			var color = $('#id-idea'+current_idea).css('background-image').match(/.+_(.+)\.[a-z]{3,4}/i);
			if (!color)
				color = new Array();
				
			/*if (color.lenght < 2)
				color[1] = '000000';			
			$('#id-idea'+current_idea).css('background-color', '#'+color[1]);*/
			
			$('#ideas-nav-line-info-text a').text($('#id-idea'+current_idea).attr('title')).attr('href', $('#id-idea'+current_idea).attr('href'));
			
			$('#ideas-line').animate({marginLeft: -(ideas_step*current_idea)}, 1000);
			
			if (total_ideas - current_idea == 0)
				$('#ideas-right-arrow').css('visibility', 'hidden');		
			
			//$(this).find('.idea-num').text(total_ideas - current_idea);
			//$('#ideas-left-arrow .idea-num').text(current_idea);
			$('#ideas-left-arrow').css('visibility', 'visible');		
			return false;
	});
}
