$(document).ready(function(){

    //TOOLTIP
	
    //var
    var x = 10;
    var y = 30;
	var toolTip_width = 200;
    
    $(".toolTip").hover(function(e){
        this.toolTip_text = this.title;
        this.title = "";
        if (this.toolTip_text) {
            var t = "<p>" + this.toolTip_text + "</p>";
        }
        else {
            var t = "";
        }
        if (this.rel) {
            $("body").append("<div id='toolTip'><img src='" + this.rel + "' alt='img preview' />" + t + "</div>");
        }
        else {
			
            $("body").append("<div id='toolTip'>" + t + "</div>");
        }
        $("#toolTip").css("top", (e.pageY - x) + "px").css("left", (e.pageX + y) + "px").css("-moz-border-radius", "5px").fadeIn("fast");
    }, function(){
        this.title = this.toolTip_text;
        $("#toolTip").remove();
    });
    $(".toolTip").mousemove(function(e){
        $("#toolTip").css("top", (e.pageY - x) + "px").css("left", (e.pageX + y) + "px");
    });
    
    //RASCALS "FADE" MENU
	
	//var
    var hoverColor = "#005b7f";
    var normalColor = "#88ddff";
    
    $("ul#nav li a").before("<span></span>");
    $("ul#nav a").not(".active").prev().css("opacity", "0");
    $("ul#nav a.active").attr({
        href: "#"
    }).css({
        "cursor": "default",
        "color": hoverColor
    });
    $("ul#nav a").not(".active").hover(function(){
        $(this).prev().stop().animate({
            opacity: 1
        }, "slow");
        $(this).stop().animate({
            "color": hoverColor
        }, "slow");
    }, function(){
        $(this).prev().stop().animate({
            opacity: 0
        }, "slow");
        $(this).stop().animate({
            "color": normalColor
        }, "slow");
        
    });
    
    //RSS
    $("div#rss a").before("<div></div>");
    $("div#rss a").prev().css("opacity", "0");
    $("div#rss a").hover(function(){
        $(this).prev().stop().animate({
            opacity: 1
        }, "slow");
    }, function(){
        $(this).prev().stop().animate({
            opacity: 0
        }, "slow");
    });
    
    //UP
    $("div#up a").before("<div></div>");
    $("div#up a").prev().css("opacity", "0");
    $("div#up a").hover(function(){
        $(this).prev().stop().animate({
            opacity: 1
        }, "slow");
    }, function(){
        $(this).prev().stop().animate({
            opacity: 0
        }, "slow");
    });
	
	$("div#up a").click(function(e) {  
            e.preventDefault();
			var target=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body');
            target.animate({ 
                scrollTop: $($(this).attr("href")).offset().top 
            }, 500);
        });            

    
    //RASCALS FORMS
    
    
    //var
    var message_ok = "Thank you!";
    var message_error = "Can't send email!";
    var sending = "sending please wait..."
	
    var num_input = $("div.rascals_form").size();
    
    //newsletter 
    var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var newsletter = $(".newsletter").val();
    $(".newsletter").focus(function(){
        var current_newsletter = $(this).val();
        if (newsletter == current_newsletter || current_newsletter == message_ok || current_newsletter == message_error) {
            $(this).val("");
        }
    }).blur(function(){
        if ($(this).val() == "") {
            $(this).val(newsletter);
        }
    });
    
    
    $(".newsletter").keyup(function(){
        if ($(this).val().match(email)) {
            var newsletter_send = $(this).val();
            $(".newsletter-submit").fadeIn("slow").click(function(){
                $(".newsletter-submit").unbind("click")
                $(".newsletter").val(sending);
                $.post("newsletter.php", {
                    newsletter: newsletter_send
                }, function(data){
                    if (data == 'error') {
                        $(".newsletter-submit").fadeOut("slow");
                        $(".newsletter").val(message_error);
                    }
                    else {
                        $(".newsletter-submit").fadeOut("slow");
                        $(".newsletter").val(message_ok);
                    }
                });
                
            });
        }
        else {
            $(".newsletter-submit").fadeOut("slow");
        }
        
    });
    
    //booking
    
    function check(){
        if ($("div.rascals_form").find("span.active").size() == num_input) {
            $("input#send").fadeIn("slow").click(function(){
                $("input#send").unbind("click");
                $(this).fadeOut("slow");
                $("div.rascals_form_submit span").text(sending).fadeIn("slow");
                var form_name = $("#name").val();
                var form_email = $("#email").val();
                var form_message = $("#message").val();
                $("#name, #email, #message").val("");
                $("div.rascals_form span").removeClass("active").fadeOut("slow");
                $.post("booking.php", {
                    name: form_name,
                    email: form_email,
                    message: form_message
                }, function(data){
                    if (data == 'error') {
                        $("div.rascals_form_submit span").text(message_error).fadeIn("slow");
                    }
                    else {
                        $("div.rascals_form_submit span").text(message_ok).fadeOut(5000);
                    }
                });
                
            })
        }
        else {
            $("input#send").fadeOut("slow");
        }
    }
    
    
    $("#name").keyup(function(){
        if ($(this).val().length > 2) {
            $(this).next().addClass("active").fadeIn("slow");
            check();
        }
        else {
            $(this).next().removeClass("active").fadeOut("slow");
            check();
        }
    });
    $("#email").keyup(function(){
        if ($(this).val().match(email)) {
            $(this).next().addClass("active").fadeIn("slow")
            check();
        }
        else {
            $(this).next().removeClass("active").fadeOut("slow");
            check();
        }
    });
    $("#message").keyup(function(){
        if ($(this).val().length > 2) {
            $(this).next().addClass("active").fadeIn("slow")
            check();
        }
        else {
            $(this).next().removeClass("active").fadeOut("slow");
            check();
        }
    });
    
    
    //RASCALS "SLIDE" SIDEBAR
	
    $("ul#sidebar h3.active").next("p").css("display", "block");
    $("ul#sidebar h3").click(function(){
        if ($(this).is(".active")) {
            $(this).removeClass("active").next("p").slideUp("normal");
        }
        else {
            $(this).addClass("active").next("p").slideDown("normal");
            
        }
        
    });
    //collapse all
    $("span#collapse-all").click(function(){
        if ($("ul#sidebar h3").is(".active")) {
            $("ul#sidebar h3").removeClass("active").next("p").slideUp("normal");
        }
        
    });
    $("span#expand-all").click(function(){
        if ($("ul#sidebar h3").not(".active")) {
            $("ul#sidebar h3").addClass("active").next("p").slideDown("normal");
        }
        
    });
    
})

