$(document).ready(function() {
    var intervalId = 0;
    var cyrcle = false;
    
    $("#radio-link").bind("click", function () {
        if ($("#send-form").is(":hidden")) {
            $("#send-form").show();
        }
    });
    $("#send-form #form-close").bind("click", function () {
        if ($("#send-form").not(":hidden")) {
            $("#send-form").hide();
        }
    });
    
    if ($("#carousel2 ul > li").length) {
        $("#carousel2 ul > li a").lightBox();
    }
    
    if ($("a[rel='lightbox-myGroup']").length) {
        $("a[rel='lightbox-myGroup']").lightBox();
    }

    if ($("#carousel1 ul li").length && $("#carousel1 ul li").length > 7) {
        $("#carousel1").carousel('#prevvv1', '#nexttt1');

        function next1click() {
            if (cyrcle) {
                $("#nexttt1").click();
                intervalId = window.setTimeout(next1click, 1000);
            }
        }
        function prev1click() {
            if (cyrcle) {
                $("#prevvv1").click();
                intervalId = window.setTimeout(prev1click, 1000);
            }
        }

        $("#nexttt1").bind("mousemove", function() {
            cyrcle = true;
            next1click();
        });
        $("#nexttt1").bind("mouseleave", function() {
            window.clearTimeout(intervalId);
            cyrcle = false;
        });

        $("#prevvv1").bind("mousemove", function() {
            cyrcle = true;
            prev1click();
        });
        $("#prevvv1").bind("mouseleave", function() {
            window.clearTimeout(intervalId);
            cyrcle = false;
        });
    }
    if ($("#carousel2 ul li").length && $("#carousel2 ul li").length > 7) {
        $("#carousel2").carousel('#prevvv2', '#nexttt2');

        function next2click() {
            if (cyrcle) {
                $("#nexttt2").click();
                intervalId = window.setTimeout(next2click, 1000);
            }
        }
        function prev2click() {
            if (cyrcle) {
                $("#prevvv2").click();
                intervalId = window.setTimeout(prev2click, 1000);
            }
        }

        $("#nexttt2").bind("mousemove", function() {
            cyrcle = true;
            next2click();
        });
        $("#nexttt2").bind("mouseleave", function() {
            window.clearTimeout(intervalId);
            cyrcle = false;
        });

        $("#prevvv2").bind("mousemove", function() {
            cyrcle = true;
            prev2click();
        });
        $("#prevvv2").bind("mouseleave", function() {
            window.clearTimeout(intervalId);
            cyrcle = false;
        });
    }
    if ($("#carousel3 ul li").length && $("#carousel3 ul li").length > 8) {
        $("#carousel3").carousel('#prevvv3', '#nexttt3');

        function next3click() {
            if (cyrcle) {
                $("#nexttt3").click();
                intervalId = window.setTimeout(next3click, 1000);
            }
        }
        function prev3click() {
            if (cyrcle) {
                $("#prevvv3").click();
                intervalId = window.setTimeout(prev3click, 1000);
            }
        }

        $("#nexttt3").bind("mousemove", function() {
            cyrcle = true;
            next3click();
        });
        $("#nexttt3").bind("mouseleave", function() {
            window.clearTimeout(intervalId);
            cyrcle = false;
        });

        $("#prevvv3").bind("mousemove", function() {
            cyrcle = true;
            prev3click();
        });
        $("#prevvv3").bind("mouseleave", function() {
            window.clearTimeout(intervalId);
            cyrcle = false;
        });
    }

    $("#menu1").bind("mousemove", function() {
        $("#padding-menu1").show();
        $(this).find("> a").css({color:"#e69617"});
    });
    $("#menu1").bind("mouseleave", function() {
        $("#padding-menu1").hide();
        $(this).find("> a").css({color:"#fff"});
    });
    
    $("#menu2").bind("mousemove", function() {
        $("#padding-menu2").show();
        $(this).find("> a").css({color:"#e69617"});
    });
    $("#menu2").bind("mouseleave", function() {
        $("#padding-menu2").hide();
        $(this).find("> a").css({color:"#fff"});
    });
    
    $("#menu3").bind("mousemove", function() {
        $("#padding-menu3").show();
        $(this).find("> a").css({color:"#e69617"});
    });
    $("#menu3").bind("mouseleave", function() {
        $("#padding-menu3").hide();
        $(this).find("> a").css({color:"#fff"});
    });
    
    $("#menu4").bind("mousemove", function() {
        $("#padding-menu4").show();
        $(this).find("> a").css({color:"#e69617"});
    });
    $("#menu4").bind("mouseleave", function() {
        $("#padding-menu4").hide();
        $(this).find("> a").css({color:"#fff"});
    });
    $("#send-form-email").bind("submit", function(){
        if (!($("#send-form-email").val() || $("#send-form-phone").val())) {
            alert("Введите Ваши контакты!");
            return false;
        }
        if (!$("#send-form-text").val()) {
            alert("Введите ваше сообщение!");
            return false;
        }
        
        return true;
    });
    $("#send-form-cancel").bind("click", function() {
        $("#send-form").hide();
        
        return false;
    });
    $("#question-pic").bind("click", function (){
        if ($("#question-open-div").is(":hidden")) {
            $("#question-open-div").show();
        }
        
        return false;
    });
    $("#question-open-div-close").bind("click", function (){
        $("#question-open-div").hide();
        return false;
    });

    if ($(".submit-button-for-form").length) {
        $(".submit-button-for-form").bind("click", function (){
            $(this).parent().parent().parent().parent().parent().submit();
        });
    }
});

/* a jquery plugin for clearing search field */
var active_color = '#000'; // Colour of user provided text
var inactive_color = '#999'; // Colour of default text
$.fn.search = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
			this.style.color = active_color;
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			this.style.color = inactive_color;
		}
	});
};