$(document).ready(function() {

        var validateEmail = $('#validateEmail');
        $('#mail').keyup(function () {
            var t = this;
            if (this.value != this.lastValue) {
                if (this.timer) clearTimeout(this.timer);
                $('#validateEmail').html('<center><img src="images/ajax-loader.gif" height="16" width="16" /></center>');

                this.timer = setTimeout(function () {
                    $.ajax({
                        url: '/ajax/mailValidation.php',
                        data: 'action=isValidMail&mail=' + t.value,
                        dataType: 'html',
                        type: 'post',
                        success: function (msg) {
                            validateEmail.html(msg);
                        }
                    });
                }, 200);
                this.lastValue = this.value;
            }
        });
        
        var validatePhone = $('#validatePhone');
        $('#phone').keyup(function () {
            var t = this;
            if (this.value != this.lastValue) {
                if (this.timer) clearTimeout(this.timer);
                $('#validatePhone').html('<center><img src="images/ajax-loader.gif" height="16" width="16" /></center>');

                this.timer = setTimeout(function () {
                    $.ajax({
                        url: '/ajax/phoneValidation.php',
                        data: 'action=isValidPhone&phone=' + t.value,
                        dataType: 'html',
                        type: 'post',
                        success: function (msg) {
                            validatePhone.html(msg); 
                        }
                    });
                }, 200);
                this.lastValue = this.value;
            }
        });        

	
	$('#rotatorInner').after('<div id="rotatorNav"><div id="rotatorNavButtons">').cycle({
	    fx:     'scrollDown', 
	    easing: 'bounceout', 
	    delay:  -2000, 
		pager : '#rotatorNavButtons'
	});
	$('#shuffler').cycle({ 
	    fx:   'fade', 
	    delay: 1500,
	    speed: 2000
	});		
    $('#folioRotatorInner').cycle({
        fx:      'fade',
        timeout:  8000,
        prev:    '#prev',
        next:    '#next',
		pager : '#nav',
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '<a href="javascript:void(0)" onFocus="blur()"><img src="images/pager_' + eval(idx+1) + '.png" width="19" height="19" /></a>'; 
	    } 		
    });
 
});

function confirmSend() {
	var mail = $('#mail').val();
	var phone = $('#phone').val();
    $.ajax({
        type: "POST",
        url: "/ajax/mailSend.php",
        data: "mail=" + mail + "&phone=" + phone,
        success: function(msg){
            $("#darken-window").html(msg);
            $("#darken").css('height',$(document).height());
            $("#darken, #darken-window").fadeIn(300);

        }
    });
}

function confirmSend2() {
	var name = $('#nameInput').val();
	var mail = $('#emailInput').val();
	var phone = $('#phoneInput').val();
	var message = $('#requestInput').val();
    $.ajax({
        type: "POST",
        url: "/ajax/mailSend2.php",
        data: "mail=" + mail + "&phone=" + phone + "&name=" + name + "&message=" + message,
        success: function(msg){
            $("#darken-window").html(msg);
            $("#darken").css('height',$(document).height());
            $("#darken, #darken-window").fadeIn(300);

        }
    });
}

function fadeOutWindow() {
    $("#darken, #darken-window").fadeOut(300);	
}

function showLoginCloud() {
    $('#validateEmail').fadeIn('slow', function() {});
}

function show(name) {
	$(name).show();
}

function hideAll() {
	$('#folioMenu').hide();
	$('#offerMenu').hide();
}

function fadeInCover(id) {
	$('#folioCover'+id).fadeIn(250);
}

function fadeOutCover(id) {
	$('#folioCover'+id).fadeOut(250);
}

function submitHover(i) {
	if(i == 1) {
		$('#submitButton').attr({ src : '/images/formSubmitHover.jpg'});
	} else {
		$('#submitButton').attr({ src : '/images/formSubmit.jpg'});		
	}
}

function Sidebar(element, stopBox, height, elementHeight) {
	var win = $(window), elementOffset = element.offset(), stopBoxOffset = stopBox.offset();
	if (win.scrollTop() >= height) {
		element.stop().animate({
			marginTop : win.scrollTop() > stopBoxOffset.top - elementHeight ? stopBoxOffset.top - elementHeight - height : win.scrollTop() - height
		});
	}
	win.scroll(function() {
		stopBoxOffset = stopBox.offset();
		if (win.scrollTop() >= height) {
			if(stopBoxOffset.top - elementHeight - height > 0) {
			element.stop().animate(
					{
						marginTop : win.scrollTop() > stopBoxOffset.top - elementHeight ? stopBoxOffset.top - elementHeight - height : win.scrollTop() - height
					});
			}
		} else {
			element.stop().animate({
				marginTop : 0
			});
		}
	});
}
