// common.js

if(typeof jQuery != 'undefined') {
	
	// typo3 backend shortcut
	$(document).ready(function() {
		$(document).keyup(function(e) {
			if(e.which == 113)
				window.open('http://' + document.URL.split('/')[2] + '/typo3/');
		});
	});
	
	// default window opener (external)
	$(document).ready(function() {
		$('a[rel~=external], a.external').click(function() {
			relation = $(this).attr('rel');
			
			if($(this).attr('href')) {
				options = '';
				
				address = $(this).attr('href');
				
				if(relation.match(/\[([0-9]{2,4})\,([0-9]{2,4})\]/)) {
					options += '';
					
					size = relation.split('[');
					size = size[1].replace(/\]/, '');
					size = size.split(',');
					
					options += 'width=' + size[0] + ', height=' + size[1] + ',left=200,top=200';
				}
				
				window.open($(this).attr('href'), '', options);
				
				return false;
			}
		});
	});
	
	// eyedea batch transition
	$(document).ready(function() {
		
		if($('#badge a.animated').size() == 0) {
			return;
		}
		
		// get presets
		var height = $('#badge a').height();
		var position = $('#badge a').css('background-position');
		
		var coordinates = position.split(' ');
		
		var x = parseInt(coordinates[0]);
		var y = parseInt(coordinates[1]) + height;
		
		$('#badge a')
			.css('background-position', position)
			.hover(function() {
				$(this).stop().animate({'background-position': x + 'px ' + y + 'px'}, 150);
			}, function() {
				$(this).stop().animate({'background-position': position}, 150);
			});
	});
	
	// window properties
	/*
	Size
		 < 1024 .......... size_tiny
	1024 - 1280 .......... size_small
	1280 - 1440 .......... size_medium
	1440 - 1600 .......... size_large
		 > 1600 .......... size_huge
	
	Ratio
	16:9 | 16:10 .......... ratio_wide
			 4:3 .......... ratio_normal
	
	Orientation
	   orientation_landscape
	   orientation_portrait
	*/
	$(document).ready(function() {
		var size;
		var ratio;
		var orientation;
		
		var properties = new Array;
		
		// resize function
		resize = function() {
			w = $(window).width();
			h = $(window).height();
			r = w/h;
			o = r;
			
			// get named size
			if(w <= 1024) {
				size = 'size_tiny';
			} else if(w <= 1280) {
				size = 'size_small';
			} else if(w <= 1440) {
				size = 'size_medium';
			} else if(w <= 1600) {
				size = 'size_large';	
			} else {
				size = 'size_huge';
			}
			
			// get named ratio
			if(r < 1.6) {
				ratio = 'ratio_normal';
			} else {
				ratio = 'ratio_wide';
			}
			
			// get named orientation
			if(o < 1) {
				orientation = 'orientation_portrait';
			} else {
				orientation = 'orientation_landscape';
			}
			
			// unset old window properties
			if(properties.length > 0) {
				$.each(properties, function(i, property) {
					$('html').removeClass(property);
				});
			}
			
			// clear old window properties
			properties = new Array;
			
			// store new window properties
			properties.push(size);
			properties.push(ratio);
			properties.push(orientation);
			
			// assign window properties
			$.each(properties, function(i, property) {
				$('html').addClass(property);
			});
		}
		
		// resize listener
		$(window).resize(function() {
			resize();
		});
		
		// call resize function once on load
		resize();
	});
}

// typo3 default functions
function decryptCharcode(n, start, end, offset) {
	n = n + offset;
	if (offset > 0 && n > end) {
		n = start + (n - end - 1);
	} else if (offset < 0 && n < start) {
		n = end - (start - n - 1);
	}
	return String.fromCharCode(n);
}

function decryptString(enc, offset) {
	var dec = "";
	var len = enc.length;
	for (var i = 0; i < len; i++) {
		var n = enc.charCodeAt(i);
		if (n >= 0x2B && n <= 0x3A) {
			dec += decryptCharcode(n, 0x2B, 0x3A, offset);
		} else if (n >= 0x40 && n <= 0x5A) {
			dec += decryptCharcode(n, 0x40, 0x5A, offset);
		} else if (n >= 0x61 && n <= 0x7A) {
			dec += decryptCharcode(n, 0x61, 0x7A, offset);
		} else {
			dec += enc.charAt(i);
		}
	}
	return dec;
}

function linkTo_UnCryptMailto(s) {
	location.href = decryptString(s, -10);
}
