//prevent ugly flash of wrong fonts
(function(){
  // if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUC
  var d = document, e = d.documentElement, s = d.createElement('style');
  if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
    s.textContent = 'body{visibility:hidden}';
    e.firstChild.appendChild(s);
    function f(){ s.parentNode && s.parentNode.removeChild(s); }
    addEventListener('load',f,false);
    setTimeout(f,3000); 
  }
})();

// EZPZ Hint v1.1.1; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function(a){a.fn.ezpz_hint=function(e){var f={hintClass:'ezpz-hint',hintName:'ezpz_hint_dummy_input'};var c=a.extend(f,e);return this.each(function(){var b;var d;text=a(this).attr('title');a('<input type="text" name="temp" value="" />').insertBefore(a(this));b=a(this).prev('input:first');b.attr('class',a(this).attr('class'));b.attr('size',a(this).attr('size'));b.attr('name',c.hintName);b.attr('autocomplete','off');b.attr('tabIndex',a(this).attr('tabIndex'));b.addClass(c.hintClass);b.val(text);a(this).hide();a(this).attr('autocomplete','off');b.focus(function(){d=a(this);a(this).next('input:first').show();a(this).next('input:first').focus();a(this).next('input:first').unbind('blur').blur(function(){if(a(this).val()==''){a(this).hide();d.show()}});a(this).hide()});if(a(this).val()!=''){b.focus()};a('form').submit(function(){a('.'+c.hintName).remove()})})}})(jQuery);

var active_page = false;
var highlight_color = [255, 0, 0];
var background_color = [255, 127, 127];
var lightening_factor = 0.8;
var highlight_stuff = false;
var highlight_links =false;
var highlight_hover_divs = false;
var currently_hovering_div = false;
var currently_hovering_link = false;
var sidebar = false;
var active_archive_link = false;

