jQuery(document).ready(function()
{


	/*****************************************************
	 *
	 *                    Contact Form                    
	 *
	 *****************************************************/
	
	
	// Setting contact tabs.
	$('#contact-tabs').tabs(
	{
		select: function(event, ui)
		{
			var url = $.data(ui.tab, 'load.tabs');
			
			if(url)
			{
				location.href = url;
				return false;
			}
			
			return true;
		}
	});
	 
	$('#contact-tabs').bind('tabsshow', function(event, ui)
	{
		// Make use of the added hack to the core which fixed the shadow not being resized properly.
		$('#dialog').dialog('refreshShadow');
	});
	 
	// Contactform dialog.
	var buttons = {};
	buttons[LANG_CLOSE] = function()
	{
		$(this).dialog("close"); 
	}
	
	$('#dialog').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 600,
 		position: new Array('center', 125),
		buttons: buttons
	});
	
	// Contactform initiator.
	$('#open-contact-form').click(function()
	{
		$('#dialog').dialog('open');
		$('#contact-success').hide();
		$('#contact-failed').hide();
		$('#contacttable').show();
		
		// Make use of the added hack to the core which fixed the shadow not being resized properly.
		$('#dialog').dialog('refreshShadow');
		
		return false;
	});

	// Contactform submition.
	$("#contactsubmit").click(function()
	{					   				   
		$(".ui-state-error").hide();
		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		// Retrieve values.
		var companynameval = $("#companyname").val();
		var contactnameval = $("#contactname").val();
		var contactphoneval = $("#contactphone").val();
		var contactemailval = $("#contactemail").val();
		var contactsubjectval = $("#contactsubject").val();
		var contactmessageval = $("#contactmessage").val();
		
		// Check values.
		if(contactemailval == '')
		{
			$("#contactemail").after('<span class="ui-state-error">' + LANG_FORGOT_EMAIL + '</span>');
			hasError = true;
		}
		else if(!emailReg.test(contactemailval))
		{	
			$("#contactemail").after('<span class="ui-state-error">' + LANG_INVALID_EMAIL + '</span>');
			hasError = true;
		}
			
		if(contactsubjectval == '')
		{
			$("#contactsubject").after('<span class="ui-state-error">' + LANG_FORGOT_SUBJECT + '</span>');
			hasError = true;
		}
		
		if(contactmessageval == '')
		{
			$("#contactmessage").after('<span class="ui-state-error">' + LANG_FORGOT_MESSAGE + '</span>');
			hasError = true;
		}
		
		// When we don't have an error, we post our vars to our mailingscript which will actually send our e-mail.
		if(hasError == false)
		{		
			$.ajax({ type:'POST', url:'http://www.geuenslommel.be/email.php', datatype:'html', data:
			{
				companyname:companynameval,
				contactname:contactnameval,
				phone_number:contactphoneval,
				from_email:contactemailval,
				subject:contactsubjectval,
				message:contactmessageval
			}, 
			complete:function(data)
			{
				// On completion, we check our response and show the appropriate message.
				var response = data.responseText;
				
				$("#contacttable").slideUp("normal", function()
				{
					//if(response == 'OK')
					{
						$('#contact-success').show();					
					}
					//else
					{
						//$('#contact-failed').show();
					}
					
					// Make use of the added hack to the core which fixed the shadow not being resized properly.
					$('#dialog').dialog('refreshShadow');
				});
			}});
			
			// Reset form values.
			$("#companyname").val('');
			$("#contactname").val('');
			$("#contactphone").val('');
			$("#contactemail").val('');
			$("#contactsubject").val('');
			$("#contactmessage").val('');
		}
		else
		{
			// Make use of the added hack to the core which fixed the shadow not being resized properly.
			$('#dialog').dialog('refreshShadow');
		}
		
		return false;
	});

	
	/*****************************************************
	 *
	 *                     Submenu's                      
	 *
	 *****************************************************/
	 
	 
	// Display submenu when clicked.
	$("ul.menu li.parent").click(function ()
	{
		// Because our list-item contains a link, we disable it so it's won't disturb our sliding in/out submenu.
		var a = this.getElementsByTagName('a')[0];
		a.href = '#';
	
		// Last part contains an unique classname, we extract it and with it we can slide in/out our submenu.
		var classNameSplit = this.className.split(" ");

		// Slide in/out our submenu.
		$("li." + classNameSplit[1] + " ul").slideToggle("1000");
    });
	
	$("ul.menu li.parent ul").click(function ()
	{
		return false;
	});
	
	$("ul.menu li.parent ul li a").click(function ()
	{
		window.location = this.href;
	});

	
	/*****************************************************
	 *
	 *                   Article Links                    
	 *
	 *****************************************************/
	
	
	// This extension will parse all links to articles on every page and extract the article from the link and parses it so it can be displayed in a jQuery dialog.
	$('#window').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 600,
 		position: new Array('center', 125),
		title: LANG_PLEASE_WAIT,
		buttons: buttons
	});
	
	var links = document.getElementsByTagName('a');

	for(i = 0; i < links.length; i++)
	{
		if(links[i].className == 'open' && links[i].target == '_blank')
		{
			links[i].onclick = function()
			{
				var linkHref = this.href;
				var linkSlashSplit = linkHref.split('/');
				var linkDotSplit = linkSlashSplit[linkSlashSplit.length - 1].split('.');
				var linkId = linkDotSplit[0];

				// Since we use AJAX to retrieve our content, this will cause a delay between the link being clicked and the content being shown.
				// So we are going to be nice and display the window with a loadingbar.
				var contentWrapper = document.createElement('div');
				var img = document.createElement('img');
				img.src = baseurl + '/templates/sod/images/other_loading.gif';
				img.width = 220;
				img.height = 19;
				img.style.margin = '0 auto 0 auto';
				img.alt = LANG_PLEASE_WAIT;
				img.title = LANG_PLEASE_WAIT;
				
				contentWrapper.appendChild(img);
				contentWrapper.style.width = '100%';
				contentWrapper.style.textAlign = 'center';
				contentWrapper.style.marginTop = '22px';
				
				var custom_window = document.getElementById('window-content');
				custom_window.innerHTML = '';
				custom_window.appendChild(contentWrapper);
			
				$('#window').dialog('option', 'title', LANG_PLEASE_WAIT);
				$('#window').dialog('open');
				$('#window-content').show();
			
				// Get contents and fill our window with it.
				$.ajax({ type:'GET', url:'http://www.geuenslommel.be/joomla/index.php', datatype:'html', data:{option:'com_content',view:'article',id:linkId,Itemid:'100',lang:LANG}, complete:function(data)
				{
					// Create a wrapper so we can use DOM to access the data.
					var wrapper = document.createElement('div');
					wrapper.innerHTML = data.responseText;

					// Extract pagecontent from div.		
					var divs = wrapper.getElementsByTagName('div');
					var content;
					var content_title;
					
					for(i = 0; i < divs.length; i++)
					{
						if(divs[i].id == 'content')
						{
							// Setting title.
							content_title = '';
							
							// Setting content.
							content = divs[i].getElementsByTagName('table')[0].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[0].getElementsByTagName('td')[0].innerHTML;
							
							break;
						}
						
						if(divs[i].className == 'componentheading')
						{
							alert('!');
							divs[i].parentNode.removeChild(divs[i]);
						}
					}
					
					// Close our window containing the loadingbar.
					$('#window').dialog('close');
					
					// Set content.
					if(data.statusText == 'Not Found')
					{
						var contentWrapper = document.createElement('div');
						contentWrapper.style.width = '100%';
						contentWrapper.style.textAlign = 'center';
						contentWrapper.style.marginTop = '22px';
						contentWrapper.innerHTML = LANG_ARTICLE_NOT_FOUND;
						custom_window.innerHTML = '';
						custom_window.appendChild(contentWrapper);
						content_title = LANG_ARTICLE_NOT_FOUND;
					}
					else
					{
						custom_window.innerHTML = content;
					}
					
					// Display window again.
					$('#window').dialog('option', 'title', content_title);
					$('#window').dialog('open');
					$('#window-content').show();
					
					// Make use of the added hack to the core which fixed the shadow not being resized properly.
					$('#window').dialog('refreshShadow');
					
					document.getElementById('window').focus();
				}});
				
				return false;
			}
		}
	}
});
