$(document).ready(function(){
	
	$('.triggerslide').click(function () {
		
		$('a.fancybox:first').trigger('click');
		
	});
	
	$("a.fancybox").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayOpacity':	0.5,
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});
	
	$('input[hover]').hover(function () {
		
		$(this).attr("src",$(this).attr('hover'));
		
	},function () {
		
		$(this).attr("src",$(this).attr('default'));
		
	});
	
	$('input[type=checkbox],input[type=radio]').prettyCheckboxes({
		
		checkboxWidth: 20, // The width of your custom checkbox
		checkboxHeight: 20
		
	});
	
	var showError = function (elm,text) {
		
		elm.css({'border': '1px solid #FF0000'});
		
		elm.before("<div class='error'>" +
				text +
				"</div>");
		
		
		elm.live('click',function () {
			
			$(this).css({'border': '1px solid #e1e1e1'});
			
			$('.error').fadeOut("slow");
		});
		
	}
		
	$('#codeform form').submit(function () {
				
		var formElm = $(this);
		
		var codeElm = formElm.find('#code');
				
		var code = codeElm.val();
		
		//alert(code);
				
		//if(code)
		//{		
			//alert(code);
			
			$.post("/checkcode",{uniqecode: code}, function (data) {
				
				if(data) {
				
					if(data.codecheck)
					{		
						if(data.codeused)
						{
							// Code is al gebruikt
							showError(codeElm,data.errorcode);
						}
						else if(data.codeexists)
						{
							// Code is goed en bestaat.
							location.reload(true);
							//formElm.submit();
							//return true;
						}
						else
						{
							showError(codeElm,data.errorcode);
						}
					}
					else
					{
						showError(codeElm,data.errorcode);
					}
				}
				else
				{
					
				}
				
			}, "json");
		//}
		//else
		//{
			//showError(codeElm,data.errorcode);
		//}		
		
		return false;
		
	});
	
});
