$(document).ready(function () {

   $('#menu ul li a').each(function(index) {
      var controller = $('#controller').val();
      if(this.rel.indexOf(controller) != -1) {
         $(this).parent().addClass('selected');
      }
   });
   
   $('#submenu ul li a').each(function(index) {
      var controller = $('#controller').val();
      if(this.rel == controller) {
         $(this).addClass('current');
      }
   });   

   $('a.delete').click(function() {
      var params = $(':input', this).serializeArray();
      var $a = $(this);      
      var rel = this.rel;
	  params.push({name: '_method', value: 'DELETE'});
      if (this.title) {
	  	if (confirm(this.title)) {
			 $.post(this.href, params, function () {
				$a.parents('.' + rel).animate({opacity: 0.0}, 500, function () {
				   $(this).remove();
				});
			 });
		   
		}
	  }
	  else {
         $.post(this.href, params, function () {
            $a.parents('.' + rel).animate({opacity: 0.0}, 500, function () {
               $(this).remove();
            });
         });
      }
      return false;
   });
   
   /* modal */
   if ($('a[rel=modal]').length > 0) {
      $('a[rel=modal]').fancybox({
         'zoomSpeedIn': 0,
         'zoomSpeedOut': 0,
         'hideOnContentClick': true
      }); 
   }
   /*
   if ($('a[rel=modal]').length > 0) {
      $('a[rel=modal]').fancybox({
         'overlayShow': true,
         'zoomSpeedIn': 0,
         'zoomSpeedOut': 0,
         'hideOnContentClick': true
      }); 
   }
   */
   
   /* */
   /*
   $('.item').click(function() {
      var $a = $(this).find('a.item-link');
      if ($a.length > 0) {
         window.location = $a.attr('href');
      }
   });
   */
   
   /* validation */
   if ($('form.validate').length) {
      $('form.validate').validate();
   }
   
   
	/* toggle */
	$('.toggle').click(function(){
		$(this).parent().next('.toggable').slideToggle('fast');
		$(this).toggleClass('active'); 
		return false;
	});  

});

(function($) {
  
  $.replace = function(template, params) {
    for (var name in params) {
       template = template.replace(new RegExp('\\$' + name, 'g'), params[name]);
    }
    return template;
  }

})(jQuery);

var include = function (path)
{
   $('head').append('<script type="text/javascript" src="' + path + '"></script>');
}
