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

function showDefaultFeedIcon(e) {
  return showDefaultIcon(e, "/images/icons/feed.png");
}

function showDefaultWebsiteIcon(e) {
  return showDefaultIcon(e, "/images/icons/html.png");
}

function showDefaultBlogIcon(e) {
  return showDefaultIcon(e, "/images/icons/html.png");
}

function showDefaultOwnerIcon(e) {
  return showDefaultIcon(e, "/images/chimp-thumb.jpg");
}

function showDefaultIcon(e, icon) {
  e.src = icon;
  e.onerror = "";
  return true;
}

function defaultContactIcon() {
  $$('img.contact_avatar').each(function(e) {
    var i = new Image();
    i.onerror = function() {
      showDefaultOwnerIcon(e);
    }
    i.src = e.src;
  });
}

function popitup(url) {
  newwindow=window.open(url,'name','height=400,width=600,scrollbars=yes');
  if (window.focus) {newwindow.focus()}
  return false;
}

function enable_system_notice_close() {
  $$('div#system-notice a.close').each (function(e) {
    e.onclick = function() {
      new Effect.Fade(e.up(2), {duration: 0.75});
    };
  });
}

Event.observe(window, 'load', function() {
  enable_system_notice_close();
  
  // TODO: refactor the following code to a single function
  $$('img.feed_icon').each(function(e) {
    var i = new Image();
    i.onerror = function() {
      showDefaultFeedIcon(e);
    };
    i.src = e.src;
  });
  $$('img.small_icon').each(function(e) {
    var i = new Image();
    i.onerror = function() {
      showDefaultFeedIcon(e);
    };
    i.src = e.src;
  });
  $$('img.website_icon').each(function(e) {
    var i = new Image();
    i.onerror = function() {
      showDefaultWebsiteIcon(e);
    };
    i.src = e.src;
  });
  $$('img.blog_icon').each(function(e) {
    var i = new Image();
    i.onerror = function() {
      showDefaultBlogIcon(e);
    }
    i.src = e.src;
  });
  $$('img.owner_icon').each(function(e) {
    var i = new Image();
    i.onerror = function() {
      showDefaultOwnerIcon(e);
    }
    i.src = e.src;
  });
  // end TODO
  
  if ($('profile-selector') != null) {
    $('profile-selector').onchange = function(e) {
      var s = $('profile-selector');
      var profile_id = $(s.options[s.selectedIndex]).value;
      var profile_change_url = $('profile_change_url').value;
      if (profile_change_url != null) {
        location.href = profile_change_url.replace("id_goes_here", profile_id);
      }
    }
  }
});
  

function hide_content_item(hide_url, element) {
	if(element && confirm("Are you sure?")) {
		$(element).remove();
		new Ajax.Request(hide_url,
		  { method:'post', parameters: {_method: 'delete'},
		    onFailure: function(){ alert('Oops, something went wrong...') }
		  }
		);
	}
}

