var path = '/';
function loadPageIntoShell(url, container, show_loader, callback)
{
	$('.loading').css({visibility:'visible'});
	
	$.get(url + "?cache=" + Math.random() ,function(data) {
		$(container).html((data));
	});
	
	return false;
}

function updateNewsPreviewImage(src)
{
	var display  = $('#latest_preview');
	if (src == display.attr('src'))
		return false;
	display.stop(true, true);
	display.fadeOut('fast', function () {
		display.attr('src', src);
	});	
}

function loadPageIntoLightbox(url)
{	
	$('#lb_content').empty();
	$('#lb_content').removeAttr('style');
	
	$('#lightbox').fadeIn(300);
	
	$.get(url,function(data) {
		$('#lb_content').css({'top' : getScrollXY()[1] + 20 + 'px'});
		$('#lb_content').html(data);
		$('#lb_content').fadeIn(300);
	});
}



function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function anchorTag(section) {
	section = $('#' + section);
	if (section.offset() != null) {
		$('html,body').animate({scrollTop: section.offset().top - $('#header').height()}, 400);	
	}
	return false;		
}

$(document).ready(function() {
	

	jQuery.fn.selectDropdown = function() {
		
		var menu_matcher = $('.menumatch[menu="'+$(this).attr('menu')+'"]');
		
		var o = menu_matcher.offset().left;
		var p = menu_matcher.parent().offset().left;
			
	//	$('.dropdown_arrow').attr('src', $('.dropdown_arrow').attr('hover'));
		$(this).stop(true, false).css({left:Number(o-p)+"px", opacity:1}).animate({"top":145 + "px"}, 350, function() {
			$(this).attr('selected', 1);
		});
	}
	jQuery.fn.unselectDropdown = function(delay) {
		delay = delay!=null ? delay : 1000;
		
		$(this).stop(true, false).animate({opacity:1}, delay).animate({"top":"-" + $(this).outerHeight() + "px"}, 350, function() {
			$(this).attr('selected', 0);
			$('.dropdown_arrow').attr('src', $('.dropdown_arrow').attr('default'));
		});
	}
						
	$('.imgSwap').live('mouseenter', function() {
		if ($(this).attr('default') == null) {
			$(this).attr('default', $(this).attr('src'));
		}
		$(this).attr("src", $(this).attr('hover'));
		return false;
	});
	$('.imgSwap').live('mouseleave', function() {
		if ($(this).attr('selected') == 1) {
			return false;
		}
		$(this).attr("src", $(this).attr('default'));
		return false;
	});

	$('a.previewlink').bind({
		mouseover : function() {
			updateNewsPreviewImage($(this).attr('default_img'));	
		}
	});
	
	$('.close_mobile').live('click', function() {
		$('#lightbox').fadeOut(300);
		$('#lb_content').fadeOut(300);
		return false;
	});
	


	
	$('.dropdown_menu').bind({
		mouseenter: function() {
			$(this).selectDropdown();		
		},
		mouseleave: function() {
			$(this).unselectDropdown();
		}
	});
	
	$('.menumatch').bind({
		mouseenter: function() {
			$("#header").children('.dropdown_menu.' + $(this).attr('menu')).selectDropdown();		
		},
		mouseleave: function() {
			$("#header").children('.dropdown_menu.' + $(this).attr('menu')).unselectDropdown();
		}
	});
	
	var mapratio = .5;
	
	$('.map .place').bind({
		load: function() {
			$(this).attr('swidth', 160);
			$(this).attr('sheight', 90);
			$(this).attr('stop', parseInt($(this).css('top')));
			$(this).attr('sleft', parseInt($(this).css('left')));
			$(this).mouseleave();
			$(this).hide(0);
			$(this).show(400);
		},
		mouseenter: function() {
			
			var css = {
				'width':$(this).attr('swidth') + 'px',
				'height':$(this).attr('sheight') + 'px',
				'top': $(this).attr('stop') - $(this).attr('sheight')/2 * mapratio + 'px',
				'left': $(this).attr('sleft') - $(this).attr('swidth')/2 * mapratio   + 'px'
			};
			$(this).stop(true, false).css({'z-index':1000}).animate(css, 100, function() {
				$('.map').children('.caption').css({
					left: parseInt($(this).css('left')) - 10 + "px"	,
					top: parseInt($(this).css('top')) - 10 + "px",
					zIndex : '999'
				});	
				
				$('.map').children('.caption').animate({opacity:1}, 200).children('.text').html($(this).attr('caption'));
			});
		},
		mouseleave: function() {
			var css = {
				'width':$(this).attr('swidth') * mapratio + 'px',
				'height':$(this).attr('sheight') * mapratio + 'px',
				'top':$(this).attr('stop') + 'px',
				'left':$(this).attr('sleft') + 'px'
			};
			
			$('.map').stop(true, false).children('.caption').css({"z-index":"99"}).animate({opacity:0}, 100).children('.text').empty();
			
			if ($(this).attr('init')) {
				$(this).stop(true, false).css({"z-index":"100"}).animate(css, 100);		
			} else {
				$(this).stop(true, false).css(css);	
			}	
			$(this).attr('init', true);
		}
		
		
	});
	
	$.address.change(function(event) {
		var url = event.value;
		if (url == '/') {
			return false;	
		}
		//anchorTag(url.substr(0, url.length));
		
	});
	
	$('.map .place').each(function() {
		$(this).attr('src', $(this).attr('imgsrc'));
	});
	
	$('.dropdown_menu').each(function(){
		$(this).css({opacity:0});
	});
	if (typeof($.fn.videoplayer) !== 'undefined')
	{
		$.videoselector.init($('.videoplayer_shell .selector'));
		$.videoselector.config(
			{
				api:$.paths.base + "/videos_dal/selector/",
				conditions: {game:1,'group_type[]=1&group_type[]=2&foo':''},
				onLoad:function()
				{
					if ($('body.device.ipad').length !== 0 || $('body.device.iphone').length !== 0)
						return;
					$('.videoplayer_shell .selector').find('li.video:eq(0)').click();	
				},
				onClick:function(video_id)
				{
					if ($('body.device.ipad').length !== 0 || $('body.device.iphone').length !== 0)
					{
						//$.videoplayer.launchYoutube(video_id);
						return;
					}
					$('#videoplayer').videoplayer({
						videoid:video_id,
						agegate:false,
						etcontrols:true
					});
					$.videoselector.showVideo(video_id);
				}
			}
		);
	}
}); 
