var navArray = new Array();

$(document).ready(function() { 

	activateNav();
   
});

function activateNav()
{
    
    
	$('h2 a, h3 a, h4 a')
	    .bind('mouseover', function() { $(this).css({color : randomHueHex(75) }); })
        .bind('mouseout', function() { $(this).css({color : '#666' }); });
        
        
	$('.home_feature h2 a')
	    .bind('mouseover', function() { $(this).css({color : randomHueHex(80) }); })
        .bind('mouseout', function() { $(this).css({color : '#444' }); });
        

	$('#mainNav a')
	    .bind('mouseover', function() { $(this).css({color : randomHueHex(100) }); })
	    .bind('mouseout', function() { $(this).css({color : '#FFF' }); });
	    
	$('#subNav a')
	    .bind('mouseover', function() { $(this).css({color : randomHueHex(100) }); })
	    .bind('mouseout', function() { $(this).css({color : '#CCC' }); });
	    
	$('#footer a')
	    .bind('mouseover', function() { $(this).css({color : randomHueHex(75) }); })
	    .bind('mouseout', function() { $(this).css({color : '#666' }); });


	$('#logo_block').css({'background-color': randomHueHex(100)});

	$('#site_title a, small a')
	    .bind('mouseover', function() {$(this).css({color : randomHueHex(100) });})
	    .bind('mouseout', function() { $(this).css({color : '#000' }); });
	
	$('.post_roll li a')
	    .bind('mouseover', function() {$(this).css({color : randomHueHex(75) });})
	    .bind('mouseout', function() { $(this).css({color : '#333' }); });
	
	
	//CALENDAR BLOCKS   
	$('.event_block')
		.bind('click', function() { 
			
			makeEventPreview('calendar_posts', 	
				$(this).attr('postID'), 
				$(this).attr('category'), 
				$(this).attr('title'), 
				$(this).attr('date'), 
				$(this).attr('thumbnail'), 
				$(this).attr('excerpt'),
				'prepend'
			);
	});
	
	//CALENDAR FILTER
	$("#calendar_filter_select").change(onCalendarFilter);
	
	//BLOG FILTER
	$("#blog_filter").change(onBlogFilter);
	
	//SEARCH FORM
	
	//$('input.search_box:last').focus(function() { $(this).css({color: #000;}); $(this).val(''); });
	    
        
}


function makeEventPreview(inParentID, inID, inCategory, inTitle, inDate, inThumbnail, inExcerpt, inLocation)
{
	//alert('PARENT: ' + inParentID + '\r TITLE: ' + inTitle + '\r LOC: ' + inLocation );
	
	if ($('#event_preview_' + inID).length == 1) { $('#event_preview_' + inID).remove(); }
	
	
	var newEventPreview = $('<div />').addClass( 'event_preview').attr({id: 'event_preview_' + inID});
	
	//TITLE
	newEventPreview.prepend($('<div />').addClass('event_preview_title').addClass('color_' + inCategory).html('<a href="/?p=' + inID + '">' +inDate + ' : ' + inTitle+ '</a>'));
	var thumb_cont = $('<div />').addClass('event_preview_thumb');

	var thumb_img = new Image();
	$(thumb_img).hide();
	$(thumb_img).load(function() {sizeThumb($(this), 100, 100); $(this).show();})
	.attr('src', inThumbnail);
	
	newEventPreview.append(thumb_cont);
	thumb_cont.append(thumb_img);

	newEventPreview.append($('<div />').addClass('event_preview_content').html('<p>' + inExcerpt +'</p>'));
	
	
	if(inLocation=='prepend') $('#' + inParentID).prepend(newEventPreview);
	if(inLocation=='append') $('#' + inParentID).append(newEventPreview);
	
	//if ($(".event_preview").length > 5) { $(".event_preview")[5].remove(); }	
}

