var OVERLAY_PATH = '/embed/_overlay-content/';

function send_contacts() {
    var form = $("form.overlay-contact");

    $('.overlay-content-placeholder').load(OVERLAY_PATH + ' .overlay-content-contact-ok', function() { 
        show_overlay('placeholder');
    });

    
    $.post('http://www.le.dk/backend/contact', { 
        email: $('[name=email]', form).val(), 
        name: $('[name=name]', form).val(), 
        phone_interval : $('[name=call-at]', form).val(), 
        phone : $('[name=phone]', form).val() 
    }, function(data) {
        
        if(data == 'ok') {
			$('.overlay-content-placeholder').load(OVERLAY_PATH + ' .overlay-content-contact-ok', function() { 
				show_overlay('placeholder');
            });
        } else {
            alert('fejl');
        }

    }, 'text');    
        
    return false; 
}

function parseNumber(str) {
	str = str.toString().replace(/[^0123456789,-]/ig, "");
	str = parseFloat(str.replace(",","."));
	return str;
}

/* Rounds a number to the nearest neighboor. Pure Math.round is "broken". */
function roundNumber(no, places) {
    if(places == undefined) places = 0;
    return Math.round(no * Math.pow(10, places)) / Math.pow(10, places)
}

/* Given a float or integer this function returns a Danish formatted number (as a string) */
function prettyNumber(n) {
    var strValue = n.toString().replace('.', ';');
    var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');
    while(objRegExp.test(strValue)) {
        strValue = strValue.replace(objRegExp, '$1.$2');
    }
    
    if(strValue == "NaN") {
        return 0;
    } else {
        return strValue.replace(';', ',');
    }
}

/* Returns a number in Danish format with a given precision. */
function formatNumber(str, decimals) {
    if(str == undefined) return str;
    if(decimals == undefined) decimals = 0;
    return prettyNumber(roundNumber(parseFloat(str.toString().replace(/,/g, '.')), decimals));
}

function show_overlay(content_name, callback) {
	// adjust overlay to visible content
	$('.overlay').css('margin-top', -parseInt($('.overlay').height()/2));
	
	// specifically fadeTo opacity 1, not just "in",
	// since stopped animation may have cause fadeIn to remember undesired opacity values
	$('.overlay-dim').fadeTo(300, 1, function() {
		$('.overlay').fadeTo(300, 1, callback);
	});

	$('.overlay-close, .overlay-dim').unbind('click').click(function() {
		// stop animation since they may be fading in
		$('.overlay, .overlay-dim').stop().fadeOut(300);
	});
}

// general call-me functionality {{{
$(document).ready(function() {
	$('.call-me-button').click(function() {

		$('.overlay-content-placeholder').load(OVERLAY_PATH + ' .overlay-content-callme', function() { 
			show_overlay('placeholder', function() {
				// focus the first text input, if any
				$('.overlay form input[type=text]:first').each(function() {
					this.focus();
				})
			});
		});
		return false;
	});
});
// }}}

// general business call-me functionality {{{
$(document).ready(function() {
	$('.call-me-button-business').click(function() {

		$('.overlay-content-placeholder').load(OVERLAY_PATH + ' .overlay-content-businesscontact', function() { 
			show_overlay('placeholder', function() {
				// focus the first text input, if any
				$('.overlay form input[type=text]:first').each(function() {
					this.focus();
				})
			});
		});
		return false;
	});
});
// }}}


// apply a graphic arrow to call-to-action buttons that needs them {{{
$(document).ready(function() {
	$('.cta-button.cta-button-arrow').wrapInner('<span class="txt"></span>').append('<span class="cta-arrow"></span>');
	$('.call-me-button').wrapInner('<span class="txt"></span>').append('<span class="cta-phone"></span>');
})
// }}}


$(document).ready(function() {
	// apply a graphical arrow on the active menu item
	$('.top .menu-items li.active').append('<div class="active-arrow"></div>');
	$('.top .menu-items li.parent-active').append('<div class="active-arrow"></div>');

	$('.footer-share .share-fb').attr('href', 'https://www.facebook.com/sharer.php?u=' + encodeURIComponent(location.href) + '&t=' + encodeURIComponent(document.title)).click(function() {
		window.open($(this).attr('href'), '_blank', 'width=600, height=300');
		return false;
	});
	$('.footer-share .share-tw').attr('href', 'http://twitter.com/home?status=Spar penge med Solceller fra Living Energy: ' + encodeURIComponent(location.href)).click(function() {
		window.open($(this).attr('href'), '_blank', 'width=600, height=300');
		return false;
	});
	$('.footer-share .share-gp').attr('href', 'https://m.google.com/app/plus/x/?content=' + encodeURIComponent(document.title + ' - ' + location.href) + '&v=compose&hideloc=1').click(function() {
		window.open($(this).attr('href'), '_blank', 'toolbar=0,resizable=1,scrollbars=1,status=1,width=450,height=295');
		return false;
	});

	// newsletter signup functionality
	$('.newsletter-signup form').submit(function() {
		$.post('http://www.le.dk/backend/mailinglist', { email: $('[name=email]', this).val() }, function(data) {

			if(data == 'ok') {
				$('.overlay-content-placeholder').load(OVERLAY_PATH + ' .overlay-content-newsletter-ok', function() { 
					show_overlay('placeholder');
				});
			} else {
				alert('fejl');
			}

		}, 'text');
		return false;
	})
	

	// stepping
	$('.steps .step-title').click(function() {
		$(this).siblings('.step-text').slideToggle();
	});

	$('.sticky-calc-banner').hover(function() {
		$(this).stop().animate({ right: '0' }, 200);
	}, function() {
		$(this).stop().animate({ right: '-95px' }, 200);
	});
	$('.sticky-calc-banner').delay(1500).animate({ right: '-95px' }, 200);

	// note: must remove element on click, to avoid mouseleave animating it out
	$(document).on('click', 'label.error', function() { $(this).fadeOut(300, function() { $(this).remove(); }); });
	$(document).on('mouseenter', 'label.error', function() { $(this).fadeTo(50, 0.3); });
	$(document).on('mouseleave', 'label.error', function() { $(this).fadeTo(50, 1); });
});

