//---------------------------------------------------------
// vars and settings
//---------------------------------------------------------

// use uri parser in strict mode
parseUri.options.strictMode = true;

// hostnames that are valid for this web
validHosts = new Array(
	'www.bertelsmann-bkk.de',
	'bertelsmann-bkk.de',
	'secure.bertelsmann-bkk.de'
);

// animation speed
aniSpeed = 400;

// time until redirect starts
timeToRedirect = 5000;

// redirect timeout/interval obj
redirectInterval = null;

// suspress the call or not
suppressCall = false;

//---------------------------------------------------------
// bind check function
//---------------------------------------------------------
$(document).ready(function(){
	
	// click link
	$('a').click(function(){
		if (!suppressCall){
			var uri = $(this).attr('href');
			return checkLinkHost(uri, $(this), false);
		}
		else {
			suppressCall = false;
			return true;
		}
	});	
	
	// submit form
	$('form').submit(function(event){
		if (!suppressCall){
			var uri = $(this).attr('action');	
			return checkLinkHost(uri, $(this), true);
		}
		else {
			suppressCall = false;
			return true;
		}
	});
});

//---------------------------------------------------------
// check links for host name
//---------------------------------------------------------
function checkLinkHost(uri, element, isForm){
 
	var host = parseUri(uri).host;
	
	if (host == '' || host == location.host || in_array(validHosts, host)) {
		//alert('this page');
		return true;
	}
	else {
		//alert('not this page: '+host);	
		showExternalMessage(uri, element, isForm);
		return false;
	}
}


//---------------------------------------------------------
// show the message box
//---------------------------------------------------------
function showExternalMessage(uri, element, isForm) {
	
	// add overlay to 
	var overlay = $('<div id="external-overlay"></div>');
	var overlayCssObj = {
		'background-color':	'#000000',
		'position': 		'fixed',
		'left': 			'0',
		'top': 				'0',
		'width': 			'100%',
		'height': 			'100%',
		'opacity':			'0.001'
	}
	overlay.css(overlayCssObj);
	$('body').append(overlay);
	
	
	// add box with content
	var message = $(
		'<div id="external-message">' +
		'<a href="javascript:closeExternalMessage();" >' +
		'<img src="/fileadmin/scripts/external-links/close.jpg" alt="Automatische Weiterleitung abbrechen" title="Automatische Weiterleitung abbrechen" style="float: right; border: 0;" width="20" height="20" />' +
		'</a>' +
		'<div style="clear:both; width: 100%; height 1px;"></div>' +
		'<h1 style="font-size: 14px; margin-bottom: 35px;">Externer Link</h1>' +
		'<p>' +
		'Sie verlassen jetzt das Angebot der Bertelsmann BKK, da die von Ihnen gew&uuml;nschten Informationen durch einen unserer Partner, bzw. andere externe Anbieter bereit gestellt werden.' +
		'<br /><br />' +
		'Sollte die automatische Weiterleitung nicht funktionieren, klicken Sie bitte <a style="font-weight: bold; text-decoration: underline;" href="'+uri+'" target="_blank">hier</a>.' +
		'</p>' +
		'</div>'
	);
	var messageCSSObj = {
		'background-color':	'#ffffff',
		'position': 		'fixed',
		'left': 			'50%',
		'top': 				'150px',
		'width': 			'400px',
		'height': 			'250px',
		'border':			'10px solid #FBE400',
		'margin-left':		'-210px',
		'padding':			'20px',
		'font-family':		'arial, helvetica, sans-serif',
		'font-size':		'11px',
		'line-height':		'150%',
		'color':			'#0F1B4A',
		'opacity':			'0.001'
	}
	message.css(messageCSSObj);
	$('body').append(message);
	
	// bind ESC keypress
	$('body').bind('keyup', function(event) {
		if (event.keyCode == 27){
			closeExternalMessage();
		}
	});
	
	// fade in box and overlay
	$('#external-overlay').animate({opacity: 0.6}, aniSpeed);
	$('#external-message').animate({
	    opacity: 1,
	  }, aniSpeed, function() {
		  
		  // automatic redirect
		  redirectInterval = window.setTimeout(function(){
			  
			  suppressCall = true;
			  closeExternalMessage();
			  element.removeAttr('target');
			  
			  if (!isForm){
				  location.href = uri;
				  //element.click();
			  }
			  else {
				  element.submit();
			  }
			  
		  }, timeToRedirect);
		  
	});
	
}

//---------------------------------------------------------
// close the messagebox
//---------------------------------------------------------
function closeExternalMessage(){
	
	$('#external-message, #external-overlay').animate({
	    opacity: 0.001,
	  }, aniSpeed, function() {
		  $('#external-overlay').remove();
		  $('#external-message').remove();
		  
		  $('body').unbind('keyup');
		  
		  window.clearInterval(redirectInterval)
	});
	
}

//---------------------------------------------------------
// check if element exists in array
//---------------------------------------------------------
function in_array(a,e){
    for (var i=0; i<a.length; ++i) {
        if (a[i] == e) {
            return true;
        }
    }
    return false;
}
