jQuery(document).ready(function(){
    Reflection.defaultHeight = 0.33;
    Reflection.defaultOpacity = 0.33;
/*
---------------------------------------
    People-Filter-Features
---------------------------------------
*/
    jQuery("#people-filter").keyup(function () {
        var filter = jQuery(this).val(), count = 0, count_not = 0;

        jQuery(".filtered:first tr:not(.notfiltered)").each(function () {
            if (jQuery(this).text().search(new RegExp(filter, "i")) < 0) {
                jQuery(this).addClass("hidden");
                count_not++;
            } else {
                jQuery(this).removeClass("hidden");
                count++;
            }
        });
        
        jQuery("#filter-count").text(count);    
        jQuery("#filter-count-total").text(count+count_not);
        jQuery("span.count").removeClass("hidden");

    });
    
    // Define what happens when the textbox comes under focus  
    // Remove the watermark class and clear the box  
    jQuery("#people-filter").focus(function() {  
  
        jQuery(this).filter(function() {  
  
            // We only want this to apply if there's not  
            // something actually entered  
            return jQuery(this).val() == "" || jQuery(this).val() == "Search people..."  
  
        }).removeClass("watermark").val("");  
  
    });  
  
    // Define what happens when the textbox loses focus  
    // Add the watermark class and default text  
    jQuery("#people-filter").blur(function() {  
  
        jQuery(this).filter(function() {  
  
            // We only want this to apply if there's not  
            // something actually entered  
            return jQuery(this).val() == ""  
  
        }).addClass("watermark").val("Search people...");  
  
    });  

    
/*
---------------------------------------
    Publication-Filter-Features
---------------------------------------
*/
    jQuery("#publication-filter").keyup(function () {
        filterPublications(this);
    });
    
        // Define what happens when the textbox comes under focus  
    // Remove the watermark class and clear the box  
    jQuery("#publication-filter").focus(function() {  
  
        jQuery(this).filter(function() {  
  
            // We only want this to apply if there's not  
            // something actually entered  
            return jQuery(this).val() == "" || jQuery(this).val() == "Search publications..."  
  
        }).removeClass("watermark").val("");  
  
    });  
  
    // Define what happens when the textbox loses focus  
    // Add the watermark class and default text  
    jQuery("#publication-filter").blur(function() {  
  
        jQuery(this).filter(function() {  
  
            // We only want this to apply if there's not  
            // something actually entered  
            return jQuery(this).val() == ""  
  
        }).addClass("watermark").val("Search publications...");  
  
    });  

        
/*
---------------------------------------
    Timeline-Features
---------------------------------------
*/
    // Timelines that are triggered by hovering the mouse over the spans
    // are the default.
    jQuery("div.timeline:not(.click) span.timeSpan").mouseover(function () {
        changeTime(this);
    });

    // Timelines that are triggered by clicking the tabs are indicated by the class "click".
    jQuery("div.timeline.click span.timeSpan").click(function () {
        changeTime(this);
    });
    
    // Timelines that are triggered by clicking the tabs get class "mover" when hovering over with the mouse.
    jQuery("div.timeline.click span.timeSpan").mouseover(function () {
        jQuery(this).addClass("mover");
    });

    jQuery("div.timeline.click span.timeSpan").mouseout(function () {
        jQuery(this).removeClass("mover");
    });

    
    // Select the requested time on the timeline
    var timeLineAccess = getQSAfter("timeline-");    
    if (timeLineAccess) changeTime("#time_" + timeLineAccess);
/*
---------------------------------------
    Publication-Features
---------------------------------------
*/    
    if (jQuery.dynaCloud) {
        jQuery.dynaCloud.auto = false;    
        jQuery.dynaCloud.scale = 2;    
        // Add chair-specific stopwords to the dynacloud
        jQuery.merge(jQuery.dynaCloud.stopwords, [ "bruegge", "conference", "international", "proceedings",
        "Wolf", "Global", "Herrmann", "IEEE", "Thurimella", "USA", "Workshop", "ACM", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" ]);
    }
    // Publication content is injected using ajax and on demand.
    // Here we load only the content of the first visible div.
    jQuery("div:not(.loaded):not(.hidden) > div.publicationContent > a.publicationLink").each(function() {
        loadPublicationList(this);
    });
    
               
/*
---------------------------------------
    Teaching-Features
---------------------------------------
*/    
    // Teaching content is injected using ajax and on demand.
    // Here we load only the content of the first visible div.
    jQuery("div:not(.loaded):not(.hidden) > a.teachingLink").each(function() {
        loadTeachingTerm(this);
    });   

/*
---------------------------------------
    Common Ajax-Features
---------------------------------------
*/    
    // Some content is injected using ajax
    jQuery("a.ajaxLink").each(function() {
        var link = jQuery(this).attr("href");
        jQuery(this).parent().addClass("loading");
        // Load the content and use only the content of the table that contains the real data
        jQuery(this).parent().load(link + " table.contentpaneopen", null, function(){
            // Indicate loaded status
            jQuery(this).removeClass("loading");
            jQuery(this).addClass("loaded");
        });
    });

/*
---------------------------------------
    "No translation"-delete          <-------------------------------------------------------------- TODO!
---------------------------------------
*/

    //jQuery(). <div class="jfdefaulttext">There are no translations available.</div> ;

/*
---------------------------------------
    Project-Features
---------------------------------------
*/    
    // Project teasers are injected using ajax
    jQuery("a.projectLink").each(function() {
        var link = jQuery(this).attr("href");
        jQuery(this).parent().addClass("loading");
        // Load the content and use only the content of the table that contains the real data
        jQuery(this).parent().load(link + " table.contentpaneopen", null, function(){
            // Indicate loaded status
            jQuery(this).removeClass("loading");
            jQuery(this).addClass("loaded");
        });
    });

    
    // Project Links click (temporary for section above)
    jQuery("span.projectTeaser").click(function() {
        var link = jQuery("#"+jQuery(this).text()).text();
        jQuery("#"+jQuery(this).text()).text("")
        jQuery(this).toggleClass("projectPlus");
        jQuery(this).toggleClass("projectMinus");
        jQuery("#"+jQuery(this).text()).addClass("loading");
        jQuery("#"+jQuery(this).text()).toggleClass("hidden");
        // Load the content and use only the content of the table that contains the real data
        //hack cause simballo didnt work and broke the whole script
        if(jQuery(this).text() == "simballo1") {
            jQuery("div#"+jQuery(this).text()).removeClass("loading");
            jQuery("div#"+jQuery(this).text()).addClass("loaded");
            jQuery("div#"+jQuery(this).text()).html("<h3>Simballo</h3><br />The goal of this project is the development of an intelligent online-learning software. In particular Simballo should support the acquisition of literacy for children aged between 4 and 8.<br />The aim of our research in this area is to discover synergies between behavioral analysis and software engineering through the realization of an intelligent learning framework.<br /><br />The research team for Simballo consists of psychologists as well as computer scientists.<br />");
        }
        else {
            jQuery("div#"+jQuery(this).text()).load(link + " table.contentpaneopen", null, function(){
                // Indicate loaded status
                jQuery(this).removeClass("loading");
                jQuery(this).addClass("loaded");
            });
        }
    });

    jQuery("img.currentImage.linkedImage").click(function () {
//        alert('test');
        jQuery(this).addClass('Gotcha');
    });

    
    // Apply MooFlow-specific scripts here, only if we have the according tag on the page
    jQuery("#contentMooFlowProjects").each(function() {
    	var myMooFlowPage = {
        	start: function(){
                var mf = new MooFlow($('mooFlowProjects'), {
                    bgColor: '#ffffff',
                    factor: 100,
                    heightRatio: 0.5,
                    offsetY: -110,
                    startIndex: 0,
                    useSlider: true,
                    useAutoPlay: false,
                    useCaption: true,
                    useResize: false,
                    useMouseWheel: true,
                    useKeyInput: true,
                    useViewer: false,
				
                    'onClickView': function(mixedObject){
//                        alert(mixedObject.id);
//                            alert(mf.getCurrent().longdesc);
                        //alert(mf.loadedImages[mf.index]);
                    },
                    'onClickCurrentView': function(mixedObject){
//                        alert(mixedObject.id);
//                            alert(mf.getCurrent().longdesc);
                        //alert(mf.loadedImages[mf.index]);
//                        alert('ere');
                        if (mixedObject.href) {
                            location.href = mixedObject.href;
                        }

                    },
                    
                    'onElementChangeStart': function(obj){
                        $$("div.mooflowContentDiv").addClass("hidden");
// Evtl. hier beim obj eine class hinzu, die mit jquery beim click redirectet?

                        if (obj.id) {
//                            $(obj.id).toggleClass("projectLink");
                        }
                        $$("img.currentImage").removeClass("currentImage");
                        mf.loadedImages[mf.index].toggleClass("currentImage");


				        if (obj.longdesc) {
				            $(obj.longdesc).toggleClass("hidden");
				        }
                    }
                });
                
            }
        };
	
        window.addEvent('domready', myMooFlowPage.start);
        
    });





/*
---------------------------------------
    Menu-Features
---------------------------------------
*/    

// Hide all submenus    
    jQuery("li.mainlevel > ul, li.mainlevel_current > ul, li.mainlevel_active > ul").hide();

// Add hover effect to main menus
    jQuery('li.mainlevel, li.mainlevel_current, li.mainlevel_active').hover(
        function() {
            jQuery('ul', this).slideDown(250);
        }, function() {
            jQuery('ul', this).stop(true,true).hide();
    });

});

// Filter publications
function filterPublications(elt) {
    var filter = jQuery(elt).val(), count = 0, count_not = 0;
    
    if (filter == "Search publications..." || filter == "Durchsuche Publikationen..." ) {
        filter = "";
    }
        
    var appliedFilter = "";
    var matches = 0;
    var total = 0;
        
    jQuery(".filtered:not(.hidden):first").each(function () {
        appliedFilter = jQuery(this).attr("appliedFilter");
        matches = jQuery(this).attr("matches");
        total = jQuery(this).attr("total");
            
        if (appliedFilter != filter) {

            jQuery(".filtered:not(.hidden):first tr:not(.notfiltered)").each(function () {
                if (jQuery(this).text().search(new RegExp(filter, "i")) < 0) {
                    jQuery(this).addClass("hidden");
                    count_not++;
                } else {
                    jQuery(this).removeClass("hidden");
                    count++;
                }
            });
        
            jQuery("#filter-count").text(count);    
            jQuery("#filter-count-total").text(count+count_not);
            jQuery("span.count").removeClass("hidden");

            jQuery(this).attr("appliedFilter", filter);
            jQuery(this).attr("matches", count);
            jQuery(this).attr("total", count+count_not);
        } else {
            jQuery("#filter-count").text(matches);    
            jQuery("#filter-count-total").text(total);
        }
   });
}

// Loads the content of the teaching page referenced by the given link
function loadTeachingTerm(teachingLink) {
    if (teachingLink) {
        var link = jQuery(teachingLink).attr("href");
        jQuery(teachingLink).parent().addClass("loading");
               
        // Load the content and use only the content of the table that contains the real data
        jQuery(teachingLink).parent().load(link + " table.contentpaneopen", null, function(){
            // Indicate loaded status
            jQuery(this).removeClass("loading");
            jQuery(this).addClass("loaded");
        });
    }
}

// Loads the content of the publications page referenced by the given link
function loadPublicationList(publicationLink){
    if (publicationLink) {
        var link = jQuery(publicationLink).attr("href");
        var tagCloud = !jQuery(publicationLink).parent().parent().hasClass("noTagCloud");
        jQuery(publicationLink).parent().parent().addClass("loading");
        // Get the position of the div
        var pos = jQuery("div.publicationDiv").index(jQuery(publicationLink).parent().parent());

        if (tagCloud) {
            // Insert a field for the tagcloud
            jQuery(publicationLink).parent().before("<div id='dynacloud" + pos + "' class='dynaCloudDiv'></div>");
        }
        
        // Inject the content.
        jQuery(publicationLink).parent().load(link, null, function(){
//          jQuery(this).find("b").addClass("dynacloud");
            if (tagCloud) {
                jQuery(this).dynaCloud("#dynacloud" + pos);
            }
            // Indicate loaded status
            jQuery(this).parent().removeClass("loading");
            jQuery(this).parent().addClass("loaded");
            // Hide the link. Removing surprisingly causes an error.
            jQuery(this).wrapInner("<table></table>");
            jQuery(publicationLink).hide();

            // Reapply filter
            filterPublications("#publication-filter");
        });
    }
}


// Changes the time
function changeTime(elt) {
    var divId = jQuery(elt).attr("id");
        
    if (divId) {
        var termDivId = divId.substring(divId.lastIndexOf('_')+1, divId.length);
            
        var termDiv = jQuery("#" + termDivId);
                        
        if (termDiv) {
            jQuery("span.timeSpan").each(function () {
                jQuery(this).removeClass("current");
            });
            jQuery(elt).addClass("current");
                
            jQuery("div.term").each(function () {
                jQuery(this).addClass("hidden");
            });
            jQuery("#" + termDivId).removeClass("hidden");
            
            // Here we load the content of the new current div on demand.
            if (!jQuery("#" + termDivId).hasClass("loaded"))
            {
                var tt = jQuery("#" + termDivId + " > a.teachingLink");
                
                if (tt.length > 0) {
                    loadTeachingTerm(tt);
                }
                loadPublicationList(jQuery("#" + termDivId + " > div.publicationContent > a.publicationLink"));
            } else {
                // Reapply filter
                filterPublications("#publication-filter");
            }
        }
    }

}

// Returns part of the query-string that is needed to switch timelines to the requested time on page request
function getQSAfter(str) {
    var wls = window.location.toString();
    var pos = wls.lastIndexOf(str);
    var ret = "";
    if (pos >= 0) {
        ret = wls.substring(pos + str.length, wls.length);
        var qsEndPos = ret.indexOf("/");        
        if (qsEndPos >= 0) ret = ret.substring(0, qsEndPos);
        qsEndPos = ret.indexOf("?");        
        if (qsEndPos >= 0) ret = ret.substring(0, qsEndPos);
        qsEndPos = ret.indexOf("&");        
        if (qsEndPos >= 0) ret = ret.substring(0, qsEndPos);
        qsEndPos = ret.indexOf("=");        
        if (qsEndPos >= 0) ret = ret.substring(0, qsEndPos);
    }
    return ret;    
}


/*
window.addEvent("domready", function() {  
    $$("img").filter(function(img) { return img.hasClass("reflect"); }).reflect({});  
});
*/  
//    $$("img.reflect").reflect();



