﻿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 tooltip
 */
function tooltip(action, item )
{
	if( action == 'show' )
	{
		var placePos = "center";
		var itemPos = $(item).offset();
		if( itemPos.top > 0 && itemPos.left > 0 )
		{
			var posX = itemPos.left+$(item).width();
			var posY = itemPos.top+$(item).height();
			placePos = [posX,posY];
		}

		$("#dialog-message p").html('test');
		$("#dialog-message").dialog({
			height: 'auto',
			modal: false,
			resizable: false,
			title: 'title',
			position: placePos,
			closeOnEscape: false,
			open: function( event, ui )
			{
				$('.ui-dialog-titlebar-close').hide();
			}
		});
	}
}

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

/**
 * Ui dialog message
 */
function uiConfirm(title, msg, callbackOk )
{
	$("#dialog-message").dialog("destroy");
	$("#dialog-message p").html(msg);
	$("#dialog-message").dialog({
		height: 'auto',
		width: 350,
		modal: true,
		resizable: false,
		title: title,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
				callbackOk();
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});
}

/**
 * Create two tone rows met javascript
 */
function trTwoTone(trClass){
	$(trClass + ':even').removeClass('hitlist_even').addClass('hitlist_odd');
	$(trClass + ':odd').removeClass('hitlist_odd').addClass('hitlist_even');
}

function initButtons(){
	//common
	$(".btn").button();

	//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-orange').button({
		icons: {
			primary: 'ui-icon-help'
		},
		text: false
	});

	//add button
	$(".btn-plus").button({
		icons: {
			primary: 'ui-icon-plus'
		}
	});

	//add button
	$(".btn-cart-orange").button({
		icons: {
			primary: 'ui-icon-cart'
		}
	});

	//cancel button
	$(".btn-close").button({
		icons: {
			primary: 'ui-icon-close'
		}
	});

	//edit button
	$(".btn-pencil").button({
		icons: {
			primary: 'ui-icon-pencil'
		}
	});

	//edit button
	$(".btn-document").button({
		icons: {
			primary: 'ui-icon-document'
		}
	});

	//prev button
	$(".btn-carat-1-w").button({
		icons: {
			primary: 'ui-icon-carat-1-w'
		}
	});

	//refresh button
	$(".btn-refresh").button({
		icons: {
			primary: 'ui-icon-refresh'
		}
	});

	//refresh button
	$(".btn-gear").button({
		icons: {
			primary: 'ui-icon-gear'
		}
	});

	//next button
	$(".btn-carat-1-e").button({
		icons: {
			//primary: 'ui-icon-hide',
			//secondary: 'ui-icon-carat-1-e'
			primary: 'ui-icon-carat-1-e'
		}
	});

	//trash button
	$(".btn-trash").button({
		icons: {
			primary: 'ui-icon-trash'
		}
	});

	//add button
	$(".btn-cart").button({
		icons: {
			primary: 'ui-icon-cart'
		}
	});

	//add print
	$(".btn-print").button({
		icons: {
			primary: 'ui-icon-print'
		}
	});
}

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

	//buttons
	initButtons();

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

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

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

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

	/*$('.icon-block').live('mouseover',
		function() { $(this).addClass('ui-state-hover'); }
	).live('mouseout',
		function() { $(this).removeClass('ui-state-hover'); }
	).live('mouseup',
		function(){ $(this).removeClass('ui-state-active'); }
	).live('mousedown',
		function(){ $(this).addClass('ui-state-active'); }
	);*/

	$('.helpText').click(function(){
		uiAlert($('.helpTextTitle', this).html(), $('.helpTextText', this).html(), 600);
	});

});
