/** Code to run on page load, including image rollovers and anything else fancy */
var rounded_image_width = 33;
var rounded_image_height = 33;
		
var link_keys = [];

function show_random_item()
{
	var key = 0;
	
	var item = link_keys[key];
	
	$('.news_tmpl_links').eq(item)
		.animate({opacity:1})
		.hover(
			function(){
				$(this).animate({opacity:0.4});
			},
			function(){
				$(this).animate({opacity:1});
			}
		);
	
	link_keys.splice( key, 1 );
	
	if( link_keys.length > 0 )
	{
		setTimeout( show_random_item, 200 );
	}
}
function show_random_brand()
{
	var key = 0;
	
	var item = link_keys[key];
	
	$('#mainclm div p img').eq(item)
		.animate({opacity:1})
		.hover(
			function(){
				$(this).animate({opacity:0.4});
			},
			function(){
				$(this).animate({opacity:1});
			}
		);
	
	link_keys.splice( key, 1 );
	
	if( link_keys.length > 0 )
	{
		setTimeout( show_random_brand, 200 );
	}
}

function page_setup() {

	if(page_setup_fired) return false;
	page_setup_fired = true;
	
	$( '#mainmenu' ).menu_h_h({
		effect : 'show' 
	});
	
	/** add rounded corners to images */
	$( 'img.rounded, .rounded img' ).each( function(){
	
		if( $(this).attr('complete') )
		{
			roundedCorners( $(this) );
		}
		else
		{
			$(this).load(
				function()
				{
					roundedCorners( $(this) );
				}
			);
		}
	
	});
	
	if( $('.page_home #home_flash_banner').length > 0 )
	{
		// var flashvars = {};
		// flashvars.settingsXML = "filelib/flash/settings.xml";
		// var params = {};
		// params.scale = "noscale";
		// params.salign = "tl";
		// params.wmode = "transparent";
		// swfobject.embedSWF("filelib/flash/botb_banner.swf", "home_flash_banner", "263", "407", "8.0.0", false, flashvars, params, {});
		
		var so = new SWFObject("filelib/flash/botb_banner.swf", "botb_banner", "263", "407", "8", "#000000");
		so.addVariable("settingsXML", "filelib/flash/settings.xml");
		so.useExpressInstall('media/js/swfobject/expressinstall.swf');
		so.write("home_flash_banner");
	}
	
	if( $('.page_links').length > 0 )
	{
		$( function(){
		
			
			var counter = 0;
			$('.news_tmpl_links').each( function(){
				$(this).css({ 'opacity': 0});
				link_keys.push( counter );
				counter++;
			});
			show_random_item();
		
		});
	}
	
	if( $('.page_brands').length > 0 )
	{
		$( function(){
		
			var counter = 0;
			$('#mainclm div p img').each( function(){
				$(this).css({ 'opacity': 0});
				link_keys.push( counter );
				counter++;
			});
			show_random_brand();
		});
	}
}
var page_setup_fired = false;
$(document).ready(page_setup);

function roundedCorners( $element )
{
	/** fetch the width and height of the image in question */
	var w = $($element).attr('width');
	var h = $($element).attr('height');
	
	/** wrap a relatively positioned div around the image */
	$($element).wrap( '<div class="rounded_wrapper" style="position: relative;"></div>' );
	
	var dimensions = 'position: absolute; width: '+rounded_image_width+'px; height: '+rounded_image_height+'px; ';
	
	/** overlay a structure of divs over the image, with rounded corner images */
	var corners =  '<div class="rounded_tl rounded_corner" style="'+dimensions+' top: 0; left: 0;"></div>';
		corners += '<div class="rounded_tr rounded_corner" style="'+dimensions+' top: 0; left: '+(w-rounded_image_width)+'px;"></div>';
		corners += '<div class="rounded_bl rounded_corner" style="'+dimensions+' top: '+(h-rounded_image_height)+'px; left: 0;"></div>';
		corners += '<div class="rounded_br rounded_corner" style="'+dimensions+' top: '+(h-rounded_image_height)+'px; left: '+(w-rounded_image_width)+'px;"></div>';
	
	$( corners ).insertAfter( $($element) );
}

