/************************************************************************\
 *
 * NuLook
 * 
 * Copyright ©2006-2007 NuHit, LLC. All Rights Reserved.
 * This file may not be redistributed in whole or significant part.
 * http://www.nuhit.com | http://www.nuhit.com/license.html
 *
\************************************************************************/

var nulook_inited = false;
var nulook_using_ie = false;
var nulook_using_ff_15 = false;
var nulook_bubble_queue = new Array();

var nulook_all_posts_id = 'posts';
var nulook_all_posts_obj = null;

var nulook_row_width = 0;

var nu_log = "Log:";

var nulook_cell_count = 0;
var nulook_row_cells = new Array();

var all_posts_width = 200;
var nulook_cell_width = 100;

var nulook_post_queue = new Array();

var nulook_previous_onload = null;
var nulook_previous_PostBit_Init = null;
var nulook_ie_vml_renderer = null;

function nulook_enqueue_post( post_id, bubble_border, bubble_background, bubble_background_lt, bubble_background_dk, is_last_post )
{
	if (!nulook_inited)
		nulook_initialize();

	var post_info = new Object();
	post_info.post_id = post_id;
	post_info.bubble_border = bubble_border;
	post_info.bubble_background = bubble_background;
	post_info.bubble_background_lt = bubble_background_lt;
	post_info.bubble_background_dk = bubble_background_dk;
	nulook_post_queue.push( post_info );
	
	if (!nulook_using_ie && !nulook_using_ff_15)		// in FF/opera, draw right away, in IE, wait for VML namespace to load.
		nulook_process_post( post_id, bubble_border, bubble_background, bubble_background_lt, bubble_background_dk, is_last_post );
}

function nulook_find_post_by_id( post_id )
{
	var post_count = nulook_post_queue.length;
	for ( var post_ix = 0; post_ix < post_count; post_ix++ )
	{
		var is_last = post_ix == (post_count-1);
		var post_info = nulook_post_queue[ post_ix ];
		if (post_info.post_id == post_id)
			return post_info;
	}
	return null;
}

function nulook_process_post_from_id( post_id )
{
	var post_info = nulook_find_post_by_id( post_id );
	if (post_info)
		nulook_process_post( post_info.post_id, post_info.bubble_border, post_info.bubble_background, 
								post_info.bubble_background_lt, post_info.bubble_background_dk, is_last );
}

function nulook_process_posts()
{
	var post_count = nulook_post_queue.length;
	for ( var post_ix = 0; post_ix < post_count; post_ix++ )
	{
		var is_last = post_ix == (post_count-1);
		var post_info = nulook_post_queue[ post_ix ];
		nulook_process_post( post_info.post_id, post_info.bubble_border, post_info.bubble_background, 
								post_info.bubble_background_lt, post_info.bubble_background_dk, is_last );
	}
}

function nulook_process_post( post_id, bubble_border, bubble_background, bubble_background_lt, bubble_background_dk, is_last )
{
	var nulook_post_ctr_id		= 'nulook_post_ctr_' + post_id;
	var nulook_canvas_ctr_id	= 'nulook_canvas_ctr_' + post_id;
	var nulook_canvas_id		= 'nulook_canvas_' + post_id;
	var nulook_post_msg_id		= 'nulook_post_msg_' + post_id;
	
	var nulook_post_ctr_obj		= fetch_object( nulook_post_ctr_id );
	var nulook_canvas_ctr_obj	= fetch_object( nulook_canvas_ctr_id );
	var nulook_canvas_obj		= fetch_object( nulook_canvas_id );
	var nulook_post_msg_obj		= fetch_object( nulook_post_msg_id );
	
	if (!nulook_post_ctr_obj || !nulook_canvas_ctr_obj || !nulook_canvas_obj || !nulook_post_msg_obj)
		return;
		
	if (!nulook_canvas_obj.getContext && nulook_using_ie && nulook_ie_vml_renderer)		// try to initialize VML for this canvas.
	{
		nulook_ie_vml_renderer.initElement( nulook_canvas_obj );
		nulook_canvas_obj = fetch_object( nulook_canvas_id );
		if (!nulook_canvas_obj)
			return;
	}

	var canvas_width = nulook_post_ctr_obj.offsetWidth;
	var canvas_height = nulook_post_ctr_obj.offsetHeight;
	
	nulook_canvas_ctr_obj.style.width = canvas_width + 'px';
	nulook_canvas_ctr_obj.style.height = canvas_height + 'px';
	
	nulook_canvas_obj.style.width = canvas_width + 'px';
	nulook_canvas_obj.style.height = canvas_height + 'px';

	nulook_canvas_obj.width = canvas_width;
	nulook_canvas_obj.height = canvas_height;
	
	// get source object
	var nulook_avatar_id = 'nulook_avatar_' + post_id;
	var nulook_source_obj = fetch_object( nulook_avatar_id );
	if (!nulook_source_obj)
	{
		var nulook_username_id = 'postmenu_' + post_id;
		nulook_source_obj = fetch_object( nulook_username_id );
	}

	var source_pos = nulook_fetch_position( nulook_source_obj, nulook_post_ctr_obj );

	// get message dimensions
	var message_pos = nulook_fetch_position( nulook_post_msg_obj, nulook_post_ctr_obj );

	nulook_draw_wrapper( nulook_canvas_obj, message_pos, source_pos, bubble_border, bubble_background, bubble_background_lt, bubble_background_dk );
}

