// JavaScript Document
$(document).ready(function() {

    $('#offerte_tracking').click(function() {
        _gaq.push(['_trackPageview', '/offerteopmaat/openen']);
    });

	$('.menu li:not(.menu li li)').hover(
		function() {
			$(this).addClass('hover');
			$(this).find('ul').css('z-index', '100');
		},
		function() {
			$(this).removeClass('hover');
			$(this).find('ul').css('z-index', '10');
			Cufon.refresh();
		}
	)

	Cufon.replace('.menu li a:not(.menu li li a)', { hover: true });

	/* alleen selects */
	//$('.form .selects').jqTransform();
	$('.form .select').jqTransform();
	$('.offerte .text').jqTransform();
	$('.banners .item').jqTransform();

	/* tooltip in offerte module */
	$(".offerte .info").tooltip({
		tip: '#tooltip',
		position: "center left",
		effect: 'slide',
		offset: [-150, 163],
		opacity: 1,
		predelay: 500
	});

	/* set equal height to col */
	setEqualHeightCols();

	$("#datepicker").datepicker({
	    minDate: 0,
	    dateFormat: 'd MM yy'
    });

	$(".highslide-html-content").hide();

	$(".footer A").mouseover(function() { $(this).addClass("hover"); })
				  .mouseout(function() { $(this).removeClass("hover"); });

})

$('.overlay-s').click(function() {
    // file2 is de kleine versie van file1
    var newBG = $(this).css('background-image').replace('file2_', 'file1_');
    $('.overlay').css('background-image', newBG);
    $('.overlay-s').removeClass('active');
    $(this).addClass('active');
});

function updateImage(o){
	$('#icon').attr('class','');
	$('#image').attr('class','');
	//optioneel een icon
	if($(o).val().indexOf('size')==-1){
		$('#icon').addClass('hidden');
	}else{
		$('#icon').addClass($(o).val().replace('size','icon'));
		$('#icon').removeClass('hidden');
 	}
	//set image
	$('#image').addClass($(o).val());

}

function updateImageOfferte(o) {
    $('#icon').attr('class', '');
    $('#image').attr('class', '');
    $('#iconFinal').attr('class', '');
    $('#imageFinal').attr('class', '');
    //optioneel een icon
    if ($(o).val().indexOf('size') == -1) {
        $('#icon').addClass('hidden');
        $('#iconFinal').addClass('hidden');
    } else {
        $('#icon').addClass($(o).val().replace('size', 'icon'));
        $('#icon').removeClass('hidden');

        $('#iconFinal').addClass($(o).val().replace('size', 'icon'));
        $('#iconFinal').removeClass('hidden');
    }
    //set image
    $('#image').addClass($(o).val());
    $('#imageFinal').addClass($(o).val());

}

function setDisabled(o){
	//set disabled to all
	$(o).parent().find('.select').addClass('disabled');
	$(o).parent().find('select').attr('disabled','disabled').attr('title','');
	//remove disabled on selected
	$(o).removeClass('disabled');
	$(o).find('select').attr('disabled','').attr('title','Verplicht');
}

function setInput(o, showTime){
	$(".item[id^='cont']").hide();
	$("#cont-" + $(o).val()).show();
	if ($(o).val() == "mailen") {
		$("#offerte-time").hide().children().hide();
		$("#offerte-submit").html("mail mij");
		$("#contactType").val("email");
	} else {
		$("#offerte-time").show().children().show();
		$("#offerte-submit").html("bel mij");
		$("#contactType").val("bel");
	}
}

function setEqualHeightCols(){
	var highest = 0;
	// compare cols to get highest
	$('.col').each(function(){
		if(highest < $(this).height()){ highest = $(this).height();}		
	});
	// only increase height of smallest col
	$('.col').each(function(){
		if($(this).height() < highest){
			/* if more than one block, only increase last minus height other block(s) */
			if($(this).find('.block').size()>1){
				var restHeight = highest;
				$(this).find('.block:not(.block:last)').each(function(){
					restHeight -= $(this).height() + 15;
				});
				$(this).find('.bg:last').height(restHeight-44);
			} else {
				$(this).find('.bg').height(highest-44);
			}
		}
	});
}

function changeVestiging(sender) {
	document.location.href = $(sender).val();
}

