var $j = jQuery.noConflict();

$j(function(){
		$j('#subbr-link-2').colorbox(
				{
					inline: true, 
					href:"#contactform-div", 
					scrolling: false,
					opacity: 0.3,
					onClosed: function(){
						$j('form')[0].reset();
					},
					overlayClose: false
				});
		
		$j('#subbr-link-1').colorbox(
				{
					inline: true, 
					href:"#callform-div", 
					scrolling: false,
					opacity: 0.3,
					onClosed: function(){
						$j('form')[0].reset();
					},
					overlayClose: false
				});
		
		
		submitForm("#callform");
		submitForm("#contactform");
		
		
		
		hoverIcon('.cases-container .case-item');
		displayContent();
		
		
		$j('.close-button').click(function(){
			$j('form')[0].reset();
			$j.colorbox.close();
		});


var options = {
	keywords: ['from:capterNL'],
	language: 'nl_NL',
	interval: 0,
	clear: false,
	returnAmount: 1,
	layout: ['message', 'date', 'more_link']
}; 

		$j('#twitter-content').twitter(options);

		menuFix("#menu-top-menu"); 
		menuFix("#menu-children-subpage > ul");
});

function validateAndSubmit(form){
	$j(form).validate({
		submitHandler: function(form){
			submitForm(form)
		}
	});
}

function menuFix(menu){
	var thisMenu = $j(menu);
	
	if(thisMenu == null) return false;
	
	var loopItems = $j(thisMenu).children();
	var avgWidth = Math.floor(679 / loopItems.length);
	var menuWidth = $j(menu).innerWidth();
	
	var cssA = {
		'text-align' : 'center',
		'padding' : '0',
		'width' : avgWidth + 'px'
	};
	
	var sizeDif = 679 - menuWidth;
	var newItemWidthAdd = (sizeDif / loopItems.length);	
	
	$j.each(loopItems, function(){
		var itemWidth = $j(this).innerWidth(true);
		var newItemWidth = itemWidth + newItemWidthAdd;
		cssA.width = newItemWidth;
		$j(this).css("width", newItemWidth + "px").find("*").css(cssA);
		
	});

}

function submitForm(form){
		
	var validate =	$j(form).validate({
			rules:{
				'contactform-name': {required: true,minlength:5},
				'contactform-email': {required: true,minlength:6,email:true},
				'contactform-question': "required",
				'callform-name': {required: true,minlength:5},
				'callform-phone': {required: true,minlength:10}
			},
			messages: {
				'contactform-name': "Vul a.u.b. uw voornaam en achternaam in.",
				'contactform-email': "Vul a.u.b. uw e-mailadres in.",
				'contactform-question': "Stel uw vraag.",
				'callform-name': "Vul a.u.b. uw naam in.",
				'callform-phone': "Vul a.u.b. uw telefoonnummer in (10-cijferig, b.v. 012 345 67 89)."
			},
			invalidHandler: function(form, validator){
				var errors = validator.numberOfInvalids();
				if (errors) {
					$j.colorbox.resize();
				}
			}
		});

		$j('.close-button').click(function(){
			$j('form')[0].reset();
			$j.colorbox.close();
			validate.resetForm();
		});
		
		
	$j(form).submit(function(e){
		e.preventDefault();
		
	
		
		
		if($j(form).valid()){
				$j.ajax({
					type : 'post',
					url: '/wp-content/themes/custom/email.php',
					data: $j(form).serialize(),
					beforeSend: function(){
						
						

					},
					success: function(){
						alert("Uw bericht is succesvol verzonden. Er wordt zo spoedig mogelijk contact met u opgenomen");
						$j('form')[0].reset();
						$j.colorbox.close();
					},
					dataType: 'json'
				});
		}

});
}

function displayContent(){
	
	
	$j('#case-menu a').hover(function(){
		var id = $j(this).attr('id');
		
		switch(id){
			case 'problem':
				$j('#caseDisplay').html($j('#partial-1').html());
				break;
			case 'manage':
				$j('#caseDisplay').html($j('#partial-2').html());
				break;
			case 'result':
				$j('#caseDisplay').html($j('#partial-3').html());
				break;
		}

				
	});
}

function hoverIcon(container){
	
	var oldPosX;
	var oldPosY;
	var newPosX;
	var newPosY;
	var old;
	
	$j(container).hover(
			function(){
				var pos = $j(this).backgroundPosition();
				
				oldPosX = pos.split(' ')[0];
				oldPosY = pos.split(' ')[1];
				
				this.old = oldPosX + " " + oldPosY;
				
//				var posX = oldPosX.replace("px", "").replace("-", "");
				var posY = oldPosY.replace("px", "").replace("-", "");
//				newPosX = parseInt(posX) + 126
				newPosY = parseInt(posY) + 126
				
				var newPos = oldPosX + " -" + newPosY + "px";
				if(parseInt(posY) < 294)
					$j(this).css('backgroundPosition', newPos);
				$j('.entry-header h1').html($j(this).text());
			},
	
			function(){
				
				var oldPos = oldPosX + " " + oldPosY;
				if($j(this).hasClass('case-active') == false)
					$j('.entry-header h1').html("&nbsp;");
				
				if($j('.cases-container').find('case-active')){
					$j('.entry-header h1').html($j('.case-active').text());
				}
				
				$j(this).css('backgroundPosition', oldPos);
	
			});
}

(function($) {
  jQuery.fn.backgroundPosition = function() {
    var p = $(this).css('background-position');
    if(typeof(p) === 'undefined') return $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');
    else return p;
  };
})(jQuery);
