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

// jquery love

jQuery(document).ready(function(){ 
  
  //highlight form sections when children fields are in focus
  
  jQuery('input[type="text"], textarea, input[type="file"], input[type="password"], input[type="checkbox"], input[type="radio"], select').focus(function(){
    jQuery(this).parents('.form_section').css('background-color','#fff7d1');
  });
  
  jQuery('input[type="text"], textarea, input[type="file"], input[type="password"], input[type="checkbox"], input[type="radio"], select').blur(function(){
    jQuery(this).parents('.form_section').css('background-color','');
  });
  
  //use the title attribute of form fields as default text within the field
  jQuery('input[type="text"], input[type="password"], textarea').each(function(){

    if(this.value == '')
    {
  	this.value = jQuery(this).attr('title');
  	jQuery(this).addClass('text-label');
    }
  	
    // on focus
  	jQuery(this).focus(function(){
  		if(this.value == jQuery(this).attr('title')) {
  			this.value = '';
  			jQuery(this).removeClass('text-label');
  		}
  	});
  	
  	// on change (mostly for autofill)
  	jQuery(this).change(function(){
      jQuery(this).removeClass('text-label');
  	});

    // on blur
  	jQuery(this).blur(function(){
  		if(this.value == '') {
  			this.value = jQuery(this).attr('title');
  			jQuery(this).addClass('text-label');
  		}
  	});
  });
  
  
  
});

// end jquery


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) {
      location.href = $('profile-selector').value;
    }
  }
});
  

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...') }
		  }
		);
	}
}

function jGrowlSuccess(message) {
  jQuery.jGrowl("<div class='success-icon'></div><p>"+ message + "</p><div class='clearfix'></div>", { header: 'Success', life: 5000 });
}

function jGrowlError(message) {
  jQuery.jGrowl("<div class='error-icon'></div><p>"+ message + "</p><div class='clearfix'></div>", { header: 'Error', life: 10000 });
}