var TreeSite = 
{
	onDocumentReady: function()
	{
		if (!this.isBadSafari())
		{
			this.safariCompat = function(){};
		}
		else
		{
			$('#home_layout #content').css('background', 'url(images/dummy/home_safari_replace.png) no-repeat');
			$('#my_tree_layout #flash_content_my_tree').css('background-image', 'url(images/dummy/my_tree_safari_replace.jpg)');
		}
		
		for (var element_id in this.elementBehaviours)
		{
			var elm = $(element_id);
			if (elm.size() > 0)
			{
				this.elementBehaviours[element_id](elm);
			}
		}
	},
	
	isBadSafari: function()
	{
		if (document.location.hash == '#safari=true')
		{
			return true;
		}
		else if (document.location.hash == '#safari=false')
		{
			return false;
		}
		var versionParts = $.browser.version.split('.');				
		return $.browser.safari && (parseInt(versionParts[0]) <= 419);
	},
	
	safariCompatElements: null,
	safariCompat: function(enable)
	{
		if (enable) 
		{
			if (this.safariCompatElements != null) 
			{
				this.safariCompatElements.css('visibility', 'hidden');
			}
		}
		else
		{
			if ($('#TB_window').not(':animated').size() > 0) 
			{			
			}
			else 
			{
				this.safariCompatElements.css('visibility', 'visible');
			}
		}
	},
	
	elementBehaviours: 
	{
		'#site_nav > ul > li': function($elms)
		{
			var onMouseOver = function()
			{
				$(this).children('ul').show();
				TreeSite.safariCompat(true);				
			};
			
			var onMouseOut = function()
			{
				$(this).children('ul').hide();
				TreeSite.safariCompat(false);				
			};
			
			$elms.hover(
				onMouseOver, onMouseOut
			);			
		},
		
		'#site_nav a.thickbox': function ($elms)
		{
			if (TreeSite.isBadSafari())
			{
				$elms.click(
					function()
					{
						$('#site_nav > ul > li > ul').hide();
					}
				);
			}
		},
		
		'#flash_content_home': function($elms)
		{
			$elms.flash
			(
				{
					src: getHomeUrl(),
					width:	800,
					height:	518,
					wmode:	'transparent'
				},
				{ 	version:	9	},
				function(htmlOptions)
				{
					$this = $(this);
					$this.html($.fn.flash.transform(htmlOptions));
					TreeSite.safariCompatElements = $elms;
				},
				function()
				{
				    $('#no_flash_content').show();
				}
			);
		},

		'#flash_content_home_TB': function($elms)
		{
			$elms.flash(
				{
					src:	'swf/explore.swf',
					width:	800,
					height:	518,
					wmode:	'transparent'
				},
				{ 	version:	9	},
				function(htmlOptions)
				{
					$this = $(this);
					$this.html($.fn.flash.transform(htmlOptions));
					TreeSite.safariCompatElements = $elms;
				},
				function() { }
			);			
		},
		
		'#flash_content_my_tree': function($elms)
		{
			$elms.flash(
				{
					src:	'swf/my_tree_proof.swf',
					width:	335,
					height:	827,
					wmode:	'transparent'
				},
				{ 	version:	9	},
				function(htmlOptions)
				{
					$this = $(this);
					$this.html($.fn.flash.transform(htmlOptions));
					TreeSite.safariCompatElements = $elms.children('embed, object');
				},
				function() { }
			);
			
		},
		
		'#content_my_tree .page .menu': function($elms)
		{
			$elms.tabs();
			$elms.tabs('select', 3);
		},
		
		'#content_join #step_1 label': function($elms)
		{
			var onMouseOver = function(e)
			{
				var className = $(this).attr('id');
				
				$('.treetip:visible').not('#treetip_' + className).hide();
				
				var $tip = $('#treetip_' + className);
				$tip.show();
				
				var pos = {cx: e.clientX, cy: e.clientY, px: e.pageX, py: e.pageY};
				
				var leftOffset = 10;
				var topOffset = 10;
				var posY, posY, ww = $(window).width(), wh = $(window).height(), $op, opo;
				
				$op = $tip.offsetParent();
				opo = $op.offset();
				
				posX = pos.cx;
				posY = pos.cy;
				
				posX += leftOffset - opo.left - $op[0].scrollLeft;
					
				posY += topOffset - opo.top - $op[0].scrollTop;
				if ( ww < pos.cx + $tip[0].clientWidth + leftOffset )
				{
					var wsl = $(window).scrollLeft();
					posX -= $tip[0].clientWidth + leftOffset * 2;
					if ( opo.left - wsl + posX < 0 )
					{
						posX -= opo.left - wsl + posX;
					}
				}
				if ( wh < pos.cy + $tip[0].clientHeight + topOffset )
				{
					var wst = $(window).scrollTop();
					posY -= $tip[0].clientHeight + topOffset * 2;
					if ( opo.top - wst + posY < 0 )
					{
						posY -= opo.top - wst + posY;
					}
				}

				 $tip.css({left: posX, top: posY});				
			};
			
			var onMouseOut = function()
			{
				var className = $(this).attr('id');
				
				var $tip = $('#treetip_' + className);
				$tip.hide();
				
			};
			
			$elms.hover(
				onMouseOver, onMouseOut
			);
		
			$('.treetip').show();
			
			if ($.browser.msie)
			{
				$('.treetip').hide();
			}
			
			sIFR.replace(din_mittelschrift, 
				{
					selector: '.treetip h3', 
					wmode: 'transparent',
					thickness: 100,
					sharpness: 0,
					css: [
						'.sIFR-root { color: #f86000; font-size: 18px;  }'
					]
				}
			);
			
			$elms.click(
				function()
				{
					$elms.removeClass('selected');
					$(this).addClass('selected');
					
					var thumb = $('.tree_tumb');					
					thumb.removeAttr('class');					
					thumb.addClass($(this).attr('id'));
					thumb.addClass('tree_tumb');
					
					$elms.children('span').children('input').attr('checked',false);
					$(this).children('span').children('input').attr('checked',true);
				}
			);
		}
	}
};

$(document).ready(
	function()
	{
		TreeSite.onDocumentReady();
	}
);