function shuffle(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function generate_random_colors(){
	var old_highlight_color = highlight_color;
	var highlight = [];
	
	if(Math.random() < 0.5){
		highlight.push(255);
	}else{
		highlight.push(127 + Math.floor(Math.random() * 127));		
	}
		
	//decent chance the next value will be 255
	if(Math.random() < 0.4){
		highlight.push(255);
	}else{
		if(Math.random() < 0.3){
			highlight.push(0);
		}else{
			highlight.push(Math.floor(Math.random() * 90));
		}
	}
	
	//decent chance the next value will be 0
	if(Math.random() < 0.5){
		highlight.push(0);
	}else{
		highlight.push(Math.floor(Math.random() * 45));
	}
	
	highlight = adjust_color(highlight);
	
	//console.log(highlight_color);
	var background = [0,0,0];
	
	//create a background color based on the highlight color
	for (var i=0; i < highlight.length; i++) {
		var color = highlight[i];
		//console.log(color);
		
		if(color < 255){
			color += Math.floor((255 - color) * lightening_factor);
		}
		//console.log(color);
		background[i] = color;
	}
	
	return [highlight, background];
}

function css_color_string(array){
	return "rgb(" + array.join(', ') + ")";
}

function adjust_color(highlight){
	var old_highlight_color = highlight_color;	
	var too_close = true;
	var tries = 0;
	
	while(too_close == true && tries < 5){
		highlight = shuffle(highlight);
		too_close = false;
		
		var similar_colors = 0;
		for (var i=0; i < highlight.length; i++) {
			var difference = highlight[i] - old_highlight_color[i];
			difference = Math.abs(difference);
			
			if(difference < 60){
				similar_colors++;
			}
		}
		
		if(similar_colors > 1){
			too_close = true;
		}
		tries++;
	}
	
	return highlight;
}

function colors_are_ok(colors){
	var highlight = colors[0];
	var background = colors[1];

	//too yellow
	if(highlight[0] > 220 && highlight[1] > 220) return false;
	
	//too aqua blue
	if(highlight[2] > 220 && highlight[1] > 200) return false;
	
	//too light green
	if(highlight[1] > 220 && (highlight[0] < 100 || highlight[2] < 100)) return false;
	
	return true;
}

function set_colors(){
	var colors = [];
	var good_colors = false;
	while(!good_colors){
		colors = generate_random_colors();
		if(colors_are_ok(colors)){
			good_colors = true;
		}
	}
	
	highlight_color = colors[0];
	background_color = colors[1];
	
	var highlight_color_string = css_color_string(highlight_color);
	var background_color_string = css_color_string(background_color);
	
	//change the active hover div if there is one
	if(currently_hovering_div){
		currently_hovering_div.css('border-color', highlight_color_string);
	}
	
	//change the active hover link if there is one
	if(currently_hovering_link){
		currently_hovering_link.css('color', highlight_color_string);
	}
	
	highlight_stuff.css('color', highlight_color_string);
	$('#search_input').css('border-color', highlight_color_string);
	$('input.ezpz-hint').css('border-color', highlight_color_string);
	$('#header').css('border-bottom', '5px solid ' + highlight_color_string);
	$('#footer').css('border-top', '5px solid ' + highlight_color_string);
	$('#footer').css('border-bottom', '15px solid ' + background_color_string);
	$('body').css('background-color', background_color_string);
}

function register_highlight_links_hover_events(){
	highlight_links.hover(function(){
		currently_hovering_link = $(this);
		currently_hovering_link.css('color', css_color_string(highlight_color));
	},function(){
		currently_hovering_link = false;
		$(this).css('color', '#000000');
	});	
}

function register_highlight_hover_divs_hover_events(obj){
	highlight_hover_divs.hover(function(){
		currently_hovering_div = $(this);
		highlight_hover_divs.css('border-color', '#ffffff');
		currently_hovering_div.css('border-color', css_color_string(highlight_color));
	},function(){
		currently_hovering_div = false;
		$(this).css('border-color', '#ffffff');
	});
}

function scan_elements(){
	$('#page_content a, #poem a, #content_sidebar a, #poem_category_navigation a').each(function(i, el){
		var link = $(this);
		if(!link.hasClass('courier_underline') && !link.hasClass('courier_no_underline')){
			link.addClass('highlight_color');
		}
	});

	highlight_stuff = $('.highlight_color');
	highlight_hover_divs = $('.highlight_hover');
	highlight_links = $('a.courier_underline, a.courier_no_underline');
}


$(function(){	
	if(active_page){
		$('#header_link_' + active_page).removeClass('highlight_color');
	}
	
	if($('#content_sidebar')){
		sidebar = $('#content_sidebar');
	}

	if(!('placeholder' in document.createElement('input'))){
		$('input.hint').each(function(i, element){
			var input = $(element);
			input.attr('title', input.attr('placeholder'));
		});
		$('input.hint').ezpz_hint();
	}

	$('#poem_archive_listing a.archive_link').addClass('highlight_color');
	$('#poem_sidebar_twitter_box a').addClass('highlight_color');
	if(active_archive_link){
		var text = $(active_archive_link).text();
		$(active_archive_link).replaceWith("<span class='black'>" + text + "</span>");
	}
	
	scan_elements();
	set_colors();
	
	if(active_page == 'home'){
		setInterval(function(){
			
			$.get('/?single_poem_ajax=1', function(data){
				var lines = data.split(/\n/);
				var permalink = lines.shift();
				var poem_html = lines.join("\n");
				
				$('#poem').html(poem_html);
				$('#poem').unbind();
				$('#poem').click(function(){ document.location = permalink; });
				
				scan_elements();
				register_highlight_links_hover_events();
				register_highlight_hover_divs_hover_events();
				set_colors();
			});
			
		}, 15000);
	}else{
		setInterval(function(){ set_colors(); }, 15000);
	}
	
	if($('#poem').hasClass('no_highlight_hover')){
		$('#poem').css('cursor', 'auto');
	}

	register_highlight_links_hover_events();	
	register_highlight_hover_divs_hover_events();
	
});
