////////////////////////////////////////////////////////
/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/javascript-url-decode-encode.html
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
////////////////////////////////////////////////////////

jQuery(document).ready(function($){

	$('.print-button').click(function(){
		window.print();
	});
	$(document).pngFix();

	var config = {
		 sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
		 interval: 200, // number = milliseconds for onMouseOver polling interval
		 over: show_spce, // function = onMouseOver callback (REQUIRED)
		 timeout: 500, // number = milliseconds delay before onMouseOut
		 out: hide_spce // function = onMouseOut callback (REQUIRED)
	};

	$('.show-spce').hoverIntent( config );

	$('#editionprevious').hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);

	$('#editionnext').hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);

	$('#editionprevious').click(
		function () {
			$('#container').fadeOut(500);
		}
	);

/*
	$('#editionprevious').click(
		function() {
			$('#container').animate({width: ['toggle', 'swing'], height: ['toggle', 'swing'], opacity: 'toggle'}, 1000, 'linear', function() {$(this).after('<div>Animation complete.</div>');});
		}
	);
*/

	$('#editionnext').click(
		function () {
			$('#container').fadeOut(500);
		}
	);

	$('a').click(function() {
		this.blur();
	});

/*
	$('.listmore').click(
		function () {
			$(this).prev().animate({height: '600px'}, 500);
			$(this).next().css('display','block');
			$(this).fadeOut(500);
			$(this).siblings(".listclose").css('display','block');
		}
	);

	$('.listclose').click(
		function () {
			$(this).css('display','none');
			$(this).siblings(".itemBody").animate({height: '220px'}, 500);
			$(this).siblings(".listmore").css('display','block');
			$(this).siblings(".itemNav").css('display','none');
		}
	);
*/

	$('.listmore').click(
		function () {
			if($(this).siblings(".itemBodyDouble").hasClass('closed')) {
				$(this).siblings(".itemBodyDouble").slideDown();
				$(this).siblings(".itemBodyDouble").removeClass('closed');
				$(this).siblings(".listclose").show();
				$(this).siblings(".itemNav").show();

			} else {
				$(this).siblings(".itemBodyDouble").slideUp();
				$(this).siblings(".itemBodyDouble").addClass('closed');
				$(this).siblings(".listclose").hide();
				$(this).siblings(".itemNav").hide();
			}
//			$(this).siblings(".itemBody").slideUp();
//			$this.hasClass('closed') ? $(this).slideDown() : $(this).slideUp() ;

		}
	);

	$('.listclose').click(
		function () {
			$(this).hide();
			$(this).siblings(".itemBodyDouble").slideUp();
			$(this).siblings(".itemBodyDouble").addClass('closed');
			$(this).siblings(".itemNav").hide();
		}
	);



/* AUTOSUGGEST START

	var data = {items: [
		{value: "20", name: "aardappelen"},
		{value: "21", name: "recepten"},
		{value: "43", name: "hans van dijk"},
		{value: "46", name: "willem groeneveld"},
		{value: "55", name: "worst"},
		{value: "79", name: "pasta"}
	]};


	$(function() {
		$("div.search-wrapper input").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name"});
	});

/* AUTOSUGGEST END

/* FLEXBOX START */

	jQuery('#ffb1').flexbox('/typo3conf/ext/nkztrdg/pi1/inc/keywords_lookup_2010.php', {
			// Appearance
		watermark: 'ZOEK OP ZTRDG',
		showArrow: false,
		showResults: true,
		noResultsText : '',
		width :  176,
		maxVisibleRows :10,
			// Behavior
		autoCompleteFirstMatch: false,
		queryDelay : 200,
		highlightMatches : true,
		paging: {
			style: 'input',             // or 'links'
			cssClass: 'paging',         // prefix with containerClass (e.g. .ffb .paging)
			pageSize: 10,               // acts as a threshold.  if <= pageSize results, paging doesn't appear
			showSummary: true,          // whether to show 'displaying 1-10 of 200 results' text
			summaryClass: 'summary',    // class for 'displaying 1-10 of 200 results', prefix with containerClass
			summaryTemplate: 'Resultaten {start}-{end} van {total}', // can use {page} and {pages} as well
			maxPageLinks: 5             // used only if style is 'links'
		},
		onSelect: function() { $('#searchbox .searchsubmit').click(); }
	});

	jQuery('#ffb1_input').attr('onkeypress','return entsub(event,this.form);');


/* FLEXBOX END */


	$(function() {
		$(".cell11").draggable({ handle:'.cell11-title'});
		$(".cell21").draggable({ handle:'.cell11-title'});
	});
    
    
    /* Fancybox
    
    $("a.iframe").fancybox({
		'hideOnContentClick': true,
        'width': 580,
        'height': 450,
        'scrolling': 'no' 
	});
    
    /* Fancybox Eof */


});



function show_spce() {
	var spce = jQuery(this);
	var target = '.spce-' + spce.attr('rel');
	jQuery(target).slideDown('slow');
}

function hide_spce() {
	var spce = jQuery(this);
	var target = '.spce-' + spce.attr('rel');
	jQuery(target).delay(1000).slideUp('slow');
}


function entsub(event,searchbox) {
		if(event && event.which == 13) {
			qValue = searchbox['ffb1_input'].value;
//			alert(qValue);
			searchbox['ffb1_hidden'].value = qValue;
			document.searchbox.submit;
			return true;
		} else {
			return true;
		}

/*

	if(event && event.which == 13) {
		alert('piep');
		searchbox.submit();
	} else {
		alert('peep');
		return true;
*/
}

