var speed = 0;
var top_width = 825;
var go_away = 0;
var olc_colors=new Array();
olc_colors[0]="#0074c5"; //blue (corel sky blue)
olc_colors[1]="#f4fbfe"; //sky (average gradient color)
olc_colors[2]="#009049"; //green
olc_colors[3]="#6ac335"; //chartreuse 
olc_colors[4]="yellow"; //chartreuse 
olc_colors[5]="white"; //chartreuse 
olc_colors[6]="black"; //chartreuse 

function rand( min, max ) {
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
$(document).ready( function(){

	if (tdp2010form){
		$('h2').each(function(){
			if ( $(this).html().indexOf('SOLD OUT')>0 ) {
				$(this).css({'font-size':'1em', color:'black', 'font-weight':'normal'});
			}
		});
	}
							
	top_width = document.body.offsetWidth;
	
	//the menu system
	var onMenu;
	$('#olc_nav ul li a').hoverIntent(
		function() {
			var section_class = $(this).attr('class');
			var sub_menu = $('ul.' + section_class);
			if (sub_menu.length) {
				sub_menu.clone().addClass('olc_menu').prependTo('#olc_body');
				$('.olc_menu').hover(
					function() { onMenu = true;  },
					function() { onMenu = false; hide_dropdown_menus(); }
				);
				$(this).animate({
					paddingBottom: '15px',
					marginBottom: '0px'
				}, 100, 'linear', function() { 
					$(this).addClass('menued');
					var nav_item_left_pos = $(this).offset().left;
					
					/* bad ie less than 8! */
					if ($.browser.msie && parseInt($.browser.version) < 8) {
						nav_item_left_pos = nav_item_left_pos  - $('#olc_feature_container').offset().left;
					}
					
					var nav_item_width = $(this).parent().width();
					var nav_item_bot_pos = $(this).parent().offset().top + $(this).parent().height();
					
					$('.olc_menu').css({
						//height: 'auto', paddingTop: '15px', paddingBottom:'15px',
						top: nav_item_bot_pos,
						marginLeft: nav_item_left_pos,
						minWidth: nav_item_width
					}).slideDown(200);
				});
			}
		},
		function() {
			hide_dropdown_menus()
		}
	)
	function hide_dropdown_menus() {
		if (!onMenu) {
			$('.olc_menu').stop().css({display:'none',height:'auto', paddingTop: '15px', paddingBottom:'15px' }).remove();
			$('#olc_nav ul li a').stop().css({ paddingBottom: '10px', marginBottom: '5px'}).removeClass('menued');
		}
	}

	//make the feature a big link if there is one link in the box
	if ( $('.olc_feature .side_text a').length ) {
		var thefirstlink = $('.olc_feature .side_text a:first-child').attr('href');
		var alllinksthesame = true;
		$.each( $('.olc_feature .side_text a'), function(i,a) {
			if ( $(a).attr('href') !== thefirstlink ) {
				alllinksthesame = false;
				return false;
			}
		});
		if (alllinksthesame) {
			$('.olc_feature .side_text').css({"cursor":"pointer"}).click( function() {
				window.location = thefirstlink;
			});
		}
	}

	//the feature
/*	$('.olc_feature .side_bg').css({opacity:'.3'});
	var bgsrc = $('.olc_feature .background_image').attr('src');
	if ( bgsrc == 'feature_community.jpg' ) {
		$('.olc_feature .side_bg').css({opacity:'0.5', backgroundColor:'black' });
		$('.olc_feature .side_text p, .olc_feature .side_text h2').css( {color:'white'} );
		$('.olc_feature .side_text h1').css( {color:'yellow'} );
	} else if ( bgsrc == 'feature_metro.jpg' ) {
		$('.olc_feature .side_bg').css({opacity:'0.5', backgroundColor:'white' });
	} else if ( bgsrc == 'feature_bike.jpg' ) {
		$('.olc_feature .side_bg').css({opacity:'0', backgroundColor:'#6ac335' });
		$('.olc_feature .side_text p, .olc_feature .side_text h2').css( {color:'white'} );
		$('.olc_feature .side_text h1').css( {color:'white'} );
	}
	*/
	
	//the blocks
	/*
	for (var i=1; i <= 5; i++){
		//$('#olc_feature_container').append('<div class="olc_bar">&nbsp;</div>')
	}
	$('.olc_bar').each( function (i) {
		var space_width = 825;
		var start_width = rand(0,825);
		var end_width = rand(0,600);
		var start_margin_left = rand(0,space_width-start_width);
		var end_margin_left = (space_width - end_width);
		$(this).css({
			width: start_width+'px',
			marginLeft: start_margin_left+'px',
			opacity:(rand(1,10)/10),
			backgroundColor:olc_colors[rand(0,6)]

		});
		bar_go_left(this);
	});
	$('#olc_logo').click( function() {
		go_away = 1;
	})
	*/
	
	//fix ie buttons and make the email form work
	if ($.browser.msie) { $('.olc_feature .side_text input.form_button').css({ paddingBottom:'2px'}); }
	if ( $('#feature_email').attr('value')=='' ) { $('#feature_email').attr('value','Your Email'); }
	$('#feature_email').focus( function(){if ( $(this).attr('value') == 'Your Email') $(this).attr('value','');} );
	$('.olc_feature .side_text form').hide(); setTimeout( function() { $('.olc_feature .side_text form').fadeIn(1000); }, 1500);

	//Site Map
	$('#olc_footer_right').append('<span id="olc_show_site_map_link" >Show Site Map</span>');
	$('#olc_site_map').hide();
	$('#olc_show_site_map_link').click(
		function() {
			$('#olc_site_map').stop();
			if ( $('#olc_show_site_map_link').html() == 'Show Site Map' ) {
				$('#olc_site_map').css({height:'auto'}).show();
				$.scrollTo( '#olc_site_map', 500);
				$('#olc_show_site_map_link').html('Hide Site Map');
				//Save state
				$.ajax({
					type: "POST",
					url: "/config_central/scripts/layout_state.php",
					data: 'map_hidden=f'
				});
			} else {
				$('#olc_site_map').slideUp(500);
				$('#olc_show_site_map_link').html('Show Site Map');
				//Save state
				$.ajax({
					type: "POST",
					url: "/config_central/scripts/layout_state.php",
					data: 'map_hidden=t'
				});
			}
		}
	)
	if (!map_hidden) {
		$('#olc_site_map').css({height:'auto'}).show();
		$('#olc_show_site_map_link').html('Hide Site Map');
	}
	
	
	//the figures
	$('.olc_bike_right, .olc_people_right, .olc_bus_right, .olc_bike_left, .olc_bike_left_upright, .olc_people_left, .olc_bus_left, .olc_business_person').addClass('olc_figure');
	$('.olc_bike_right, .olc_people_right, .olc_bus_right, .olc_business_person').addClass('olc_figure_right');
	$('.olc_bike_left, .olc_bike_left_upright, .olc_people_left, .olc_bus_left').addClass('olc_figure_left');
	$('.olc_bike_right, .olc_bike_left, .olc_bike_left_upright').addClass('olc_bike');
	$('.olc_people_right, .olc_people_left, .olc_business_person').addClass('olc_people');
	$('.olc_bus_right, .olc_bus_left').addClass('olc_bus');
	$('.olc_figure_left').attr('direction','left');
	$('.olc_figure_right').attr('direction','right');
	$('.olc_people').attr("speed",150000);
	$('.olc_bike').attr("speed",20000);
	$('.olc_bus').attr("speed",15000);	
	top_width = $('#olc_top').width();
	$('.olc_figure').each( function(i) {
	
		this.positionme = function(at) {
			if (at == 'start') start_position = ( $(this).attr('direction') == 'right' ? 0 : top_width - this.offsetWidth );
			else start_position = rand(0,top_width - this.offsetWidth);
			$(this).css({ left: start_position+'px'});
		}
		
		this.moveme = function(option) {	
			top_width = $('#olc_top').width();
			
			if (option=='fast' || option=='bump') speed = rand(1000,2000);
			else speed = rand(parseInt($(this).attr('speed'))-5000, parseInt($(this).attr('speed'))+5000);
			
			current_left = parseInt($(this).css('left'));
		
			if ( $(this).attr('direction') == 'right' ) {
				if ( top_width - this.offsetWidth - current_left < 200 || option == 'fast') end_left_position = top_width-this.offsetWidth;
				else if (option == 'bump') end_left_position = current_left+rand(10,100);
				else end_left_position = rand(current_left+100,top_width-this.offsetWidth);
				
				if (end_left_position<100) end_left_position = rand(100,500);
				if (end_left_position>top_width-100-this.offsetWidth) end_left_position = top_width-this.offsetWidth;

			} else {
				if (option == 'fast' || current_left < 200) end_left_position = 0;
				else if (option == 'bump') end_left_position = current_left-rand(10,100);
				else end_left_position = rand(0,current_left-100);
				
				if (end_left_position>top_width-100) end_left_position = top_width-rand(100,500)-this.offsetWidth;
				if (end_left_position<100) end_left_position = 0;
			}
			//$(this).html(end_left_position);
			//speed = speed;
			
			$(this).stop();			
			$(this).animate({ left: end_left_position}, speed, 'swing', function(){		
				var self = this;
				setTimeout(
					function(){
						if ($(self).attr('direction')=='left' && $(self).css('left') == '0px') { self.positionme('start'); }
						else if ($(self).attr('direction')=='right' && $(self).css('left') == (top_width - self.offsetWidth)+'px' ) { self.positionme('start'); }
						self.moveme(false);
					}
					, rand(5000,15000)
				);
			} )
		}
		
		this.onmouseover = function() {
			$(this).stop();
			this.moveme('bump');
		} 

		this.onclick = function() {
			$(this).stop();
			this.moveme('fast');
		}
		
		this.ondblclick = function() {
			$(this).stop();
			$(this).fadeOut('normal', function() { $(this).remove(); } );
		}
		
		this.positionme('random');
		this.moveme(false);
	})
})


function figure_stop_regenerate(figure) {
	//$(figure).fadeOut();
}

/*
//Feature
function bar_go_left(bar) {
	if ($(bar).css('opacity') == 0) {
		$(bar).remove();
	} else {
		if (go_away == 1) { var final_opacity = 0; } else { var final_opacity = rand(5,10)/10; }
		$(bar).animate({
			marginLeft: '0px',
			width: rand(0,825)+'px',
			opacity:final_opacity,
			backgroundColor:olc_colors[rand(0,6)]
		}, rand(1000,3000), 'swing', bar_go_right );
	}
}
function bar_go_right() {
	if ($(this).css('opacity') == 0) {
		$(this).remove();
	} else {
		if (go_away == 1) { var final_opacity = 0; } else { var final_opacity = rand(5,10)/10; }
		var space_width = 825;
		var end_width = rand(0,825);
		var end_margin_left = (space_width - end_width);		
		$(this).animate({
			marginLeft: end_margin_left+'px',
			width: end_width+'px',
			opacity:final_opacity,
			backgroundColor:olc_colors[rand(0,6)]
		}, rand(1000,3000), 'swing', bar_go_left(this) );
	}
}
*/
