var err_timeout;

$(document).ready(function() {
	//-- Assign target to appropriate links
	$("a[rel='license']").click(function() {
		this.target = "_blank";
	});
	$("a[rel='download']").click(function() {
		this.target = "_blank";
	});
	/*$("a[rel='search']").click(function() {
		this.target = "_blank";
	});*/
	$("a[rel='external']").click(function() {
		this.target = "_blank";
	});
	//-- Assign javascript:void(0); to links with # as href
	$("a[href='#']").attr("href", "javascript:void(0);");
	//-- Show Errors, make them disappear after 5 seconds or on click.
	$('#errors').click(function() {
		closeErrors();
	});
	toggleErrors();
	err_timeout = setTimeout( "toggleErrors()", 15000 );
	//-- Misc functionalities
	$("a[href='#go-back']").attr("href", "javascript:history.go(-1);");

	//-- hide last block separator
	$('#left div.block-separator:last').hide();
	
	//-- Booking
	$('.btt_booking_com').live('click',function(event){
				 url =  ''+ $(this).attr('rel') +'&lang='+lng_bt_booking_lng+';#availability_target	'
		 title_table = '<div id="shadowBox_sp_titleBar">';
		 title_table += '<table width="100%"><tr>';
		 title_table += '<td><img src="images/logo.png" width="100px"></td>';
		 title_table += '<td width="30">&nbsp;</td>';
		 title_table += '<td align="right"><a href="'+url+'" class="booking_com_bt_new_window new_bt_blue" target="_blank">'+lng_bt_booking+'</a></td>';
		 title_table += '</tr></table>';
		 title_table += '</div><div class="clear"></div>';

		$.fancybox(
		{
			'type'				: 'iframe',
			'href'				:  url,
			'width'				: ($(document).height()*0.9),
			'height'			: ($(document).height()*0.8),
			'autoScale'			: true,
			'autoDimensions'	: true,
			'centerOnScroll'	: true,
			'onComplete'		:	function() {
            							$('div#fancybox-content').prepend(title_table);
										$('iframe#fancybox-frame').css('height','90%')
			},
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		}
	);
	return false;
		 url =  ''+ $(this).attr('rel') +'&lang='+lng_bt_booking_lng+';#availability_target	'
		 title_table = '<div id="shadowBox_sp_titleBar">';
		 title_table += '<table width="100%"><tr>';
		 title_table += '<td><img src="images/logo.png" width="100px"></td>';
		 title_table += '<td width="30">&nbsp;</td>';
		 title_table += '<td align="right"><a href="'+url+'" class="booking_com_bt_new_window new_bt_blue" target="_blank">'+lng_bt_booking+'</a></td>';
		 title_table += '</tr></table>';
		 title_table += '</div><div class="clear"></div>';
		 
		Shadowbox.open({
			content:   url,
			player:     "iframe",
			title:		title_table,
			height:     ($(document).height()*0.8),
			width:      ($(document).height()*0.9),
			options: { 
				onFinish: function() {
					$('#shadowBox_sp_titleBar').parent().parent().addClass('shadowBox_titleBar')
					$('#sb-wrapper-inner').css('border-top','none !important')
				} 
				}
			});
	 event.stopPropagation();
	})

});

function shadowBoxOverflow(set) {
	$('#sb-body').css('overflow','hidden');
	if (set) { $('#sb-body').css('overflow-y','auto'); }
}

function switchShadowbox(id) {
	Shadowbox.close();
	setTimeout('$("#'+id+'").click();',500);
}

function toggleErrors() {
	$('#errors').slideToggle('slow');
}

function closeErrors() {
	$('#errors').slideToggle('fast');
	clearTimeout(err_timeout);
}

function preloadImgs() {
	for(var i = 0; i<arguments.length; i++) {
		var tempImage = new Image();
		tempImage.src = arguments[i];
	}
}

function rand(min,max) {
	return Math.floor((Math.random() * (max-min+1))+min);
}

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

jQuery.fn.cfadeIn = function(speed, easing, callback) {
   return this.css({visibility: "visible"}).animate({opacity: 1}, speed, easing, callback);
};

jQuery.fn.cfadeOut = function(speed, easing, callback) {
   return this.css({visibility: "hidden"}).animate({opacity: 0}, speed, easing, callback);
};

jQuery.fn.sort = function() {
	return this.pushStack( [].sort.apply( this, arguments ), []);
};