function nulook_draw_wrapper( canvas, message_pos, source_pos, bubble_border, bubble_background, bubble_background_lt, bubble_background_dk )
{
	if (!canvas || !message_pos)
		return;

	if (!canvas.getContext)
	{
		//alert('You need a canvas enabled browser.');
		return;
	}

	var ctx = canvas.getContext('2d');
	if (!ctx)
		return;

	canvas.style.display = "block";		// needed for FF 1.5

	var radius = nl_bubble_radius;
	if (nulook_using_ie)
		radius = radius * .8;
		
	radius = Math.min( radius, Math.floor(message_pos.height/6) );
	var x = message_pos.left + radius;
	var y = message_pos.top	+ radius;
	var w = message_pos.width	- radius*2;
	var h = message_pos.height	- radius*2;
	
	var dx = Math.min( radius, h/6 );
	var dy = Math.min( radius, w/6 );
	var t = Math.min( nl_connection_size, h/2 );
		
	var source_x, source_y;
	var conn_at_left = true;

	if (source_pos)
	{
		if (source_pos.left < (x+w) )
			source_x =  (source_pos.left + source_pos.width + nl_cell_padding);
		else
		{
			source_x = (source_pos.left - nl_cell_padding);
			conn_at_left = false;
		}
		source_y = source_pos.top	+ source_pos.height / 2;
	}
	else
	{
		source_x = x - 20;
		source_y = y + h / 2;
	}

	var control_x = radius / Math.tan( Math.PI/4 );
	var control_y = radius / Math.tan( Math.PI/4 );
	
	var x2 = x + w, y2 = y + h, xm = x + w/2, ym = y + h/2;
	
	ctx.beginPath();
	ctx.moveTo( x, y );
	ctx.quadraticCurveTo( x + control_x,	y - dy, 		xm,			y - dy );
    ctx.quadraticCurveTo( x2 - control_x,	y - dy, 		x2,			y );
    
    if (conn_at_left)
		ctx.quadraticCurveTo( x2 + dx,			y + control_y, 	x2 + dx,	ym );
	else
	{
		ctx.quadraticCurveTo( x2 + dx,			y + control_y, 	x2 + dx,	ym - t/2);
		ctx.lineTo( source_x, source_y );
		ctx.lineTo( x2 + dx, ym + t/2 );
	}
    ctx.quadraticCurveTo( x2 + dx,			y2 - control_y, x2,			y2 );
    ctx.quadraticCurveTo( x2 - control_x,	y2 + dy, 		xm,			y2 + dy );
    ctx.quadraticCurveTo( x + control_x,	y2 + dy, 		x,			y2 );

    if (conn_at_left)
    {
	    ctx.quadraticCurveTo( x - dx,			y2 - control_y, x - dx,		ym + t/2 );
		ctx.lineTo( source_x, source_y );
		ctx.lineTo( x - dx,		ym - t/2 );
	}
	else
		ctx.quadraticCurveTo( x - dx,			y2 - control_y, x - dx,		ym );

    ctx.quadraticCurveTo( x - dx,			y + control_y, 	x,			y );

	//ctx.globalAlpha = 0.8;
	if (bubble_background && (bubble_background != ''))
	{
		if (nl_use_gradient)
		{
			var m = Math.min( w, h );
			var lineargradient = ctx.createLinearGradient( x, y, x, y2);
			
			lineargradient.addColorStop(0,	 bubble_background_lt );
			lineargradient.addColorStop(0.3, bubble_background );
			lineargradient.addColorStop(1,	 bubble_background_dk );
			ctx.fillStyle = lineargradient;
		}
		else
			ctx.fillStyle = bubble_background;
		ctx.fill();
	}
	
	if (bubble_border && (bubble_border != ''))
	{
		if (nulook_using_ie)
		{
			ctx.beginPath();
			ctx.moveTo( x, y );
			ctx.quadraticCurveTo( x + control_x,	y - dy, 		xm,			y - dy );
			ctx.quadraticCurveTo( x2 - control_x,	y - dy, 		x2,			y );
		    
			if (conn_at_left)
				ctx.quadraticCurveTo( x2 + dx,			y + control_y, 	x2 + dx,	ym );
			else
			{
				ctx.quadraticCurveTo( x2 + dx,			y + control_y, 	x2 + dx,	ym - t/2);
				ctx.lineTo( source_x, source_y );
				ctx.lineTo( x2 + dx, ym + t/2 );
			}
			ctx.quadraticCurveTo( x2 + dx,			y2 - control_y, x2,			y2 );
			ctx.quadraticCurveTo( x2 - control_x,	y2 + dy, 		xm,			y2 + dy );
			ctx.quadraticCurveTo( x + control_x,	y2 + dy, 		x,			y2 );

			if (conn_at_left)
			{
				ctx.quadraticCurveTo( x - dx,			y2 - control_y, x - dx,		ym + t/2 );
				ctx.lineTo( source_x, source_y );
				ctx.lineTo( x - dx,		ym - t/2 );
			}
			else
				ctx.quadraticCurveTo( x - dx,			y2 - control_y, x - dx,		ym );

			ctx.quadraticCurveTo( x - dx,			y + control_y, 	x,			y );
			
		}
		ctx.strokeStyle = bubble_border;
		ctx.stroke();
	}
}