function sendOfferte(form) {
	var result = false;
	$.ajax({
		type: "GET",
		url: PROJECT_URL + "nl/xml/BelOfMailMij",
		data: $(form).serialize(),
		async: false,
		success: function(theResponse) {
			result = true;
		}
	});
	
	// Hide steps
	$("#frmOfferte .offerte").hide();
	
	// Show result
	if (result == false) {
		$("#offerte-result .text").html("Er is een fout opgetreden tijdens het versturen van de offerte. Probeer het nogmaals.");
	}
	$("#offerte-result").show().css("visibility", "visible");
	
	return false;
}

function sendOfferteOverlay(form) {
	var result = false;
	$.ajax({
		type: "POST",
		url: PROJECT_URL + "nl/xml/RequestOfferte?langId=nl",
		data: $(form).serialize(),
		async: false,
		success: function(theResponse) {
			//$("#offerte-overlay-result").html(theResponse);
			result = true;
		}
    });
    $.ajax({
        type: "POST",
        url: PROJECT_URL + "offerteopmaat/verstuur?langId=nl",
        data: {},
        async: false,
        success: function(theResponse) {
            $("#offerte-overlay-result").html(theResponse);
            result2 = true;
        }
    });
	$(form).hide();
	if (!result) {
		$("#offerte-overlay-result").html("Er is een fout opgetreden tijdens het versturen van de offerte. Probeer het nogmaals.");
	}
	$("#offerte-overlay-result").show();
	return false;
}

function fromToStep(from, to) {
	if (from != '')
		$(".offerte." + from).css("visibility", "hidden");
	else
		$("DIV[class^=offerte step]").css("visibility", "hidden");

	if (to == "step3") {
		// Change icon according to selected size or housing
//		var box = ($("#amount:not(:disabled)").length > 0 ? "size" : "house");
//		$(".offerte.step3 .image").firstChild().attr("class", box + "");
	}
	
	$(".offerte." + to).css("visibility", "visible");
	$(".offerte." + to).jqTransform();

}
function openForm(open, close) {
  $("#"+close).css("display", "none");
  $("#"+open).css("display", "block");
}

function calcChoiceOpen(id) {
  $(".calcChoices .listing .main").addClass("ClosedMain")
  $(".calcChoices .listing .main").removeClass("main")
  
  $("#m_" + id).removeClass("ClosedMain")
  $("#m_"+id).addClass("main")
}

function m3Counter(id) {
  var part1 = $("#m3_" + id).html().replace(",", ".");
  var part2 = $("#a_" + id).val();
  var som = (part1 * part2);

  $("#r_" + id).val(som);
  countTotal();
}

function countTotal() {
  var total = 0
  var repTotal
  $("#calculator").find("input[id^=r]").each(function() {
    if ($(this).val() != '') {
      var value = parseFloat($(this).val());
      total = (total + value);
    }
  });
  repTotal = roundNumber(total,2)
  $("#cnt").val(repTotal);
  $("#cnt").addClass('alert')
  $("#cnt").fadeOut(200);
  setTimeout(resetInput, 200);
}

function fillm3(obj) {
  $("#cnt").val(obj.value);
  $("#cnt").addClass('alert')
  $("#cnt").fadeOut(200);
  setTimeout(resetInput, 200);
}
function resetInput() {
  $("#cnt").fadeIn(200);
  if ($('#cnt').hasClass('alert')) {
    $("#cnt").removeClass('alert');
  }
  
}
function roundNumber(num, dec) {
  var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
  return result;
}
function filldiverse(id) {
  
  var l = parseFloat($("#div_" + id + "_l").val());
  var b = parseFloat($("#div_" + id + "_b").val());
  var h = parseFloat($("#div_" + id + "_h").val());

  som = (l * b * h);
  if (!isNaN(l) && !isNaN(b) && !isNaN(h)) {
    $("#div_" + id + "_m3").html(som);
  }
  if ($("#div_" + id + "_a").val() != '') {
    var a = parseFloat($("#div_" + id + "_a").val());
    total = (som * a);
  }
  if (!isNaN(a) && !isNaN(som)) {
    $("#r_div_" + id).val(total);
    countTotal();
  }
}

function fbs_click(t) {
    u = location.href;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

function lin_click(t) {
    u = location.href;
    window.open('https://www.linkedin.com/cws/share?url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t), 'sharer2', 'toolbar=0,status=0,width=626,height=436');
    return false;
}
