if( /aboutus/.test(window.location.href) )
{
	var aboutus = true;
}
else
{
	var aboutus = false;
	loader = new Image();
	loader.src = 'styles/backgrounds/ajax-loader-2.gif';
	img_array = preloadImages(1);
	img_array2 = preloadImages(2);
	img_array3 = preloadImages(3);
}

function preloadImages(a)
{
	var img_array = new Array();
	
	if( a == 1 && /device/.test(window.location.href))
	{
		for(var i = 1; i <= 6; i++)
		{
			var img = new Image();
			img.src = 'styles/backgrounds/mda_button_' + i + '_over.png';
			img_array[i] = img;
		}	
	}
	
	if( a == 2  && /pharmaceutical/.test(window.location.href))
	{
		for(var i = 1; i <= 6; i++)
		{
			var img = new Image();
			img.src = 'styles/backgrounds/pma_button_' + i + '_over.png';
			img_array[i] = img;
		}	
	}
	
	if( a == 3 && /illustration/.test(window.location.href))
	{
		for(var i = 1; i <= 12; i++)
		{
			var img = new Image();
			img.src = 'styles/backgrounds/ill_button_' + i + '_over.png';
			img_array[i] = img;
		}
	}
	
	return img_array;
}

$(function()
{	
	if( aboutus == false )
	{
		$('#img').css({display: 'none'});
		$('#illustrations').prepend('<div id="loading"><img src="' + loader.src + '" /></div>');
		$('#loading')
		.css({
			textAlign: 'center', 
			color: '#666', fontWeight: 'bold', 
			display: 'block', 
			position: 'absolute',
			top: '130px',
			left: '270px'
		});
	}
	
	var img = new Image();
	$(img)
	.load(function()
	{
		$('#loading').fadeOut(function()
		{
			$('#img').fadeIn('slow');
		});
		
	});
	
	if( /illustration/.test(window.location.href) )
	{
		if( getQueryParam('id', '?') )
		{
			var id = getQueryParam('id', '?');
			img.src = 'styles/backgrounds/illustration_poster_' + id + '.png';
		}
		else
		{
				img.src = 'styles/backgrounds/illustration_poster_12.png';
		}
	}
	
	//var btnClicked = queryString('vn', '?');
	
	$('a[rel=mda_button], a[rel=pma_button], a[rel=ill_button]')
	.each(function()
	{
		var thisDiv = $(this).find('div');
		var id_attr = thisDiv.attr('id');
		var id_parts = id_attr.split('_');
		var id = parseInt(id_parts.pop());
		var btnName = thisDiv.attr('rel');
		
		if( /medical-device-animations/.test(window.location.href) || /pharmaceutical-animations/.test(window.location.href) )
		{
			var btnClicked = getQueryParam('vn', '?');
		}
		else
		{
			var btnClicked = getQueryParam('ill', '?');
		}
		
		$(this)
		.bind('mouseover', function()
		{
			if( /medical-device-animations/.test(window.location.href) )
			{
				thisDiv
				.css({
					background: 'URL(' + img_array[id].src + ') left top no-repeat'
				});
			}
			else if( /pharmaceutical-animations/.test(window.location.href) )
			{
				thisDiv
				.css({
					background: 'URL(' + img_array2[id].src + ') left top no-repeat'
				});
			}
			else
			{
				thisDiv
				.css({
					background: 'URL(' + img_array3[id].src + ') left top no-repeat'
				});
			}
		})
		.bind('mouseout', function()
		{
			thisDiv
			.css({
				background: 'URL(styles/backgrounds/' + id_attr + '.png) left top no-repeat'
			});
		});
		
		if( !btnClicked )
		{
			if( thisDiv.attr('rel') == 'terumo' || thisDiv.attr('rel') == 'psoriasis', thisDiv.attr('rel') == 'dna')
			{
				if( /medical-device-animations/.test(window.location.href) )
				{
					$(this).unbind();
					thisDiv
					.css({
						background: 'URL(' + img_array[id].src + ') left top no-repeat'
					});
				}
				else if( /pharmaceutical-animations/.test(window.location.href) )
				{
					$(this).unbind();
					thisDiv
					.css({
						background: 'URL(' + img_array2[id].src + ') left top no-repeat'
					});
				}
				else
				{
					$(this).unbind();
					thisDiv
					.css({
						background: 'URL(' + img_array3[id].src + ') left top no-repeat'
					});
				}
			}
		}
		else
		{
			if( btnClicked == thisDiv.attr('rel') )
			{
				if( /medical-device-animations/.test(window.location.href) )
				{
					$(this).unbind();
					thisDiv
					.css({
						background: 'URL(' + img_array[id].src + ') left top no-repeat'
					});
				}
				else if( /pharmaceutical-animations/.test(window.location.href) )
				{
					$(this).unbind();
					thisDiv
					.css({
						background: 'URL(' + img_array2[id].src + ') left top no-repeat'
					});
				}
				else
				{
					$(this).unbind();
					thisDiv
					.css({
						background: 'URL(' + img_array3[id].src + ') left top no-repeat'
					});
				}
			}
		}
	});
	
	//Temporarily disable hoverbox for illustration page
	if( !/illustration/.test(window.location.href) )
		$('a[title]').hoverbox({left: 50, top: -25});
	else
		$('a[rel=ill_button]').attr('title', '');
});

function getQueryParam(param, type)
{
	var sep = '&';
	var query_args = new Array();
	var query_string = '';
	
	if( param.length < 1 )
		return false;
	if( type == null || type == '' )
		type = '?';
	else
	{
		if( type == '?' )
			query_string = window.location.search.substring(1);
		else
			query_string = window.location.hash.substring(1);
	}
	
	if( query_string == null || query_string == '' )
		return false;
	var key_vals = query_string.split(sep);
	for( var i = 0; i < key_vals.length; i++ )
	{
		var arg = key_vals[i].split('=');
		if( arg[0] == param )
			return arg[1];
	}
}