var ie6 = ($.browser.msie && $.browser.version < 7);

$(document).ready(function(){
	
	// Replaces the H2 title with a flash title
	try {
		if ($.swfobject.getFlashPlayerVersion().major >= 9) {

			var color = $.fmtColor($('h2').css('color'), 'hexadecimal');
			var title_txt = $('h2').text();

			$('h2').html('<div id="h2-wrapper"><div id="h2-flash" /></div>');
			
			$.swfobject.embedSWF("media/flash/title-h2.swf?c="+new Date().getTime(), "h2-flash", "100%", "29", "9.0.0", '', {
				mytitle:title_txt,
				color:color,
				resizeFunc:'resizeTitle',
				targetElement:'h2-wrapper',
				lang:'en'
			}, {wmode:'transparent'});
			
			throw('finished replacing the title with the flash version');
			
		} else {
			throw('old flash player');
		}
		
	}catch(e) {
		$('h2').css('visibility', 'visible');
	}	
	
	// Fix the foposition on ie6
	if (ie6) {
		$('ul#navigation ul[foposition]').each(function(){
			var p = $(this).attr('foposition');
			var x = Math.round(p.match(/^\-{0,1}[0-9]{1,6}/));
			var y = Math.round(p.match(/\-{0,1}[0-9]{1,6}$/));
			$(this).attr('foposition', x+','+(y-23));
		});
	}	
	
	// Flyouts
	$('ul#navigation, ul#tools').flyouts();
	
	// Fixing IE6 png logo
	if (ie6) $("h1, ul#footer-tools, ul#worldmap, div#header-image img, ul#lhs-hero li img, div#content img, div#search").pngfix();
	
	// Preload the navigation and tools rollovers
	$(window).bind('load', function(){
	
		$('ul#navigation > li > a, ul#tools > li > a, ul#content-footer > li > a').each(function(){
			if ($(this).css('background-image') != 'none') {
				var file = $(this).css('background-image').match(/url\((.{0,})\.(.{3})\)/);
				if (file) {
					file = file[1] + '_.' + file[2];
					$('<img>').attr('src', file);
				}
			}
		});
	
	});
	
	// Apply the pbbox
	$(window).bind('load', function(){
		if ($('a[class=pbbox]')) $.pbBox({showclose:true});
	});
	
	// Search field
	$('a.search').bind('click', function(){
		$('div#search').slideDown(200, function(){
			$('#searchinput').select();
			$('#searchinput').focus();
		});
	});
	
	$('#searchinput').bind('blur', function(){
		$('div#search').hide();
	});
	
	//Content footer tooltips (does not run in IE6)
	if (!ie6) {
	
		$('#content-footer > li > a').each(function(){
		
			var attr = $(this).attr('title');
			var tt = $('<div class="'+attr+'"></div>');
			
			tt.css({
				'margin':'0',
				'padding':'0',
				'width':'121px',
				'height':'27px',
				'background-repeat':'no-repeat',
				'position':'absolute',
				'background-image':'url(media/images/tooltip-'+attr.replace(/\s/g, '-')+'.png)',
				'display':'none'
			});
			
			$(this).hover(
				function(){
					$(this).data("tt").css({
						'left':($(this).offset().left-9)+'px',
						'top':($(this).offset().top-23)+'px',
						'display':'block'
					});
				},
				function(){$(this).data("tt").css('display', 'none')}
			);
			
			$(this).attr('title', '');
			$(this).data('tt', tt);
			$(document.body).append(tt);
		
		});
	
	}
	
});


function bookmark() {
	var url = window.location;
	var title = 'this.title';
	
	try {
		window.sidebar.addPanel(title, url,"");
	} catch(e) {
		try {
			window.external.AddFavorite(url, title);
		} catch(e) {
			alert('Unfortunately, this browser does not support the requested action,'
			+ ' please bookmark this page manually.');
		}
	}
	
}

function printpage() {
	$("div#content").jPrint();
}

function forwardtoafriend() {

	//alert('forward to a friend is coming soon!');

	window.location = "http://durian/TDIC/Saadiyat_Island_Website_D4182/Online_Correct_D4182/en/Content/Contact_Us/Forward_To_A_Friend.aspx";
	
}

function resizeTitle(bounds) {
	try {
		
		$(bounds.element).css({
		
			width:bounds.width
		
		});
		
	}catch(e){}
}