var menuName = "#miniMenu";
var contentName = "#content";
var menuYloc = null;

$(document).ready(function(){

	menuYloc = parseInt($('.L2C5A').position()['top']);
	
	$(menuName).css('top', menuYloc + 'px');
		
	resize = function(){
		docHeight = $(window).height();
		currentY = parseInt($(menuName).css("top").substring(0,$(menuName).css("top").indexOf("px")))
		
		height = docHeight - currentY;
		
		contentOffset = ($(contentName).height() + menuYloc) - $(document).scrollTop();
		
		if (height > contentOffset)
			height = contentOffset;
		$(menuName).css('height', height + 'px');
	}
	
	onAnimateComplete = function(){
		scr = $(document).scrollTop(); 
		currentY = parseInt($(this).css("top").substring(0,$(this).css("top").indexOf("px")))
		if (scr < menuYloc){
			$(this).stop(true)
			$(this).css('top', menuYloc - scr);
		}
		resize();
	}
	
	relocate = function () {
		if ($(document).scrollLeft())
			$('#miniMenu').css('left', -$(document).scrollLeft());
		else 
			$('#miniMenu').css('left', '');
		
		scr = $(document).scrollTop();
		var offset = menuYloc - scr;
		if (offset < 0) offset = 0;
		
		var current = parseInt($(menuName).css("top").substring(0,$(menuName).css("top").indexOf("px")))
		var duration = 0;
		if (offset < current) {
			duration = 500;
		}
		else  {
			duration = 1;
		}
		offset = offset + "px";
		if ($.browser.safari)
			$(menuName).css({top: offset});
		else
			$(menuName).animate(
				{top: offset},
				{
					duration: duration,
					queue: false,
					complete: onAnimateComplete
				}
			);
	};
	
	scrollToPiece = function(){
		piece = null;
		if ($(this).attr('scrollto')){
			piece = $(this).attr('scrollto');
		}
		else if ($(this).attr('href')){
			piece = $(this).attr('href');
		}
		
		if (piece){
			var target = $(piece);
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: $(piece).offset().top}, 500 );
			return false;
		}
		return true;
	}
	
	$('#miniMenu a[href*="#"], #miniMenu a[scrollto]').click(scrollToPiece);
	
	relocate();
	
	$(window).resize(resize);	
	$(window).scroll(relocate);
	
	$(menuName).show();
	
	
	//---- 
	$('.language-select').change(function(){
		location = $(this).val();
	})
	
	//----
	selUrl = function(){this.select();}
	$('#url-input').live('mouseup', selUrl).live('keyup', selUrl);
	$('#url-input').focus(selUrl);
	
	//----
	$('.thumbGallery')
	.live('click', function() {
		if(typeof gallery != "undefined")
			gallery.close();
        gallery = window.open(
        	$(this).attr('href'), 
        	'gallery', 
        	'status=1,toolbar=1,location=1,menubar=0,directories=0,resizable=1,scrollbars=1,height='+window.screen.availHeight+',width='+window.screen.availWidth
        );
        gallery.focus();
        return false;
    })
    .tooltip({
		bodyHandler: function() { 
			return '<img class="ico16"  src="' + image('/img/icon/newWin12.gif') + '"> ' + $(this).attr('tooltip');
	    }, 
	    showURL: false,
	    delay: 0,
	    track: true
	});
	
	//Load Street View
	loadSV = function(){streetView($("#llatitude").val(), $("#llongitude").val(), $('#street-view')[0], noDataMsg, function(available){
		$('#streetview').attr('link', '/lodgings/streetview/' + (available + 0));
		if (available) {
			$('#streetview, #street-view').css('height', '300px');
			$('#cant-move-me').show();
			$('#noStreetViewMsg').hide();
		}
		else {
			$('#streetview').css('height', '40px');
			$('#street-view').css('height', '30px');
			$('#street-view').removeClass('loaderCon');
			$('#cant-move-me').hide();
		}
	});};
	loadSV();
});