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

Array.prototype.each = function(callback) { $.each(this,callback);};
Array.prototype.unique = function() {return $.unique(this);};
Array.prototype.position = function(value) {return $.inArray(value,this);};
Array.prototype.map = function(callback) {return $.map(this,callback);};
String.prototype.trim = function() {return $.trim(this);};
String.prototype.to_i = function() {return parseInt(this);};
Number.prototype.to_s = function() {return this+'';};

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
});

$.fn.check = function(mode) {
	var mode = mode || 'on'; // if mode is undefined, use 'on' as default
	return this.each(function() {
		switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
		}
	}); 
};

jQuery.getRjs = function(url, data) {
  return $.ajax({dataType:'script', url: url, data: data || {}});
};

jQuery.postRjs = function(url, data) {
  return $.ajax({dataType:'script',type: "POST", url: url, data: data || {}});
};

jQuery.fn.turnAjax = function() {
  this.click(function() {
    $.getRjs(this.href);
    return false;
  });
};

jQuery.fn.turnAjaxPost = function() {
  this.click(function() {
    $.postRjs(this.href);
    return false;
  });
};

jQuery.fn.closeDialog = function() {
  $('#'+$(this).parents('.ui-dialog-content').attr('id')).dialog('close');
};

jQuery.fn.destroyDialog = function() {
  $('#'+$(this).parents('.ui-dialog-content').attr('id')).dialog('destroy');
};


function $F(field) {
  return $(field).val();
};

Array.prototype.last = function() {
  return this[this.length - 1];
};
Array.prototype.first = function() { return this[0];};


var IST = {
  confirm_delete: '',
  current_event: null,
  
  message_dialog: function(user_id, title, reply) {
    $('#new_message'+reply+'_user_'+user_id).dialog({
      width: 500, height: 320, title: title, resizable: false
    });
  },
  
  invite_dialog: function(user_id, title) {
    $('#new_invite_user_'+user_id).dialog({
      width: 460, height: 250, title: title, resizable: false
    });
  },
  
  change_password_dialog: function(user_id, title) {
    $('#new_password_user_'+user_id).dialog({
      width: 460, height: 170, title: title, resizable: false
    });
  },
  
  edit_contact_dialog: function(user_id, title) {
    $('#change_contact_'+user_id).dialog({
      width: 460, height: 200, title: title, resizable: false
    });
  }

};

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

$(document).ready(function(){

  var slides = $('.images_slides  a');
  if (slides.size() > 0) {
    $('.images_slides  a').lightBox({loop: true});
    
  $(".images_slides ul").simplyScroll({
			autoMode: 'loop'
		});
  }
  






  setTimeout("$('.flash_error,.flash_notice').hide('slow');", 2000);
  
  $('#loading').ajaxStart(function() {
    $(this).show();
  }).ajaxStop(function() {
    $(this).hide();
  });
  
  $('#update_profile_button').hover(function() {
    $(this).attr('src', '/images/main/update-profile2.jpg');
  }, function() {
    $(this).attr('src', '/images/main/myaccount/updateProfile.jpg');
  });  
  
  $('.index_menu_item').hover(function() {
    
    $(this).find('.index_menu_tooltip').show();
  }, function() {
     $(this).find('.index_menu_tooltip').hide();
  }); 
  
  $('a.ajax_post').turnAjaxPost();
  $('a.ajax').turnAjax();
  
  $('a.get').livequery('click', function() {
    $.get($(this).attr('href'));
    return false;
  }).attr("rel", "nofollow");

  $('#message_private').click(function() {
    if ($(this).attr('checked')) {
      $('#tags_row').hide();
    } else {
      $('#tags_row').show();
    }
    return true;
  }); 
  
  
  $('.ui-dialog-content .cancel').livequery('click', function() {
    $(this).closeDialog();
  });
  
  
  $('#new_events .event_invite a.accept,#new_events .event_invite a.decline').livequery('click', function() {
    var link = this;
    $.post(this.href, function(data){
      $(link).parents('.inv_content').html(data);
    });
    return false;
  });

  
  $('.new_invite_user form').livequery('submit', function() {
    var form = $(this);
    form.ajaxSubmit({dataType: 'script', resetForm: true, success: function() { 
      
      form.closeDialog();
    }}); 
    return false;
  });
  
  
  
  $('a.invite').livequery('click',function() {
    $.get(this.href, function(data){
      $('#container').append(data);
    });
    //$(this).parents('.menu').hide();
    return false;
  });
  
  $('.change_contact form').livequery('submit', function() {
    var form = $(this); 
    form.ajaxSubmit({dataType: 'script', resetForm: true, success: function() { 
      form.destroyDialog();
      form.parents('.change_contact').remove();
    }}); 
    return false;
  });
  
  

  $('#tag_select').change(function() {
    document.location.href = $(this).val();
    return false;
  });
  
  
  $('a.edit_contact_link').livequery('click',function() {
    $.get(this.href, function(data){
      $('#container').append(data);
    });
    //$(this).parents('.menu').hide();
    return false;
  });
  
  ['post', 'put', 'delete'].each(function(method) {
    $('a.' + method).livequery('click', function() {
      if (method == 'delete' &&  !confirm(Social.confirm_delete)) {
        return false;
      }
      $.post($(this).attr('href'), "_method=" + method);
      return false;
    }).attr("rel", "nofollow"); 
  });
 
  $('tr.recent_message').hover(function() {
    $(this).find('div.delete_message').show();
  }, function() {
    $(this).find('div.delete_message').hide();
  });
  
  //$('#learning_center .info').hover(function() {
  //  $(this).find('div.text').show();
  //}, function() {
  //  $(this).find('div.text').hide();
  //});
  
  $('#learning_center .info a').each(function(){
    $(this).lightBox();
  });

  
});