function billboard_go_to(page){
	
	$('.active-billboard-selector').removeClass('active-billboard-selector');
	$('.billboard-selector-'+page).addClass('active-billboard-selector');
	$('.current-monitor-pic').animate({ opacity : 0 }, 500, function(){
		
		$(this).css({ display : 'none' }).removeClass('current-monitor-pic');
		
		$('#left-monitor-screen').find('a:eq('+page+')').css({ display : 'block' }).animate( {opacity : 1}, 500, function(){
			$(this).addClass('current-monitor-pic');
		});
		$('#center-monitor-screen').find('a:eq('+page+')').css({ display : 'block' }).animate( {opacity : 1}, 500, function(){
			$(this).addClass('current-monitor-pic');
		});
		$('#right-monitor-screen').find('a:eq('+page+')').css({ display : 'block' }).animate( {opacity : 1}, 500, function(){
			$(this).addClass('current-monitor-pic');
		});
		
	});
	
}

function preloading_images(){
	
	$('img.with-loader').each(function(i, event){
	
		$(this).parent('a').addClass('preloading');
		
		$(this).load(function(){
			$(this).parent('a').removeClass('preloading');
			$(this).removeClass('with-loader').css({ display : 'block', opacity : 1});
		});
		
		if(event.complete == true){
			$(this).parent('a').removeClass('preloading');
			$(this).removeClass('with-loader').css({ display : 'block', opacity : 1});
		}
		
	});
}

$(document).ready(function() {
	
	preloading_images();
	
	$('ul.sf-menu').superfish({
		animation: {height:'show'},
		autoArrows : false
	});
	
	$('ul.sf-menu li').has('ul').addClass('sf-menu-contains-sub');
	$('ul.sf-menu li ul li').has('ul').addClass('sf-menu-contains-sub');
	
	$('#s').click(function(){
		if($(this).val() == 'Search'){
			$(this).val('');
		}
	});
	
	$('#s').blur(function(){
		if($(this).val() == ''){
			$(this).val('Search');
		}
	});
	
	$('#thickbox-mask').css({ 'width' : $(window).width(), 'height' : $(document).height() });
	$(".scrollable").scrollable();
	
	$('#thickbox-content .close').click(function(){
		$('#thickbox-content, #thickbox-mask').fadeOut(500);
	});
	
	var billboard_num_el = $('#left-monitor-screen a').size();
	var billboard_nav_html = '';
	
	var i=0;
	for (i=0;i<billboard_num_el;i++){
		billboard_nav_html += '<a href="javascript:billboard_go_to('+i+')" class="billboard-selector billboard-selector-'+i+'"></a> ';
	}
	
	$('#billboard-navigation').html(billboard_nav_html);
	$('#billboard-navigation a:first').addClass('active-billboard-selector');
	
	$('#left-monitor-screen a:first, #center-monitor-screen a:first, #right-monitor-screen a:first').addClass('current-monitor-pic').css({display : 'block', opacity : 1});
	
	var billboard_el_width = 21;
	$('#billboard-navigation').width(billboard_el_width * billboard_num_el);

	$('.centered').each(function(){
		
		var parent_width = $(this).parent().outerWidth();
		var this_width = $(this).outerWidth();
		var this_offset = (parent_width - this_width)/2;
		$(this).css({ 'left' : this_offset });
		
	});
	
	$('#billboard-monitors a').click(function(e){
		e.preventDefault();
		$('#thickbox-mask').fadeTo(500, 0.7);
		$('#thickbox-content').fadeIn();
	});
	
	$('.scrollable .items a').click(function(e){
		e.preventDefault();
		var the_image = $(this).find('img').attr('src').replace('_thumb', '');
		$('#thickbox-big-image').addClass('with-loader').attr('src', the_image);
		preloading_images();
	});
	
});
