// JavaScript Document

$(function()// cataloog, opbouwen subcategorie als categorie veranderd
{
	$(".meer").button();
	
	$("#Tstock").Tstock();
	
	$("#categorie").change(function()
	{
		var cat = $('#categorie option:selected').val();
		$("#trefwoord").val("");
		
		$.post($url + "/cataloog/get-subcats",
		{ 
			categorie: cat
		},
		function(data)
		{
			$("#subcategorie option:gt(0)").remove();
			$.each(data, function(key, value) 
			{
   			 	$("<option/>").attr({'value': key }).html(value).appendTo($('#subcategorie'));
			});
		},'json');
	})
	
	$(".help").mouseover(function()
	{
		drukID = $(this).parent().attr('title');
		help = $(this);
		
		$.post($url + "/druk/help",
		{ 
			druk: drukID
		},
		function(data)
		{
			help.qtip(
			{
				 content: data,
				 show: { delay: 0 },
				 position: { target: 'mouse', adjust: { mouse: true, y:-70, x: -370 } },
				 style: { width: { max: 350 } }
			});
		});
	});
});

$(function()// bij textiel als merk gekozen is categorie op 0 en omgekeerd
{
	if(page == 'textiel')
	{
		$("#categorie").change(function()
		{
			if($("#categorie option:selected").val() != 'all') { $("#merk").val('all'); }
			$('#trefwoord').val('');
		});
		
		$("#merk").change(function()
		{
			if($("#merk option:selected").val() != 'all') { $("#categorie").val('all'); }
			$('#trefwoord').val('');
		});
		
		$('#trefwoord').keyup(function()
		{
			$("#merk").val('all');
			$("#categorie").val('all');
		});
	
		$('.prijsInfo').click(function()
		{
			$('#dialog-prijsInfo').dialog('open');
		});
		
		$("#dialog-prijsInfo").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 500,
			width: 340,
			modal: true,
			buttons: {
				'Zend': function() 
				{
					var naam = $("#dialog-prijsInfo #naam"),
						straat = $("#dialog-prijsInfo #straat"),
						postcode = $("#dialog-prijsInfo #postcode"),
						gemeente = $("#dialog-prijsInfo #gemeente"),
						telefoon = $("#dialog-prijsInfo #telefoon"),
						email = $("#dialog-prijsInfo #email"),
						artikel = $("#dialog-prijsInfo #artikel"),
						
						allFields = $([]).add(naam)
										 .add(straat)
										 .add(postcode)
										 .add(gemeente)
										 .add(email)
										 .add(telefoon),
						
						tips = $("#dialog-prijsInfo #validateTips"),
						
						valid = true;
						
					function updateTips(t)//opmerking geven  
					{
						tips.children('li').remove();
						$("<li>").html(t).appendTo(tips);
					}	
					
					$('#dialog-prijsInfo input').removeClass('ui-state-error');
					
					allFields.each(function()
					{ 
						if($(this).val() == "")
						{
							$(this).addClass('ui-state-error');
							valid = false; 
							updateTips('Gelieve de aangeduidde velden in te vullen:');
						
						} else if($(this).attr('name') == 'straat' && $(this).val().length < 5) {
							
							$(this).addClass('ui-state-error');
							valid = false; 
							updateTips('Minimum 5 karakters');
							
						}
						
					}); 
					
					if(valid)
					{
						$.post("/textiel/info-zend", 
						{ 
							naam: naam.val(), straat : straat.val(), postcode : postcode.val(), gemeente: gemeente.val(), telefoon: telefoon.val(), email: email.val(), artikel: artikel.val(), aantal: $("#dialog-prijsInfo #aantal").val(), 
							bedrukking:  $("#dialog-prijsInfo input[name='bedrukking']:checked").val(), kleuren: $("#dialog-prijsInfo #kleuren").val(), 
							bericht: $("#dialog-prijsInfo #bericht").val()
						},
						function(data)
						{
							$("#dialog-prijsInfo").html('<p><b>Wij hebben uw aanvraag goed ontvangen.</b></p><p>Wij zullen u zo spoedig mogelijk contacteren.</p>');
							var options = { buttons: {'Sluit': function() {  $("#dialog-prijsInfo").dialog('close');  } } };
							$("#dialog-prijsInfo").dialog('option', options);
						
						}, 'json');
					}
				},
				
				'Annuleer': function() 
				{
					$(this).dialog('close');
				}
			},
			close: function() {
				
			}
		});
	}
});


$(function()// img handling qtip aan elke afbeelding toevoegen als ook checken if image is loaded
{
	$('.item img:not(#refImage)').fixBroken().each(function(index, item)
	{	
		$(item).load(function()
		{
			var src = item.src.replace(/thu/,'img');
			var art = $(item).attr('title');
			var merk = $(item).attr('rel');

			if(!$(item).hasClass('off'))
			{
				var div = $("<div/>");
				$("<img/>").attr({'src': src}).appendTo(div);
				
				$.each($(item).parent().parent().parent().parent().find('.artikel .kleur'), function(id, val)
				{
					$("<img/>").attr({'src': '/assets/prod/'+ merk +'/thu/'+ art +'_'+ $(val).html() +'.jpg' }).appendTo(div);
				});
				
				var x = ((index +1)%3 == 0) ? -400 : 20;
				var y = -70 - ((index +1)%3) * 50;
				
				$(item).qtip(
				{
					 content: div,
					 position: { target: 'mouse', adjust: { mouse: true, y:y, x: x } },	
					 style: { width: { max: 350 } }
				});
			}
		});
	});
	
	$('.kleur').each(function(index, item)
	{	
		var art = $(item).parent().attr('title');
		var kleur = $(item).html();
		
		var src = '/assets/prod/MOB/img/'+ art +'_'+ kleur +'.jpg';
		var content = $("<img/>").attr({'src': src});
			
		$(item).qtip(
		{
			 content: content,
			 position: { target: 'mouse', adjust: { mouse: true, y:-70, x: 20 } },
			 style: { width: { max: 350 } }
		});
	});
});


$.fn.Tstock = function()
{
	this.click(function()
	{
		var url = $(this).attr('href'),
			title =  $(this).attr('title'),
			div = $("<div/>").attr({ 'title': title }).html('<iframe src ="'+ url +'" width="100%" height="450" frameborder = "0"></iframe>').appendTo('body').dialog({ height:500, width: 470, modal: true });
		
		return false; 
	});
};


$.fn.fixBroken = function()
{
	return this.each(function()
	{
		var tag = $(this),
		 	alt_img = '/assets/prod/off.jpg';
		
		tag.error(function() 
		{ 
			tag.attr("src", alt_img).addClass('off'); // change the src attribute of the image
			return true;
		});
	});
};

$.fn.allData = function() {
    var intID = jQuery.data(this.get(0));
    return(jQuery.cache[intID]);
};
