var name;
var email;
var comment;

$(document).ready(function() {



    $("#contactForm_submit").click(function() {

        $('#contactForm').fadeOut(100);
        $('#antwort').html("Einen Moment bitte.");

        getData();

        var dataString = 'name='+ encodeURI(name) +
                         '&email='+ encodeURI(email) +
                         '&comment='+ encodeURI(comment);


        //alert ( ">> "+dataString ); return false;

        $.ajax({
        type: "POST",
        url: "form.php",
        data: dataString,
        success: function(inHtml) {
            $('#antwort').html(inHtml);
          }
        }); // close ajax
        return false;

    }); // close click
    

    $("a[rel=gal]").fancybox({
            'titleShow'		: false,
            'transitionIn'	: 'elastic',
            'transitionOut'	: 'elastic',
            'overlayColor'  : '#000'
    });


    //  change standard easing
    // -----------------------
    jQuery.easing.def = "easeOutQuint";

    // jQuery SmoothScroll | Version 09-11-02
    $('a[href*=#]').click(function() {

        return scrollTo(this);

    });



    //  submenu
    // -----------------
    $("#b_menu").hover(function() {
        $("#menu").fadeIn();
    },function(){
        $("#menu").fadeOut();
    });



    // collapse projekte & impressum
    $("#projektSwitch").toggle( function() {
        $("#projektContent").fadeOut();
        this.blur();
        return false;

     }, function() {
        $("#projektContent").fadeIn();
        this.blur();
        return false;
    });

    $("#impressumSwitch").toggle( function() {
        $("#impressumContent").fadeOut();
        this.blur();
        return false;

     }, function() {
        $("#impressumContent").fadeIn();
        this.blur();
        return false;
    });



    // Link AktuellBox
    $("#AProjekt a").click(function(){
        loadProject( $(this).attr('href') );
        this.blur();
        return false;
    });
    
    animatePList();
    makeCycle();

    $("#tagcloud a").click(function(){
        $("#tagcloud a").removeClass("active");
        $('#plist').html("<li style='color: #000;'>loading ...</li>");
        //$('#plist').animate({height: "100px"});
        loadPList( $(this).attr('href') );
        $(this).addClass("active");
        this.blur();
        return false;
    });

    setCloudTagLinks();

});




//  project bigImg slide
// ---------------------
function makeCycle() {

    $("#imgNext").click(function() {
        $('#slideImages').cycle('next');
        this.blur();
        return false;
    });

    $("#imgPrev").click(function() {
        $('#slideImages').cycle('prev');
        this.blur();
        return false;
    });


    $('#slideImages').cycle({
        fx:     'scrollHorz',
        speed:  '2300',
        timeout: 0
    });
}


//  Load Projekt
// --------------------
function loadProject( dataString ) {

    $('#projektContent').html("<p>loading ...</p>");

    //$("#projektContent").animate({opacity: 1, display: "block", height: "330px"});

    $.ajax({
        type: "POST",
        url: dataString,
        data: "",
        success: function(inHtml) {
        // output error
        if(inHtml=="error") {
            $('#projektContent').html("<p style='color:#ff0000'>"+inHtml+"</p>");

        // output
        } else {
            $('#projektContent').html(inHtml);
            makeCycle();
            $("#projektContent").fadeIn();
            setCloudTagLinks();
            scrollToIntern("#projekt");

        }
      }
    }); // close ajax

}



//   tagcloud
// ---------------------
function loadPList( dataString ) {

    $.ajax({
        type: "POST",
        url: dataString,
        data: "",
        success: function(inHtml) {
        // output error
        if(inHtml=="error") {
            $('#plist').html("<li style='color:#ff0000'>"+inHtml+"</li>").slideDown();

        // output
        } else {
            $('#plist').html(inHtml);
            animatePList();
        }
        //$('#plist').html(inHtml).slideDown();
        $('#plist').animate({height: "auto"});
        //$('#plist').dequeue();
      }
    }); // close ajax
}



//  Mouseover plist
// ---------------------
function animatePList() {
    $("#plist li").hover(function() {
        $(this).children(".text").fadeIn();
    },function(){
        $(this).children(".text").fadeOut();
    });


    $("#plist a").click(function(){
        loadProject( $(this).attr('href') );
        this.blur();
        return false;
    });
}

function getData() {

    name = $("input#cfname").val();
    email = $("input#cfemail").val();
    comment = $("#cfcomment").val();

}

function setCloudTagLinks() {
        
    $("#keywordEnv a").click(function(){
        $("#tagcloud a").removeClass("active");
        $('#plist').html("<li style='color: #000;'>loading ...</li>");
        //$('#plist').animate({height: "100px"});
        loadPList( $(this).attr('href') );
        this.blur();
        return false;
    });
}


function scrollTo( path ) {

       // easing values: swing | linear
       var easing='swing';

       // get / set parameters
       var newHash=path.hash;
       var target=$(newHash).offset().top;
       var oldLocation=window.location.href.replace(window.location.hash, '');
       var newLocation=path;

       // duration in ms
       var duration=1000;

       alert(oldLocation+""+newHash+" = "+newLocation);

       // make sure it's the same location
       if(oldLocation+newHash==newLocation)
       {
          // animate to target and set the hash to the window.location after the animation
          $('html,body').stop();
          $('html,body').animate({scrollTop: target}, duration, easing, function() {
             // add new hash to the browser location
          });

          // cancel default click action
          return false;
       }

       return true;
}


function scrollToIntern( newHash ) {

    // easing values: swing | linear
    var easing='swing';

    // get / set parameters
    var target=$(newHash).offset().top;

    // duration in ms
    var duration=1000;

    // animate to target and set the hash to the window.location after the animation
    $('html,body').stop();
    $('html,body').animate({scrollTop: target}, duration, easing);

}
