jQuery.noConflict();
(function($) {
	$.tfeOverlay = function(){};
	$.tfeOverlay.attachOverlayData = function(colorbox) {
		$img = $('#cboxLoadedContent img.tx-srfreecap-pi2-image');
		$img.attr('src', $img.attr('src') + '&' + Math.random());
		
		$('#cboxLoadedContent a').each(function() {
			if($(this).attr('rel') != 'tfe-overlay-ignore') {
				$(this).bind('click', function(evt) {
					link = $(this);
					evt.preventDefault();
					
					//if no correct link exists do nothing
					if(link.attr('href') == '' || link.attr('href') == '#') return;
					
					var append = link.attr('href').match(/\?/) ? '&' : '?';
					$.get(link.attr('href') + append + 'type=20000', function(data) {
						$('#cboxLoadedContent').html(data);
						
						$.tfeOverlay.attachOverlayData(colorbox);
						colorbox.resize();
					});
				});
			}
		});
		// Traverse forms and trap submit buttons
		$('#cboxLoadedContent form').each(function() {
			var formElement = $(this);
			$('input:submit,input:image', formElement).bind('click', function(evt) {
				evt.preventDefault();
				
				//Simulate button click
				formElement.append('<input type="hidden" name="' + $(this).attr('name') + '" value="' + $(this).attr('value') + '" />');
				formElement.attr(
					'action',
					formElement.attr('action' ).replace(/&cHash=[0-9a-f]+/i, '').replace(/#c[0-9]+/i, '')
				);
				
				var append = $(formElement).attr('action').match(/\?/) ? '&' : '?';
				$.post(
					formElement.attr('action') + append + 'type=20000',
					formElement.serialize(),
					function(data) {
						$data = $(data);
						$img = $('img.tx-srfreecap-pi2-image', $data);
						if ($img.length > 0) {
							$img.attr('src', $img.attr('src') + '&' + Math.random());
							$('#cboxLoadedContent').html($data.html());
						} else {
							$('#cboxLoadedContent').html(data);
						}
						$.tfeOverlay.attachOverlayData(colorbox);
						colorbox.resize();
					}
				);
			});
		});
	}
	
	$(function() {
		$('a[rel="tfe-overlay"],a.tfe-overlay').each(function() {
			$(this).colorbox({href: function() {
				var append = $(this).attr('href').match(/\?/) ? '&' : '?';
				return $(this).attr('href') + append + 'type=20000';
			}, onComplete:function() {
				$.tfeOverlay.attachOverlayData($.fn.colorbox);
			}, rel:'nofollow', scrolling: false});
		});
	});
})(jQuery);
