// 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;
			}
		});
	});
	
	// 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();
	});
}