function nulook_fetch_position( obj, parent_obj )
{
	var left = obj.offsetLeft;
	var top = obj.offsetTop;
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	
	while (((obj = obj.offsetParent) != null) && (obj != parent_obj))
	{
		left += obj.offsetLeft;
		top += obj.offsetTop;
	}

	return { 'left' : left, 'top' : top, 'width' : width, 'height' : height };
}


function nulook_initialize()
{
	if (nulook_inited)
		return;
	nulook_inited = true;
	
	// fix for IE Canvas
	if (/MSIE/.test(navigator.userAgent) && !window.opera)
		nulook_using_ie = true;
	else if (/FireFox\/1.5/.test(navigator.userAgent))
		nulook_using_ff_15 = true;

	if (nulook_using_ie || nulook_using_ff_15)
	{
		nulook_previous_onload = window.onload;
		window.onload = nulook_ie_onload;
	}
}

function nulook_end_of_page()
{
	//// hook quick editor
	//if (vB_QuickEditor && vB_QuickEditor.nulook_previous_qe_restore == undefined)
	//{
	//	vB_QuickEditor.nulook_previous_qe_restore = vB_QuickEditor.restore;
	//	vB_QuickEditor.restore = nulook_quickeditor_restore;
	//}

	// hook PostBit_Init
	if (nulook_previous_PostBit_Init == undefined)
	{
		nulook_previous_PostBit_Init = PostBit_Init;
		PostBit_Init = nulook_PostBit_Init;
	}
}

function nulook_quickeditor_restore( post_html, type )
{
	if (!vB_QuickEditor)
		return;
		
	if (vB_QuickEditor.nulook_previous_qe_restore)
		vB_QuickEditor.nulook_previous_qe_restore( post_html, type );
	
	var post_id = vB_QuickEditor.postid;

	// now re-draw wrapper
	nulook_process_post_from_id( post_id );
}

