//document.write = function(text) {
//  alert(text); // or whatever
//}


function opera_stylesheet(){
	try{
		if(navigator.userAgent.indexOf("Opera")> -1){
			document.write("<link href=\"/stylesheets/screen-opera.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />");
		}
	}catch(e){
		//alert(e);
	}	
}

function webkit_stylesheet(){
	try{
		if(navigator.userAgent.indexOf("AppleWebKit")> -1){
			document.write("<link href=\"/stylesheets/screen-webkit.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />");
		}	
	}catch(e){
		//alert(e);
	}	
}


// Default all XHR requests to appending the Accept header required for Rails' RJS
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

// Call this if we are working with a JSON request
function setJSONHeaders(xhr){
  xhr.setRequestHeader("Accept", "application/json")
}

// Simple form post over AJAX
jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

jQuery.fn.submitGetWithAjax = function() {
  this.submit(function() {
    $.get(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};


// Update generic error message 
function genericError(error){
  $('#messages').html("<p class='warning'>An unexpected error occurred. Please try again.</p>");
}


// Mimics the Rails ActiveRecord error_messages_for helper
function errorMessages(errors, message){
  errWrapperPrefix = "<div class=\"errorExplanation\" id=\"errorExplanation\">";
  if(errors.length > 1){
    errorString = " errors "
  }else{
    errorString = " error "
  }
  errPrefix = "<h2>" + errors.length + errorString + message + "</h2>";
  errWrapperSuffix = "</ul></div>";
  errStr = "<ul>";
  $.each(errors, function(i, item){
    errStr += "<li>" + item + "</li>";
  });
  return errWrapperPrefix + errPrefix + errStr + errWrapperSuffix;
}


function loadTwitterFriendFeed(element){
  var url = '/twitter_statuses';
  var elem = $(element);
  $.ajax({
    url: url,
    cache: false,
    success: function(html){
      $(element).html(html);
    },
    error: function(html){
      elem.html('<li>There was a problem retrieving the feed.</li>')
    }
  });
}

// Clear and show/hide related form fields for the order form
function activateCardFields(element, object){
  var field = '#' + object + '_' + element;
  var cardType = $(field).val();  
  switch(cardType){    
  case 'switch':
    $('#card_starts_on, #card_issue_number').show();
    break;
  case 'solo':
    $('#card_starts_on, #card_issue_number').show();
    break;    
    
  default:
    $('#' + object + '_card_starts_on_2i').clearForm();
    $('#' + object + '_card_starts_on_1i').clearForm();
    $('#' + object + '_card_issue_number').clearForm();
    $('#card_starts_on, #card_issue_number').hide();
    break;
  }
}

function preactivateCardFields(object){
  activateCardFields('card_type',object);
}

// JQuery Form Clearing
$.fn.clearForm = function() {
  return this.each(function() {  
    if (this.tagName.toLowerCase() == "form") {
      return $(":input",this).clearForm();
    }
    
    switch(this.type){
      case 'text':
      case 'password':
      case 'textarea':
        this.value = '';
        break;
      case 'checkbox':
      case 'radio':
        this.checked = false;
        break;
      case 'select-multiple':
      case 'select-one':
        this.selectedIndex = -1;
        break;
      }
  });
};

function showTopNavLoginForm(){
  $('#top_nav_login_form').slideDown();
}


$(function(){
  /*
  $('#top_nav_login_form').hide();  
  $('#top_nav_login_link').click(function(){
    showTopNavLoginForm()
    return false;
  });
	$('#top_nav_login_form #user_email').autofill({value: 'Email',	defaultTextColor: '#666666', activeTextColor: '#000'});
	$('#top_nav_login_form #user_password').autofill({value: 'Password',	defaultTextColor: '#666666', activeTextColor: '#000'});
  */
})

