﻿jQuery.fn.infoPopup = function(oParams) {
	var text 	= $(this).attr("title");

	if(oParams.pos == "L"){
		var div		= $("div.popup_infoL[@rel="+oParams.type+"]");
	} else {
		var div		= $("div.popup_info[@rel="+oParams.type+"]");
	}

	var cDiv	= div.find("div");
	var item	= $(this);
	var typeID	= oParams.typeID;

	item.removeAttr("title");

	item.hover(function() {
		cDiv.html(text);
		div.appendTo(item.parent());
		div.show();
	}, function() {
		div.hide();
		div.insertBefore($("#"+typeID));
	});
};

jQuery.fn.extend({

	initInfoPopups: function(pos) {
		if(!pos){
			pos="";
		}

		var thumbs 	= $(this).find("label.error img");
		var type	= $(this).attr("id")+"Display";
		var typeID	= $(this).attr("id");
		thumbs.each( function(i) {
			if($(this).attr("title")){
				$(this).infoPopup({"type":type,"typeID":typeID,"pos":pos});
			}
		});
	}

});

/**
 * Ui dialog message
 */
function uiAlert(title, msg, width)
{
	if($.ui)
	{
		$("#dialog-message p").html(msg);
		$("#dialog-message").dialog({
			height: 'auto',
			modal: true,
			resizable: false,
			width: ( typeof width != 'undefined' ? width : '300' ),
			title: title,
			buttons: [
				{
					text: YM_trans('Ok'),
					click: function() {
						$(this).dialog('close');
					}
				}
			]
		});
	} else {
		alert( msg );
	}
}

/**
 * Ui dialog message
 */
function uiConfirm(title, msg, callbackOk, width )
{
	if($.ui)
	{
		$("#dialog-message p").html(msg);
		$("#dialog-message").dialog({
			height: 'auto',
			width: ( typeof width != 'undefined' ? width : '300' ),
			modal: true,
			resizable: false,
			title: title,
			buttons: [
				{
					text: YM_trans('Annuleren'),
					click: function() {
						$(this).dialog('close');
					}
				},
				{
					text: YM_trans('Ok'),
					click: function() {
						$(this).dialog('close');
						return callbackOk();
					}
				}
			]
		});
	} else {
		if( confirm(msg) ){
			return callbackOk();
		}
	}
}

/**
 * YM bot Text ported van PHP naar Javascript
 */
function YM_botText( str )
{
	// Replace invalid characters
	var replace = new Array('.','%','#','*','\"','?','(',')','[',']','ç','æ','œ','á','é','í','ó','ú','à','è','ì','ò','ù','ä','ë','ï','ö','ü','ÿ','â','ê','î','ô','û','å','ø','&');
	var by = new Array('','','','','','','','','','','c','ae','oe','a','e','i','o','u','a','e','i','o','u','a','e','i','o','u','y','a','e','i','o','u','a','o',' en ');
	for (var i=0; i<replace.length; i++) {
		str = str.replace(replace[i], by[i]);
	}

	// Trim string
	str = $.trim(str);

	// Convert all other non-alphanumeric characters to spaces
	str= str.replace(/[^\w\d]+/g, '-');

	//Remove a - at the beginning or end and make lowercase
	str = str.replace(/-$/, '');
	str = str.replace(/^-/, '');
	str = str.toLowerCase();

	return str;
}

function initButtons(){
	//add icon
	$('.icon-block-plus').button({
		icons: {
			primary: 'ui-icon-plus'
		},
		text: false
	});

	//remove/trash icon
	$('.icon-block-trash').button({
		icons: {
			primary: 'ui-icon-trash'
		},
		text: false
	});

	//edit icon
	$('.icon-block-pencil').button({
		icons: {
			primary: 'ui-icon-pencil'
		},
		text: false
	});

	//refresh
	$('.icon-block-refresh').button({
		icons: {
			primary: 'ui-icon-refresh'
		},
		text: false
	});

	//remove/trash icon
	$('.icon-help-green').button({
		icons: {
			primary: 'ui-icon-help'
		},
		text: false
	});
}

$(document).ready(function() {
	$('img[src$=".png"]').ifixpng();

	//buttons
	initButtons();

	if (typeof jQuery.fn.jdMenu == 'function') {
		$('ul.sitemenu').jdMenu({
			disableLinks: false
		});

		$('ul.sitemenu a').hover(
			function () {
				$(this).data('title', $(this).attr("title") );
				$(this).removeAttr("title");
			},
			function () {
				$(this).attr("title", $(this).data('title') );
				$(this).removeData("title");
			}
		);
	}

	//submit form
	$('.submit').click(function(){
		$(this).parents('form').submit();
	});

	//toggleval
	$(".toggleInput").toggleVal({
		populateFrom:	"labelAndEmpty",
		removeLabels:	true,
		focusClass:		"hasFocus",
		changedClass:	"isChanged"
	});

	//quick search
	$("form#searchContent, form#loginForm, form#lost_pw").submit(function(){
		$(this).find(".toggleInput").each(function() {
			if($(this).val() == $(this).data("defText")) {
				$(this).val("");
			}
		});
	});

	//datepicker
	$.datepicker.setDefaults({
		showOn: 'both',
		buttonImage: '/site/'+ys_site+'/design/graphics/calendar.gif',
		buttonImageOnly: true,
		buttonText: YM_trans('Klik om de kalender te openen'),
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd/mm/yy',
		showAnim: 'show'
	});

	$('#switchHeaderLogin').click( function(){
		$('#switchBoxSearch').hide();
		$('#switchBoxLogin').show();
	});
	$('#switchHeaderSearch').click( function(){
		$('#switchBoxSearch').show();
		$('#switchBoxLogin').hide();
	});

	$('.adserver-banner:not(:has(a))').hide();
});
