var waitingMessage = '<img src="' + mediaURL + 'img/loading.gif" />';
var successMessage = '<p class="success">Thank you for your submission.<br /> We will contact you shortly</p>';
var failMessage = '<p class="error">There was an error submitting your info.<br />Please make sure you have entered a valid email address and supplied the required fields.</p>';
var budgetText = '<div id="slider"><div id="slider-handle"></div></div><br/><span id="budget-text">$2,500</span>';

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById('nav-list')
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteBottom(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop + o.offsetHeight            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function setDimensions(dropObject) {
	var left = getAbsoluteLeft('nav-list') - 1; /* account for border */
	var width = $('#nav-list').width() - 20 + 6; /* border - padding + sliding door margin */
	$('#' + dropObject).css({
		left:left+"px", 
		top:"29px",
		width: width
	});
}

function popup(linkObject) {
	/*
		Load content for popup via AJAX if necessary and open popup.
	*/
	id = linkObject.id + '-popup';
	setDimensions(id); /* verify we still have the right size */
	$('#' + id).slideDown();
}


$(function(){
	$('.ir').jQIR('gif', mediaURL + 'img/ir/');
	// prep contact form
	$('#clients-popup,#contact-popup').addClass('ajax-slide');
	$('#budget').append(budgetText);
	$('#contact-form').before('<div id="contact-message"></div>');
	$('.hidden').hide();
	
	$(window).resize(function(){ /*move our drop downs if the window resizes */
		$('.ajax-slide').each(function(){
			setDimensions(this.id);
		});
	});
	$('.container').click(function(){
		$('.ajax-slide').slideUp('fast');
	});

	$('.ajax').click(function(){
		var boxId = this.id + '-popup';
		var wasClosed = $('#' + boxId).is(':hidden');
		$('.ajax-slide').slideUp('fast');
		if (wasClosed) {
			popup(this);
		}
		/* get ready for an insanely dumb trick to make IE play nice */
		$('#nav-list').css({'padding':'3px 8px 20px 13px'}); 
		return false;
	});
	$('#id_contact-budget,#budget').hide();
	$('#id_contact-request').change(function(){
		if ($(this).val() == 'RFQ') {
			$('#budget').show();
			$('#slider').slider({ /* only works when parent is visible */
				min: 7000,
				max:100100,
                step:100,
				slide: function(e, ui) {
					if (ui.value == 100100) {
						var value = 'Over $100,000';
					}
					else {
						var value = String(Math.round(ui.value / 100) * 100);
						var regex = /(\d+)(\d{3})/;
							while (regex.test(value)) {
								value = value.replace(regex, '$1' + ',' + '$2');
							}
						value = '$' + value;
					}
					$('#id_contact-budget').val(value);
					$('#budget-text').text(value);
				}}); 
		}
		else {
			$('#budget').hide();
		}
	});
	$('#contact-form').submit(function(){
	    $('#contact-form').hide();
	    $('#contact-message').html(waitingMessage);
	    $('#contact-message').show();
	    url = $(this).attr('action');
	    data = {
	        name: $('#id_contact-name').val(),
            phone: $('#id_contact-phone').val(),
	        email: $('#id_contact-email').val(),
	        request: $('#id_contact-request').val(),
	        budget: $('#id_contact-budget').val(),
	        note: $('#id_contact-note').val(),
	        ajax: 'true'
	    };
	    $.post(url, data, function(response){
	        if (response == '1') {
	            $('#contact-message').html(successMessage)
	        }
	        else {
	            $('#contact-form').show();
	            $('#contact-message').html(failMessage);
	        }   
	    });
	    return false;
	});
	//highlight named anchors
	function highlight(elemId){
	    $(elemId).css("backgroundColor", "#ffffff"); // hack for Safari
	    $(elemId).animate({ backgroundColor: '#ffffaa' }, 1500);
	    setTimeout(function(){$(elemId).animate({ backgroundColor: "#ffffff" }, 3000)},1000);
	}
	
        if (document.location.hash) {
	    highlight(document.location.hash);
	}
	$('#main a[href*=#]').click(function(){
	    var elemId = '#' + $(this).attr('href').split('#')[1];
	    highlight(elemId); 
	});	
});