function nulook_PostBit_Init(obj, postid)
{
	if (nulook_previous_PostBit_Init)
		nulook_previous_PostBit_Init( obj, postid );
		
	// now, draw wrapper, use first post's colors.
	if (nulook_post_queue.length == 0)
		return;
		
	var post_info = nulook_find_post_by_id( postid );
	if (!post_info)
	{
		var ref_info = nulook_post_queue[0];
		
		var post_info = new Object();
		post_info.post_id = postid;
		post_info.bubble_border		= ref_info.bubble_border;
		post_info.bubble_background = ref_info.bubble_background;
		post_info.bubble_background_lt = ref_info.bubble_background_lt;
		post_info.bubble_background_dk = ref_info.bubble_background_dk;
		nulook_post_queue.push( post_info );
	}
	
	nulook_process_post( post_info.post_id, post_info.bubble_border, post_info.bubble_background, 
								post_info.bubble_background_lt, post_info.bubble_background_dk, true );
}

function nulook_ie_onload()
{
	nulook_process_posts();
	if (nulook_previous_onload)
		nulook_previous_onload();
}

function nulook_resize_post( post_id, nulook_canvas_ctr_id, nulook_user_id, post_message_id, td_post_id, is_last_post )
{
	if (!nulook_all_posts_obj)
	{
		nulook_all_posts_obj = fetch_object( nulook_all_posts_id );
		if (!nulook_all_posts_obj)
			return;
		all_posts_width = nulook_all_posts_obj.offsetWidth - 2*nl_spacer_size - 20;
		nulook_cell_width = Math.floor( all_posts_width / nl_posts_per_row );
	}
	
	var post_obj = fetch_object( post_id );
	var post_message_obj = fetch_object( post_message_id );
	if (!post_obj || !post_message_obj)
		return;

	if (nl_posts_per_row <= 0)
		nl_posts_per_row = 1;

	var post_width = nulook_cell_width;
	
	if (nl_debug_mode)
	{
		if (nulook_cell_count == 0)
			nu_log += "\nnulook_all_posts_obj.offsetWidth = " + nulook_all_posts_obj.offsetWidth + "spacer = " + nl_spacer_size 
						+ ", all_posts_width = " + all_posts_width + ", cell_width = " + nulook_cell_width + "\n";
	}
	
	if (nulook_cell_width <= 0)
		return;
	
	if (nulook_row_width + post_width >= (all_posts_width - 2*nl_posts_per_row))	// last cell
		post_width = all_posts_width - nulook_row_width;
		
	post_obj.style.width = post_width + 'px';
	
	// Now, analyze if the post content is just too wide or too tall.
	var post_message_left = post_message_obj.offsetLeft;
	var post_message_top = post_message_obj.offsetTop;
	var post_message_width = post_message_obj.offsetWidth;
	var post_message_height = post_message_obj.offsetHeight;
	

	var ideal_cell_height = nulook_cell_width * 3 / 4;	
	var cells_it_should_use_x = Math.min( Math.floor( (post_message_left + post_message_width + nulook_cell_width-1) / nulook_cell_width ), nl_posts_per_row );
	var cells_it_should_use_y = Math.min( Math.floor( (post_message_height + ideal_cell_height-1) / ideal_cell_height ), nl_posts_per_row );
	
	var cells_it_should_use = Math.max( cells_it_should_use_x, cells_it_should_use_y );

	post_width = cells_it_should_use * nulook_cell_width;
	
	if (cells_it_should_use == nl_posts_per_row)
		post_width = all_posts_width;
		
	if (nl_debug_mode)
	{
		nu_log += "\nPost #" + nulook_cell_count;
		nu_log += "\n\tpost_message: (" + post_message_left + ", " + post_message_top + ")x(" + post_message_width + "x" + post_message_height + ")";
		nu_log += "\n\tcells_it_should_use: (" + cells_it_should_use_x + "x" + cells_it_should_use_y + ") => " + cells_it_should_use;
		nu_log += "\n\tnulook_row_width: " + nulook_row_width + ", post_width = " + post_width;
	}

	if ((nulook_row_width + post_width) > all_posts_width)		// then this cell should be moved to a new row
	{
		nulook_equalize_row( all_posts_width );
		post_obj.style.clear = 'left';
		
		if (nl_debug_mode)
		{
			//post_obj.style.border= "1px solid blue";
			nu_log += "\n\t*going to new line: nulook_row_width + post_width = " + (nulook_row_width + post_width);
		}
		
		nulook_row_width = 0;
	}
	else if ((nulook_row_width + post_width) >= (all_posts_width - 2*nl_posts_per_row))	// flush last cell
	{
		post_width = all_posts_width - nulook_row_width;
		if (nl_debug_mode)
		{
			nu_log += "\n\t*flushing last cell in row";
		}
	}		

	var adj_post_width = post_width - 2*nl_post_border_width;
	post_obj.style.width = adj_post_width + 'px';
	
	nulook_row_width += post_width;
	
	// resize post due to absolute positioning
	post_message_height = post_message_obj.offsetHeight;
	
	if (nl_debug_mode)
	{
		nu_log += "\n\tnew post width = " + post_width + ", adjusted = " + adj_post_width + "resulting height = " + post_message_height;
	}

	var nulook_user_obj = fetch_object( nulook_user_id );
	var userinfo_height = nulook_user_obj ? nulook_user_obj.offsetWidth : 0;
	
	var body_height = Math.max( userinfo_height, post_message_height );
	
	var nulook_canvas_ctr_obj = fetch_object( nulook_canvas_ctr_id );
	if (nulook_canvas_ctr_obj)
		nulook_canvas_ctr_obj.style.height = body_height + 'px';
	nulook_cell_count++;
	
	var cell = new Object();
	cell.post_obj = post_obj;
	cell.nulook_canvas_ctr_obj = nulook_canvas_ctr_obj;
	cell.adj_post_width = adj_post_width;
	cell.td_post_obj = fetch_object( td_post_id );
	nulook_row_cells.push( cell );

	if (is_last_post)
	{
		nulook_equalize_row( all_posts_width );
		//window.alert( nu_log );
		//document.write( "<textarea cols=20 rows=20>" + nu_log + "</textarea>" );
	}
}

