// JavaScript Document

$(document).ready(function() {
	$('#emailLoading').hide();
	$('#email').blur(function(){
	  $('#emailLoading').show();
      $.post("includes/php/emailexist.php", {
        email: $('#email').val()
      }, function(response){
        $('#emailResult').fadeOut();
        setTimeout("finishAjax('emailResult', '"+escape(response)+"')", 400);
      });
    	return false;
	});
	
});

function finishAjax(id, response) {
  $('#emailLoading').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
} //finishAjax

$(document).ready(function() {
	$('#cpLoading').hide();
	$('#cp').blur(function(){
	  $('#cpLoading').show();
      $.post("includes/php/listvilles.php", {
        cp: $('#cp').val()
      }, function(response){
        $('#cpResult').fadeOut();
        setTimeout("finishAjaxVille('ville', '"+escape(response)+"')", 400);
      });
    	return false;
	});
});

function finishAjaxVille(id, response) {
  $('#cpLoading').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
} //finishAjax

$(document).ready(function(){
                    $("#price-slider").slider({
                    steps:20000,
                    minValue:0,
                    maxValue:1000000,
                    slide:function(e,ui){
                    var prval=ui.slider.curValue;
                    $("#pfrom").val();
                    $("#pto").val();
                    
                    if($(ui.handle).attr("id")=="hand1"){
                    $("#price1").text(prval);
                    $("#pfrom").val(prval);
                    } else {
                    if(ui.value == 1000000) {
                    $("#price2").text('1000000 et plus'); 
                    }
                    else
                    $("#price2").text(prval);
                    $("#pto").val(prval);
                    }
                    }
                    ,change:function(e,ui){
                    if(ui.value == 1000000) {
                    $('#hand1').css('zIndex', 2); 
                    $('#hand2').css('zIndex', 1);
                    }
                    else {
                    $('#hand1').css('zIndex', 1);
                    $('#hand2').css('zIndex', 2);
                    }
                    }
                    });
                    });  


            $(document).ready(function(){
                 //thickbox replacement
    var closeModal = function(hash)
    {
        var $modalWindow = $(hash.w);

        //$('#jqmContent').attr('src', 'blank.html');
        $modalWindow.fadeOut('2000', function()
        {
            hash.o.remove();
            //refresh parent

            if (hash.refreshAfterClose === 'true')
            {

                window.location.href = document.location.href;
            }
        });
    };
    var openInFrame = function(hash)
    {
        var $trigger = $(hash.t);
        var $modalWindow = $(hash.w);
        var $modalContainer = $('iframe', $modalWindow);
        var myUrl = $trigger.attr('href');
        var myTitle = $trigger.attr('title');
        var newWidth = '520', newHeight = '400', newLeft = '50%', newTop = '17%';
        $modalContainer.html('').attr('src', myUrl);
        $('#jqmTitleText').text(myTitle);
        myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
        var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;

        if (queryString != null && typeof queryString != 'undefined')
        {
            var queryVarsArray = queryString.split("&");
            for (var i = 0; i < queryVarsArray.length; i++)
            {
                if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
                {
                    var newWidth = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
                {
                    var newHeight = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
                {
                    // if true, launches a "refresh parent window" order after the modal is closed.

                    hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
                } else
                {

                    hash.refreshAfterClose = false;
                }
            }
            // let's run through all possible values: 90%, nothing or a value in pixel
            if (newHeight != 0)
            {
                if (newHeight.indexOf('%') > -1)
                {

                    newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));

                }
                var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
            }
            else
            {
                newHeight = $modalWindow.height();
            }
            if (newWidth != 0)
            {
                if (newWidth.indexOf('%') > -1)
                {
                    newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
                }
                var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

            }
            else
            {
                newWidth = $modalWindow.width();
            }

            // do the animation so that the windows stays on center of screen despite resizing
            $modalWindow.css({
                width: newWidth,
                height: newHeight,
                opacity: 0
            }).jqmShow().animate({
                width: newWidth,
                height: newHeight,
                top: newTop,
                left: newLeft,
                marginLeft: 0,
                opacity: 1
            }, 'slow');
        }
        else
        {
            // don't do animations
            $modalWindow.jqmShow();
        }

    }

    $('#modalWindow').jqm({
        overlay: 70,
        modal: true,
        trigger: 'a.thickbox',
        target: '#jqmContent',
        onHide: closeModal,
        onShow: openInFrame
    });

            });
