$(document).ready( function() {
	
	/* SUBMENU */
	$('.Submenu ul ul').hide();
	$('li.active img').attr('src', 'assets/templates/images/Submenu-Arrow-Marked.png');
	$('li.active > ul').show(); 
	
	$('.Search input').focus( function () {
		if ( $(this).val() == 'Sök på webbplatsen' )
			$(this).val('');
	});
	
	$('.question li:first-child').prepend('<img src="assets/templates/images/ikon-plus.png">');
	
	$('.question li:first-child').css({ 'color' : '#005dab', 'cursor' : 'pointer', 'margin-bottom' : '7px' });
	$('.question li:last-child').css({ 'display': 'none', 'font-size' : '12px', 'line-height' : '20px'  });

	$('.question li:first-child').click( function(event) {
		event.stopPropagation();
		
		if( $(this).siblings('li:last').css('display') == 'none' ) {
			$(this).siblings('li:last').css({ 'display': 'inline' });
			$(this).children('img').attr('src', 'assets/templates/images/ikon-minus.png');	
		}
		else {
			$(this).siblings('li:last').css({ 'display': 'none' });
			$(this).children('img').attr('src', 'assets/templates/images/ikon-plus.png');  
		}
	});
	
	if( jQuery.browser.safari )
		$('.Image a').colorbox();
	else
		$('a[rel="colorbox"]').colorbox(); 
	
	/* Mail us */	
	$('.inputField').click( function() {
		if ( $(this).val() == $(this).attr('title') ) {
			$(this).val('');
			$(this).css({ 'color':'#000000','font-style':'normal'});
		}
	});
	
	$('.inputField').blur( function() {
		if ( $(this).val() == '' ) {
			$(this).css({ 'color' : '#888888','font-style':'italic' });
			$(this).val($(this).attr('title'));
		}
	});
	
	$('.sendMail').click( function() {
		
		var flag = true;
		var vars = '';
		
		$('.inputField').each( function() {
			if(vars == '') vars = $(this).attr('name') + '=' + $(this).val();
			else vars += '&' + $(this).attr('name') + '=' + $(this).val();
			
			if( $(this).val() == $(this).attr('title') ||  $(this).val() == '' ) {
				$(this).css({ 'color' : '#ff0000','font-style':'italic' });
				$(this).val($(this).attr('title'));
				flag = false;
			}
		});
		
		if ( flag ) {

			$('.buttonLoad').html('Skickar mail...');
			
			var output = $.ajax({
				type: 'POST',
				url: 'sendmail',
				data: vars,
				cache: false,
				dataType: 'json',
				async:false,
				error: function(asd){
					alert('Could not get server respons: '+ asd);
				},
				success: function(msg) {
					switch(msg.action) {
						case 'ok':
							$('.buttonLoad').html('Mail skickat');
						break;	
						
						case 'error':
							alert('Mail not sent');
						break;
					}
					
					
				}
			});
			
				
		} 
					
	});
	
});
