// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Removed this since it was our only jquery and put it in the index_beta.html

// This function will find link_ class links and set up on click behaviors
function enableAjaxReferenceLinks() {
    if (!Prototype.Browser.IE) {
        // search for the link_term
        var links = $$('.info_link');
        for (var i=0; i < links.length; i++ ) {
            links[i].onclick = function() {
                var my_url = this.href;
                var my_link_name = this;
                var my_old_function = this.onclick;
                var my_url_array = my_url.split("/");
                var my_ajax_url = '/search/get_info/'+my_url_array[3]+'/'+ my_url_array[4];
                new Ajax.Request(my_ajax_url, {asynchronous:true, evalScripts:true, onCreate:function(request){ my_link_name.onclick=function() { return false; } }, onComplete:function(request){my_link_name.onclick=my_old_function}});
                return false;
            }
        }
        // search for image links
        var image_links = $$('.image_link');
        for (var p=0; p < image_links.length; p++ ) {
            image_links[p].onclick = function() {
                var my_url = this.href;
                var my_link_name = this;
                var my_old_function = this.onclick;
                var my_url_array = my_url.split("/");
                var my_ajax_url = '/search/get_info/item_image/'+ my_url_array[5];
				// trying to disable the link after click to limit double clicks
                new Ajax.Request(my_ajax_url, { asynchronous:true, evalScripts:true, onCreate:function(request){ my_link_name.onclick=function() { return false; } }, onComplete:function(request){my_link_name.onclick=my_old_function}});
                return false;
            }
        }

        // search for image links
        var text_links = $$('.text_link');
        for (var t=0; t < text_links.length; t++ ) {
            text_links[t].onclick = function() {
                var my_url = this.href;
                var my_link_name = this;
                var my_old_function = this.onclick;
                var my_url_array = my_url.split("/");
                var my_ajax_url = '/search/get_info/text_info/'+ my_url_array[5];
                new Ajax.Request(my_ajax_url, {asynchronous:true, evalScripts:true, onCreate:function(request){ my_link_name.onclick=function() { return false; } }, onComplete:function(request){my_link_name.onclick=my_old_function}});
                return false;
            }
        }
    }
}

// Old fashioned onload event cue -- TODO: Replace with prototype function?
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}


// Prototype specific 
// Gallery and scrapbook filters rewritten for easier maintenance
function revealFilter(filter_id)
{
    // hide everything after making sure they exist and are not being requested
	if ($('medium_popup') && filter_id != 'medium_popup') $('medium_popup').hide();
	if ($('location_popup') && filter_id != 'location_popup') $('location_popup').hide();
	if ($('artist_popup') && filter_id != 'artist_popup') $('artist_popup').hide();
	if ($('owner_popup') && filter_id != 'owner_popup') $('owner_popup').hide();
    if ($('subject_popup') && filter_id != 'subject_popup') $("subject_popup").hide();

    // get the active element
    active_filter = $(filter_id)
    
    // now toggle the correct one, putting it up or down depending on its state
    if (active_filter) Effect.toggle(active_filter, 'blind');
}

// Code to fade in Gallery Thumbnails on /gallery/index.html.erb

  function showPopup(id){
      if (!Prototype.Browser.IE) {
          clearTimeout(this.timeout);
          if($(id).style.display == 'none'){
              this.timeout = setTimeout(function(){new Effect.Appear(id, {duration:.3, fps:40})},700);
          }
      }
  }
  function hidePopup(id){
      if (!Prototype.Browser.IE) {
          clearTimeout(this.timeout);
          Effect.Fade(id, {duration:.3, fps:40});
      }
  }

// Code to fade in map markers



// NOTE: This is a general onLoad thing for Prototype, maybe move it to the top of the page?
Event.observe(window, 'load', function() {
	// This effect controls the fade in of the map markers on the Landscape page
	
	// if($('locations_list')) {
	// 		if($('map_marker_detail')) {
	// 			// this is a super hack to do deal with the onload of the when the map detail card is in view vs. not
	// 		}else{
	// 			// $('locations_list').hide();
	// 		    Effect.Appear('locations_list', {duration:2.2,from:0.5,to:1});
	// 		}
	// 	
	// 	}
	
    enableAjaxReferenceLinks();
	
});

