/*=========================================*/
/*	infoBox CONSANTS	*/
/*=========================================*/
var OFFSET_TOP = 0,
	SCROLL_TOP = 0,
	SCROLL_LEFT = 0,
	MOUSE_X = 0,
	MOUSE_Y = 0;
/*=========================================*/
/*	infoBox IMAGES	*/
/*=========================================*/	
//Preloading images
var infoBoxImages = new Array(new Image(207,30), new Image(40,30), new Image(207,180), new Image(207,31), new Image(207,3), new Image(90, 108));
infoBoxImages[0].src = "http://system.fastcinema.com/infoBox/images/header.png";
infoBoxImages[1].src = "http://system.fastcinema.com/infoBox/images/header_right.png";
infoBoxImages[2].src = "http://system.fastcinema.com/infoBox/images/body.png";
infoBoxImages[3].src = "http://system.fastcinema.com/infoBox/images/footer.png";
infoBoxImages[4].src = "http://system.fastcinema.com/infoBox/images/line.jpg";
infoBoxImages[4].src = "http://system.fastcinema.com/infoBox/images/stars.jpg";

/*=========================================*/
/*	ASSIGNING infoBox FUNCTIONS	*/
/*=========================================*/
$(document).ready(function()
{
	//Loading CSS for infoBox
	loadInfoBoxCSS();
});

$(window).load(function()
{
	$('.infoBox').each(function(index)
	{
		//Register event handlers
		$(this).mouseover(function(event)
		{		
			MOUSE_Y = event.clientY;
			MOUSE_X = event.clientX;
			showInfoBox(this);
		});
		
		$(this).mousemove(function(event)
		{	
			MOUSE_Y = event.clientY;
			MOUSE_X = event.clientX;
			setTimeout("moveInfoBox()", 0);
		});
		
		$(this).mouseout(function()
		{
			hideInfoBox();
		});
	});
});
/*=========================================*/
/*	infoBox FUNCTIONS	*/
/*=========================================*/

//Loading CSS
function loadInfoBoxCSS()
{
	$('head').append('<link rel="stylesheet" href="http://system.fastcinema.com/infoBox/css/IB_default.css" type="text/css" />');
	//If browser is IE6
	if(navigator.userAgent.toLowerCase().indexOf("msie 6") != -1 && 
	navigator.userAgent.toLowerCase().indexOf("msie 7") == -1)
	{
		$('head').append('<link rel="stylesheet" href="http://system.fastcinema.com/infoBox/css/IB_ie.css" type="text/css" />');
	}
}
//Show new infoBox
function showInfoBox(obj)
{
	//Getting moive's id
	var pattern = /bid=(\d+)/;
	if(!pattern.test($(obj).parent().attr('href')))
	{
		pattern = /-(\d+)\.html/;
	}
	pattern.exec($(obj).parent().attr('href'));
	var moviedID = RegExp.$1;
	
	
	$('body').append('<div class="IB_window"></div>');
	$('.IB_window')
		.append('<h2 class="IB_wHeader"><span></span></h2>')
		.append('<div class="IB_wBody"></div>')
		.append('<div class="IB_wFooter">powered by Fastcinema.com</div>');
	$('.IB_wBody')
		.append('<p class="IB_bLine"></p>')
		.append('<p class="IB_bDescription"><span class="IB_dOverlay"></span></p>')
		.append('<p class="IB_bRate"></p>')
		.append('<p class="IB_bInfo"></p>');

	$('.IB_wHeader').append(unquote(MOVIES[moviedID]['title']));
	$('.IB_bDescription').append(unquote(MOVIES[moviedID]['description']));
	$('.IB_bRate').append('<span class="star_'+ MOVIES[moviedID]['rating'] +'"></span>');
	$('.IB_bInfo').append('<strong class="underlined">Starting</strong>: <i>'+ unquote(MOVIES[moviedID]['starring']) +'</i><br/><strong class="underlined">Genre</strong>: <i>'+ unquote(MOVIES[moviedID]['genre']) +'</i>');
	
	OFFSET_TOP = ($('.IB_window').height() + $(document).scrollTop() + MOUSE_Y + 20 > $(document).scrollTop() + $(window).height())?($('.IB_window').height() + 20)*(-1):0;
	SCROLL_TOP = $(document).scrollTop();
	SCROLL_LEFT = $(document).scrollLeft();

	if(navigator.userAgent.toLowerCase().indexOf("msie 6") != -1 && 
	navigator.userAgent.toLowerCase().indexOf("msie 7") == -1)
	{
		$('.IB_window').css({top: (SCROLL_TOP + MOUSE_Y + OFFSET_TOP + 20), left: (SCROLL_LEFT + MOUSE_X + 20)}).show();
	}
	else
	{
		$('.IB_window').css({top: (SCROLL_TOP + MOUSE_Y + OFFSET_TOP +20), left: (SCROLL_LEFT + MOUSE_X + 20)}).fadeIn('slow');
	}
}
//Move infoBox
function moveInfoBox()
{
	$('.IB_window').css({top: (SCROLL_TOP  + MOUSE_Y + OFFSET_TOP + 20) , left: (SCROLL_LEFT + MOUSE_X + 20)});
}
//hide infoBox
function hideInfoBox()
{
	$('.IB_window').empty().remove();
}
//Replace HTML quote and apostrof entites with character
function unquote(str)
{
	str = str.replace(/&#34;|&amp;#34;/, '"');
	str = str.replace(/&#39;|&amp;#39;/, "'");
	
	return str;
}
 