function loadThumb(inParent, inUrl, inWidth, inHeight, inLink)
{

	var cont = $('<div />').css({width: inWidth + 'px', height: inHeight + 'px', position: 'relative', overflow: 'hidden', float: 'left' });
	
	var thumb_img = new Image();
	$(thumb_img).hide();
	$(thumb_img).load(function() {sizeThumb($(this), inWidth, inHeight); $(this).show();})
	.attr('src', inUrl);
	
	if (inLink != '')
	{
		$(thumb_img).bind('click', function() { window.location = inLink; })
		.css({cursor: 'pointer'});
	}
	
	cont.append(thumb_img)
	
	var par = $("#" + inParent);
	
	par.append(cont);
}


function sizeThumb(theThumb, tW, tH)
{
	var targ = $(theThumb);
		
	var iW = targ.width();
	var iH = targ.height();
	
	var maxW = tW;
	var maxH = tH;
	
	var endScale = (maxW / maxH);
	
	var ratio = (iW/iH);
	
	if (ratio == endScale)
	{
		targ.width(maxW).height(maxH);
	}
	
	if (ratio > endScale)
	{
		var scaleH = maxH/iH;
		var newW = Math.ceil(iW * scaleH);
		var newH = Math.ceil(iH * scaleH);
		var leftAdj = 0 - Math.round((newW - maxW)/2);
		targ.width(newW).height(newH).css({position:'relative', left:leftAdj})
	}
	
	if (ratio < endScale)
	{
		var scaleW = maxW/iW;
		var newW = Math.ceil(iW * scaleW);
		var newH = Math.ceil(iH * scaleW);
		var topAdj = 0 - Math.round((newH - maxH)/2);
		targ.width(newW).height(newH).css({position:'relative', top:topAdj})
	}
	
}

function onCalendarFilter()
{
	var selected = $("#calendar_filter_select option:selected");      
    window.location = '/calendar?filterby=' + selected.val();
}

function onBlogFilter()
{
	var selected = $("#blog_filter option:selected");      
    window.location = '/blog?category=' + selected.val();
}
	
function checkCalendarFilter(inFilter)
{
	$("#calendar_filter_select option[value='" + inFilter +  "']").attr("selected", "selected");
}

function checkBlogFilter(inFilter)
{
	$("#blog_filter option[value='" + inFilter +  "']").attr("selected", "selected");
}

/////////##### COLOR FUNCTIONS #######////////
function randomHueHex(inVal)
{	
	var theVal = 100;
	if (inVal != null) theVal = inVal
	var theHex = "";
	var theRGB = hsv_to_rgb(Math.round(Math.random()*360), 100, theVal);
	theHex = rgb_to_hex_c(theRGB);
	return "#" + theHex;
}

function roundVal ( val, upper, lower)
{
	val = Math.round(val);
	return val > upper ? upper : val < lower ? lower : val;
}

function mix_rgb (c_1, c_2, m)
{
	var ret = [], i = 0;
    for( ; i < 3; i++ )
    {
    	ret[i] = c_1[i] + m * ( c_2[i] - c_1[i] );
    }
    return ret;
}

function hue_to_rgb (h)
{
	h = h % 360;
	var delta = h % 60;
	h -= delta;	
	delta = ( 255 / 60 * delta ) >> 0;
	if( h < 60 ) return [0xff, delta, 0];
	if( h < 120 ) return  [0xff - delta, 0xff, 0];
	if( h < 180 ) return  [0, 0xff, delta];
	if( h < 240 ) return  [0, 0xff - delta, 0xff];
	if( h < 300 ) return  [delta, 0, 0xff];
	if( h < 360 ) return  [0xff, 0, 0xff - delta];
}

function hsv_to_rgb ( h, s, v ) 
{
	var ret = hue_to_rgb(h)
	ret = mix_rgb(ret, [0xff, 0xff, 0xff], 1 - s / 100);
	ret = mix_rgb([0, 0, 0], ret, v/100);
	return ret;
}

function rgb_to_hex_c (c_arr)
{
	var ret = '', i = 0, c = 0;
	for( ; i < 3; i++ )
	{
	  c = roundVal(c_arr[i], 255, 0);
	  if( c > 255 ) c = 255;
	  ret +=  ( c < 16 ? '0' : '' ) + c.toString(16);
	}
	return ret;
}
