jQuery(function($){
	// Global var needed for preview pictures
	previewPictures = new ImagePreview();

	addCaptchaReloadHandling();

	// Handle fields with a default value
	$('.defaultValue')
		.focus(function(){
			var $this = $(this);

			if (this.defaultValue == $this.val()) {
				$this.val('');
				$this.removeClass('defaultValue');
			}
		})
		.blur(function(){
			var $this = $(this);
			if ($this.val() == '') {
				$this.val(this.defaultValue);
				$this.addClass('defaultValue');
			}
		});

	// Bind click event for search tabs
	var $searchTabVideo = $('#searchTabVideo').click(function(e){
		e.preventDefault();
		setSearchType('videos');
		$searchTabVideo.attr('class', 'active');
	});
	var $searchTabGame = $('#searchTabGame').click(function(e){
		e.preventDefault();
		setSearchType('games');
		$searchTabGame.attr('class', 'active');
	});
	var $searchTabBlog = $('#searchTabBlog').click(function(e){
		e.preventDefault();
		setSearchType('article');
		$searchTabBlog.attr('class', 'active');
	});

	// Initialize autocompletion for header search field
	$('#searchFieldHead').autocomplete({
		source: "/ajax/search/ajaxAutocompleteSearch.php?searchType=" + $('#searchTypeValue').val(),
		minLength: 3
	});
});

/**
 * Binds some events to the captcha reload elements
 *
 * @author      Philipp Schärer <ps@adfinis.com>
 * @author      Damian Senn <ds@adfinis.com>
 * @return      void
 */
function addCaptchaReloadHandling() {
	function reloadCaptcha() {
		// add random get parameter to prevent caching
		$('.captchaImage').attr('src', '/captcha/picture.php?t=' + Math.random());
	}

	$('.captchaImage, .reloadCaptchaImage').live('click', function(e){
		e.preventDefault();

		reloadCaptcha();
	});

	$('form.hasCaptcha').one('focusin', reloadCaptcha);
}

/**
 * Set new search type and remove "old" classes
 *
 * @author      Adrian Wittwer <aw@adfinis.com>
 * @param       string
 * @return      void
 */
function setSearchType(value) {
	// Set new search type value
	$('#searchTypeValue').val(value);

	// Remove all classes
	$('#searchTabVideo').removeAttr('class');
	$('#searchTabGame').removeAttr('class');
	$('#searchTabBlog').removeAttr('class');

	// Set autocompletion for new searchType
	$('#searchFieldHead').autocomplete({
		source: "/ajax/search/ajaxAutocompleteSearch.php?searchType=" + value,
		minLength: 3
	});
}

// Email encoding - shoul be refactored
var g_base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

function base64_decode(encStr){
	var bits;
	var decOut = '';
	var i = 0;

	for(; i < encStr.length; i += 4){
		bits = (g_base64_table.indexOf(encStr.charAt(i))     & 0xFF) << 18 |
			   (g_base64_table.indexOf(encStr.charAt(i + 1)) & 0xFF) << 12 |
			   (g_base64_table.indexOf(encStr.charAt(i + 2)) & 0xFF) <<  6 |
			   (g_base64_table.indexOf(encStr.charAt(i + 3)) & 0xFF);

		decOut += String.fromCharCode((bits & 0xFF0000) >> 16, (bits & 0xFF00) >> 8, bits & 0xFF);
	}

	if(encStr.charCodeAt(i - 2) == 61)
		return(decOut.substring(0, decOut.length - 2));
	else if(encStr.charCodeAt(i - 1) == 61)
		return(decOut.substring(0, decOut.length - 1));
	else
		return(decOut);
}

function print_mail_link(addr, title, tagparm){
	addr = base64_decode(addr);

	var decoded = "";
	var len = addr.length;
	var i   = 0;

	for (i = 0; i < len; i++) {
		decoded += String.fromCharCode(addr.charCodeAt(i) ^ 156);
	}

	if (title == null || title == '') {
		title = decoded;
	}

	decoded = decoded.replace(/</g, '&lt;');
	decoded = decoded.replace(/>/g, '&gt;');

	title = title.replace(/</g, '&lt;');
	title = title.replace(/>/g, '&gt;');

	document.write('<a ' + tagparm + ' hr' + 'ef="mai' + 'lto' + ':' + decoded + '">' + title + '</a>');
}