function nulook_equalize_row( all_posts_width )
{
	var cell_count = nulook_row_cells.length;
	if (!cell_count)
		return;
	
	var extra_width = all_posts_width - nulook_row_width;		// how much we need to fill up
	var extra_width_per_post = Math.floor( extra_width / cell_count );
	
	nu_log += "\n\nEqualizing row. nulook_row_width = " + nulook_row_width + " extra_width = " + extra_width
					+ " extra_width_per_post = " + extra_width_per_post;
	
	var max_canvas_height = 0;
	var max_td_post_height = 0;
	for (var post_ix = 0; post_ix < cell_count; post_ix++)
	{
		var cell = nulook_row_cells[ post_ix ];
		if (cell && cell.post_obj && cell.adj_post_width)
		{
			var adj_post_width = cell.adj_post_width;
			
			nu_log += "\n\tcell #" + post_ix + ": adj_post_width = " + adj_post_width;
			
			if (post_ix == (cell_count - 1))
				adj_post_width += extra_width;
			else
				adj_post_width += extra_width_per_post;
			
			cell.post_obj.style.width = adj_post_width + 'px';
			extra_width -= extra_width_per_post;
			
			if (cell.nulook_canvas_ctr_obj && (cell.nulook_canvas_ctr_obj.offsetHeight > max_canvas_height))
				max_canvas_height = cell.nulook_canvas_ctr_obj.offsetHeight;

			if (cell.td_post_obj && (cell.td_post_obj.offsetHeight > max_td_post_height))
				max_td_post_height = cell.td_post_obj.offsetHeight;

			nu_log += "new_adj_post_width = " + adj_post_width + " height = " + cell.nulook_canvas_ctr_obj.offsetHeight
						+ "/" + max_canvas_height;
		}
	}
	
	// now resize heights
	for (var post_ix = 0; post_ix < cell_count; post_ix++)
	{
		var cell = nulook_row_cells[ post_ix ];
		if (cell && cell.post_obj && cell.nulook_canvas_ctr_obj && cell.adj_post_width && cell.td_post_obj)
		{
			cell.td_post_obj.style.height = max_td_post_height + 'px';
			cell.nulook_canvas_ctr_obj.style.height = max_canvas_height + 'px';
			
			nu_log += "\n\t\tsetting canvas height to " + max_canvas_height;
		}
	}
	
	// empty row
	nulook_row_cells = new Array();
}

