$(document).ready(function(){ 
	//Sitespecific javascript here
	//Searchbox clear and fill
	function input_values()
	{	
		var inputBox = $('#newsletter input, #search input');
	
		
		inputBox.focus(function(){
			if ($(this).val() != "") {
				
				var theValue = $(this).val();
				$(this).attr('temp', theValue)
				$(this).val('');
			};
		
		});
		inputBox.blur(function(){
			if ($(this).val() == "") {
				var theValue = $(this).attr('temp');
				$(this).val(theValue);
			};
			
		});
	}
	input_values();
	
	//Even and odd rows in tables
	/* if Environment is NOT Editor */
		$("table tr").mouseover(function() {
			$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");
		});
		$("table tr:nth-child(even), table tr td:nth-child(even)").addClass("even");
		$("table tr:nth-child(odd), table tr td:nth-child(odd)").addClass("odd");
		$("table tr:first-child").removeClass('even').addClass("th");
		$('table tr td:first').addClass('first');
		$('table tr td:last').addClass('last');

	
	/*Accordion menu*/
	//Hide submenus
	$('#sub_nav li ul').hide();
	//If menu option is current, show sub menu
	if ($('#sub_nav li:first-child').hasClass('current'))
		{
			$('#sub_nav ul:first-child > li > ul').slideToggle('slow');
		};
	//When clicking the top level, show submenu, but not sub sub menus
	$('#sub_nav ul:first-child > li > a').click(function() {
		
			$(this).parent().toggleClass('current');
		
		$(this).next().slideToggle('slow');
		return false;
	});
	
	/*query form fields*/
	//add new fields automatically by clicking a button
	$('#query_form .add_item').click(function(){
		var thisPrevClass = $(this).prev().attr('class');
		var thisLength = $('.'+thisPrevClass).length;
		var thisNumber = thisLength++;

		$('.'+thisPrevClass+':eq(0)').clone().insertBefore($(this));
		$(this).prev().find('input').each(function(){
			
			var currID = $(this).attr('id');
			var currVal = $(this).val();

			//Set the id to a new version
			$(this).attr('id',function(){
				return currID+thisNumber;
			});
			
			//Set name and value for the different inputs
			if ($(this).is('input[name^=form]')) {
				$(this).val('');
				$(this).attr('name',function(){			
					return 'form['+currID+thisNumber+']';
				});
					
				} else{
					
					$(this).attr('value',function(){
						return currVal+thisNumber;
					});
					$(this).attr('name',function(){
						return currID+thisNumber;
					});	
				}
		});
		return false;
	});
	
	//add/remove fields depending on selectbox
	$('#query_form #activity_number').change(function(){
		var numberofitems = $(this).val();
		var items = 1;

		$('.activities .item').fadeToggle('slow').remove();

		while (items <= numberofitems)
		{
			
			var itemObject = '<div class="uthyrdlokal item" style="display: none;"><span><label for="activity_type'+items+'">Verksamhet (Ex. kontor, butik, restaurang):</label>'
			+'<input type="text" maxlength="150" value="" class="textfield" id="activity_type'+items+'" name="form[activity_type'+items+']">'
			+'<input type="hidden" value="Verksamhet '+items+'" id="label_activity_type'+items+'" name="label_activity_type'+items+'">'
			+'</span><span>'
			+'<label for="activity_area'+items+'">Uthyrd area i m<sup>2</sup>:</label>'
			+'<input type="text" maxlength="6" value="" class="textfield" id="activity_area'+items+'" name="form[activity_area'+items+']">'
			+'<input type="hidden" value="Uthyrd area i m&lt;sup&gt;2&lt;/sup&gt;'+items+'" id="label_activity_area'+items+'" name="label_activity_area'+items+'">'
			+'</span></div>';
			
			$(this).parents('.activities').append(itemObject);

			items++;
		}
		$('.activities .item').fadeToggle('slow');
	
	});
	
	$('#query_form #injury_number').change(function(){
		var numberofitems = $(this).val();
		var items = 1;
		$('.skadelista .item').fadeToggle('slow').remove();

		while (items <= numberofitems)
		{
			
			var itemObject = '<div class="skadetillfalle item" style="display: none;"><span>'
			+'<label for="injury_year'+items+'">Skadeår:</label>'
			+'<input type="text" maxlength="150" value="" class="textfield" id="injury_year'+items+'" name="form[injury_year'+items+']">'
			+'<input type="hidden" value="Skadeår'+items+'" id="label_injury_year'+items+'" name="label_injury_year'+items+'">'
			+'</span><span>'
			+'<label for="injury_type'+items+'">Skadetyp (t.ex. vatten, brand, skadegörelse):</label>'
			+'<input type="text" maxlength="150" value="" class="textfield" id="injury_type'+items+'" name="form[injury_type'+items+']">'
			+'<input type="hidden" value="Skadetyp'+items+'" id="label_injury_type'+items+'" name="label_injury_type'+items+'">'
			+'</span><span>'
			+'<label for="injury_compensation'+items+'">Ersättning utbetald från försäkringen (kr):</label>'
			+'<input type="text" maxlength="150" value="" class="textfield" id="injury_compensation'+items+'" name="form[injury_compensation'+items+']">'
			+'<input type="hidden" value="Kompensation'+items+'" id="label_injury_compensation'+items+'" name="label_injury_compensation'+items+'">'
			+'</span></div>';
			
			$(this).parents('.skadelista').append(itemObject);
			//alert(items);
			items++;
		}
		$('.skadelista .item').fadeToggle('slow');
	
	});
	
	
	//Calculate size
	function calculateArea(){
		
		$('#building_area input.textfield:not(#area_total)').each(function(){
			if ($(this).val() == '') {
				$(this).val('0');
			}
			$('#area_total').val('');
			var vals_area1 = parseInt($('#area_living').val());
			var vals_area2 = parseInt($('#area_business').val());
			var vals_area3 = parseInt($('#area_cellar').val());
			$('#area_total').css('border','2px solid #000000').val(vals_area1+vals_area2+vals_area3);
			
			
		});
		
	}
	$('#building_area input.textfield:not(#area_total)').blur(function(){
		calculateArea();
	});
	
	//Show hidden contents
	//activities
	$('#activity1').click(function(){
		if ($(this).is(':checked')) {
			$('.activities').show('fast');
		} else{
			$('.activities').hide('fast');
				$('.activities input').val('');
		}
		
	});
	//injuries
	$('#injuries2, #injuries1').click(function(){
		if ($('#injuries2').is(':checked')) {
			$('.skadelista').show('fast');
		} else{
			$('.skadelista').hide('fast');
				$('.skadelista input').val('');
		}
	});
	//renovation
	$('#changes1').click(function(){
		if ($(this).is(':checked')) {
			$('.renovationlist').show('fast');
		} else{
			$('.renovationlist').hide('fast');
				$('.renovationlist input').val('');
		}
		
	});
	
	/*quoteform - media specific*/
	function removeSpaces(string) {
	 return string.split(' ').join('');
	}


	
	$('#premium_covers input,#premium_addons input').click(function(){
		//$('#premium_price_total').val('');
		
			var totalval = 0;
		
		
			var Myval;
			$('#premium_covers input:checked,#premium_addons input:checked:not(#sickbreak)').each(function(){
				Myval = parseInt(removeSpaces($(this).parent().find('em').text())); //Gets the number
				totalval += parseInt(Myval);
			});
			$('#premium_price_total').val(totalval);
	});
	
	/*quoteform - sfo specific*/
	$('#sfo_omfattning').hide();
	$('.inline input:not([type=text])').parent().contents().filter(function(){
			return this.nodeType == 3
			}).wrap('<em></em>').end();
	$('.no_people input').click(function(){
		var theVal = $(this).val();
		//set variables with values of ID's
		if (theVal == '1st') {
			var employees								= "1 st anställd";
			var ext_nordic							= 1370;
			var ext_world								= 3698;
			var ext_world_usa						= 6100;
			var extras_accident					= 793;
			var extras_service					= 371;
			var extras_board						= 1056;
			var extras_responsibility		= 773;			
			var extras_allrisk					= 0;
			var sum											= 0;

		}else{
			var employees								= "2-5 anställda";
			var ext_nordic							= 3698;
			var ext_world								= 3955;
			var ext_world_usa						= 6600;
			var extras_accident					= 1586;
			var extras_service					= 793;
			var extras_board						= 1056;
			var extras_responsibility		= 1545;			
			var extras_allrisk					= 0;
			var sum											= 0;	
		}
		
		//set visual for number of employees
		$('span.no_employees').html(employees);
		//for each input replace value and label with values from above
		$('.inline input#ext_nordic').val(ext_nordic).next('em').text(ext_nordic);
		$('.inline input#ext_world').val(ext_world).next('em').text(ext_world);
		$('.inline input#ext_world_usa').val(ext_world_usa).next('em').text(ext_world_usa);
		$('.inline input#extras_accident').val(extras_accident).next('em').text(extras_accident);
		$('.inline input#extras_service').val(extras_service).next('em').text(extras_service);
		$('.inline input#extras_board').val(extras_board).next('em').text(extras_board);
		$('.inline input#extras_responsibility').val(extras_responsibility).next('em').text(extras_responsibility);
		

		//reset input choices
		$('.inline input:checked').removeAttr('checked');
		$('input#sum,input#allrisk_special').val(0);
		$(this).attr('checked','checked');
		$('#sfo_omfattning').show('slow');
	return true;
	});
	//get all input checkboxes with valid values on change
		$('.inline input[type=checkbox]').change(function(){
			
			var sumTarget = $('input#sum'); //input to collect total value
			if (sumTarget.val() == '' || sumTarget.val() == null) {
				sumTarget.val('0');
			};			
			var sumValue = parseInt(sumTarget.val());
			var optionValue = parseInt($(this).val());
			//alert (sumValue+' '+optionValue);
			if (typeof(sumValue) != 'number') {
					sumValue = parseInt(sumValue);
				}
				else{
					sumValue;
				}
				if (typeof(optionValue) != 'number') {
					optionValue = parseInt(optionValue);
				}
				else{
					optionValue;
				}
			
			if ($(this).is(':checked') && $(this).is('[type=checkbox]')) {
				//add value of input
				sumTarget.val(sumValue+optionValue);
			} else{
				//subtract value of input
				sumTarget.val(sumValue-optionValue);
			}
		
		});
		//get all input radios with valid values on change
		$('.inline input[type=radio]').change(function(){
			
			var sumTarget = $('input#sum'); //input to collect total value
			
			//reset form
			sumTarget.val('0');
			$('.inline input[type=checkbox]').removeAttr('checked');
			$('#allrisk_special').val(0);
	
			var sumValue = parseInt(sumTarget.val());
			var optionValue = parseInt($(this).val());
			//alert (sumValue+' '+optionValue);
			if (typeof(sumValue) != 'number') {
					sumValue = parseInt(sumValue);
				}
				else{
					sumValue;
				}
				if (typeof(optionValue) != 'number') {
					optionValue = parseInt(optionValue);
				}
				else{
					optionValue;
				}
			
			if ($(this).is(':checked') && $(this).is('[type=radio]')) {
				//add value of input
				sumTarget.val(sumValue+optionValue);
			} else{
				//subtract value of input
				sumTarget.val(sumValue-optionValue);
			}
			
			//change the label for the different extent of the insurance
			var txt_nordic								= 'Omfattning- Norden';
			var txt_world									= 'Omfattning- Världen exkl. USA';
			var txt_world_usa							= 'Omfattning- Världen inkl. USA';
			switch($('.radio_extent:checked').attr('id')){
				case 'ext_nordic':
				$('input[name=label_extent]').val(txt_nordic);
				break;
				case 'ext_world':
				$('input[name=label_extent]').val(txt_world);
				break;
				case 'ext_world_usa':
				$('input[name=label_extent]').val(txt_world_usa)
				break;
				default:
				break;
			}
		
		});
	//deal with textbox values
	$('#allrisk_special').focus(function(){
		if ($(this).val() != '' || $(this).val() != null) {
				var sumTarget = $('input#sum'); //input to collect total value
				var sumValue = sumTarget.val();
				var optionValue = $(this).val()*0.05; //The cost of all risk is 5% of total sum
				var minOption = 371;
				
				if (typeof(sumValue) != 'number') { //if the value is a string, make it an integer
					sumValue = parseInt(sumValue);
				}
				else{
					sumValue;
				}
				if (typeof(optionValue) != 'number') { //if the value is a string, make it an integer
					optionValue = parseInt(optionValue);
				}
				else{
					optionValue;
				}
				
				if (optionValue != 0 && optionValue <= minOption) { //if the value is greater than the max value of 371, set it to max value
					optionValue = minOption;
				}
				
				sumTarget.val(sumValue-optionValue); //remove the value from the sum
				$(this).val(0).next('em').remove(); //reset this input
		} else if($(this).val() == 0){
			
		}
		else{
			
		}
		
	
	}).blur(function(){
		var sumTarget = $('input#sum'); //input to collect total value
		var sumValue = parseInt(sumTarget.val());
		var optionValue = ($(this).val()*0.05); //The cost of all risk is 5% of total sum
		var minOption = 371;
		if (typeof(sumValue) != 'number') { //if the value is a string, make it an integer
					sumValue = parseInt(sumValue);
				}
				else{
					sumValue;
				}
				if (typeof(optionValue) != 'number') { //if the value is a string, make it an integer
					optionValue = parseInt(optionValue);
				}
				else{
					optionValue;
				}
				if (optionValue != 0 && optionValue <= minOption) { //if the value is greater than the max value of 371, set it to max value
					optionValue = minOption;
				}
		$(this).after('<em>&nbsp;('+optionValue+' kr)</em>');
		sumTarget.val(sumValue+optionValue); //Add value to the sum
	});
	
	
	
	
});