function nulook_enqueue_bubble( canvas_id, text_id, source_id, source_x_pos, source_y_pos, bubble_type, border_color, bg_color )
{
	// see if canvas is ready
	var canvas_obj	= nulook_get_movie( canvas_id );
	if (canvas_obj && (canvas_obj.nuhit_add_bubble != undefined))
		nulook_add_bubble( canvas_id, text_id, source_id, source_x_pos, source_y_pos, bubble_type, border_color, bg_color )
	else
	{
		var b = new Object();
		b.canvas_id		= canvas_id;
		b.text_id		= text_id;
		b.source_id		= source_id;
		b.source_x_pos	= source_x_pos;
		b.source_y_pos	= source_y_pos;
		b.bubble_type	= bubble_type;
		b.border_color	= border_color;
		b.bg_color		= bg_color;
		nulook_bubble_queue.push( b );
	}
}

function nulook_add_bubble( canvas_id, text_id, source_id, source_x_pos, source_y_pos, bubble_type, border_color, bg_color )
{
	var canvas_obj	= nulook_get_movie( canvas_id );
	var text_obj	= fetch_object( text_id );
	var source_obj	= fetch_object( source_id );
	
	if (!canvas_obj || !text_obj)
		return;

	var left	= text_obj.offsetLeft;
	var top		= text_obj.offsetTop;
	var width	= text_obj.offsetWidth;
	var height	= text_obj.offsetHeight;
	var radius	= Math.min( nl_bubble_radius, Math.floor(height/6) );

	var source_x = -1;
	var source_y = -1;
	if (source_obj)
	{
		source_x = source_obj.offsetLeft + (source_obj.offsetWidth * source_x_pos) / 100;
		source_y = source_obj.offsetTop + (source_obj.offsetHeight * source_y_pos) / 100;
	}
	
	canvas_obj.nuhit_add_bubble( left, top, width, height, bubble_type, source_x, source_y, border_color, bg_color, radius );
}

function nulook_get_movie( movieName )
{
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function nuhit_canvas_inited( canvas_id )
{
	setTimeout( "nulook_process_canvas( '" + canvas_id + "' )", 100 );
}

function nulook_process_canvas( canvas_id )
{
	for (var i=0; i<nulook_bubble_queue.length; i++)
	{
		var b = nulook_bubble_queue[ i ];
		if (b.canvas_id == canvas_id)
			nulook_add_bubble( b.canvas_id, b.text_id, b.source_id, 
					b.source_x_pos, b.source_y_pos, b.bubble_type, b.border_color, b.bg_color );
	}
}

function nulook_update_color( target_id, value )
{
	var obj = fetch_object( target_id );
	if (!obj)
		return;
		
	if (nulook_is_hex( value ))
	{
		obj.style.background = '#' + value;
	}
	else
		obj.style.backgroundColor = value;
}

function nulook_is_hex( str )
{
	var str_length = str.length;
	if (!str_length)
		return false;

	for (var i=0; i<str_length; i++)
	{
		var ch = str.substr(i, 1);
		var is_number = ((ch >= '0') && (ch <= '9'));
		var is_alpha = ((ch >= 'A') && (ch <= 'F')) || ((ch >= 'a') && (ch <= 'f'));
		if (!is_number && !is_alpha)
			return false;
	}
	return true;
}

function nulook_color_changed( myclass, element, value, modifier, cookie, fieldvalue )
{
	nulook_initialize_color_preview( fieldvalue );
}

function nulook_initialize_color_preview( input_id )
{
	var obj = fetch_object( input_id );
	if (!obj)
		return;
		
	if (obj.onkeyup != undefined)
		obj.onkeyup();
}

/************************************************************************\
 *
 * Downloaded: $Download_DateTime$
 * Licensed to: $License_SiteURL$
 * 
\************************************************************************/